您的位置:首页 > 其它

uva 673 Parentheses Balance

2015-08-09 19:21 483 查看




Parentheses Balanc


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 program that takes a sequence of strings of this type and check their correctness. Your program can assume that the maximum string length is 128.

Input

The file contains a positive integer n and a sequence of n strings
of parentheses () and [], one string a line.

Output

A sequence of Yes or No on the output file.

Sample Input

用栈解决,充分利用顺序性。

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<climits>
#include<string>
#include<algorithm>
#include<queue>
#include<set>
#include<vector>
#include<map>
#include<stack>
//typedef __int64 ll;
//  %I64d
char s[135];
using namespace std;
stack<char >st;
bool match(char x,char y)
{
    if(x=='('&&y==')'  ||x=='['&&y==']')  return 1;
    return 0;
}
int main()
{
    int i,j;
    int T;
    scanf("%d",&T);
    getchar();
    while(T--)
    {

        gets(s+1);
//    if(strcmp(s+1,""==0) ) {puts("Yes");continue;}
        int n=strlen(s+1);
        while(!st.empty())
        st.pop();
        for(int i=1;i<=n;i++)
        {
            if(st.empty())
            {
                st.push( s[i] );
            }
            else if(!match(st.top(),s[i]   )  )
            {
                if( s[i]=='('||s[i]=='['  )
                    st.push(s[i]);
            }
            else
            {
                st.pop();
            }
        }
      puts(st.empty()?"Yes":"No");
    }

}




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: