uva 673 Parentheses Balance

来源:互联网 发布:网络电影播放器排行榜 编辑:程序博客网 时间:2024/06/13 13:09

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;//  %I64dchar 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");    }}


0 0
原创粉丝点击