UVa 673 Parentheses Balance

UVa 673 Parentheses Balance

673 Parentheses Balance Accepted C++ 0.192

简单的括号匹配:
wa了几次

代码:
 1#include<iostream>
 2#include<string>
 3#include<stack>
 4using namespace std;
 5int main()
 6{
 7    int i,t;
 8    cin>>t;
 9    getchar();
10    string str;
11    while(t--)
12    {
13     //cin>>str;
14     getline(cin,str);
15     if(str.size()==0){cout<<"Yes"<<endl;continue;}
16     stack<char> s;
17     for(i=0;i<str.size();i++)
18     {
19         if(str[i]=='['||str[i]=='(')s.push(str[i]);
20         else
21         {
22             if(str[i]==')')
23             {
24                if(s.empty()||s.top()!='(')break;
25                else s.pop();
26             }

27             else if(str[i]==']')
28             {      
29                    if(s.empty()||s.top()!='[')break;
30                   else s.pop();
31             }

32             else break;//刚开始做就是忘了这句
33         }

34     }

35     if(s.empty()&&i==str.size())cout<<"Yes"<<endl;
36     else cout<<"No"<<endl;
37              
38    }

39    
40    return 0;
41}

你可能感兴趣的:(UVa 673 Parentheses Balance)