Parentheses Balance UVA 673

说说:

题意就是由字符串中的[]()匹不匹配的问题。解法很简单,搞个栈就搞定了。但是题目中有一个陷阱,那就是字符串为空也是合理的。所以在读取字符串的时候最好使用gets,因为scanf会自动将换行给忽略掉的。

源代码:

#include <stdio.h>
#include <string.h>
#define MAXN 128+5

int main(){
  char stack[MAXN],c,s[MAXN];
  int p,T,i,wrong;
  //freopen("data","r",stdin);
  scanf("%d\n",&T);
  while(T--){

   p=wrong=0;
   gets(s);
   if(strlen(s)==0){//注意字符串为空的情况
    printf("Yes\n");
    continue;
   }

   for(i=0;i<strlen(s);i++){
     if(p==0)
       stack[p++]=s[i];
     else if(s[i]==']'){
       if(stack[p-1]!='['){
         wrong=1;
         break;
        }
        else
	 p--;
     }
     else if(s[i]==')'){
      if(stack[p-1]!='('){
        wrong=1;
	break;
      }
      else
        p--;
     }
     else
       stack[p++]=s[i];
   }

  if(wrong||p!=0) //结束时栈不为空,也是不合法的
    printf("No\n");
  else
    printf("Yes\n");
  }

return 0;
}
时间: 2024-11-08 19:58:46

Parentheses Balance UVA 673的相关文章

UVA - 673 - Parentheses Balance (栈的应用!)

UVA - 673 Parentheses Balance Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description  Parentheses Balance  You are given a string consisting of parentheses () and []. A string of this type is said to be co

uva 673 Parentheses Balance

Parentheses Balance You are given a string consisting of parentheses () and []. A string of this type is said to be correct: (a)if it is the empty string (b)if A and B are correct, AB is correct, (c)if A is correct, (A) and [A] is correct. Write a pr

UVa 673 Parentheses Balance(括号配对 栈)

题意  判断输入的括号序列是否是配对的 栈的基础应用  栈顶元素与输入的字符匹配就出栈咯  注意括号序列可以为空 STL栈 #include <bits/stdc++.h> using namespace std; int main() { int cas; char c; cin >> cas; getchar(); while(cas--) { stack<char> s; while((c = getchar()) != '\n') { if(s.empty())

UVA Parentheses Balance

题目如下: Parentheses Balance You are given a string consisting of parentheses () and []. A string of this type is said to be correct: (a) if it is the empty string (b) if A and B are correct, AB is correct, (c) if A is correct, (A) and [A] is correct. W

UVA 637 Parentheses Balance(栈)

题目代号:HDU 1237 题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=103&page=show_problem&problem=614 题目原文: Parentheses Balance You are given a string consisting of parentheses () and []. A string of this type

UVa 673 括号平衡

思路:简单的匹配操作,利用栈. Code: #include<stdio.h> #include<string.h> char stack[135]; int main() { int n; scanf("%d",&n); getchar(); while(n-->0) { memset(stack,0,sizeof(stack)); char c; int top=0; int flag=1; while((c=getchar())!='\n')

Week 1 # E Parentheses Balance

原题描述: E - Parentheses Balance Parentheses Balance You are given a string consisting of parentheses () and []. A string of this type is said to be correct:(a) if it is the empty string(b) if A and B are correct, AB is correct,(c) if A is correct, (A)

UVA 673(括号匹配)

本地单文件上传脚本,命名uf 这是在本机上做的测试,利用bpcs_uploader脚本实现,只是进行简单的封装,自动完善云端文件路径. 技术要点:使用dirname获取文件所在目录,使用pwd获取文件完整路径,并作为云端文件路径. #!/bin/bash cur_dir=$(cd "$(dirname "$1")"; pwd) name=$(basename "$1") /home/grm/bin/bpcs_uploader/bpcs_uploa

UVA 673 Parentheses Balance 解题心得

原题 Description You are given a string consisting of parentheses () and []. A string of this type is said to be correct: (a) if it is the empty string (b) if A and B are correct, AB is correct, (c) if A is correct, (A ) and [A ] is correct. Write a pr