UVa 673 Parentheses Balance
673 | Parentheses Balance | Accepted | C++ | 0.192 |
簡單的括號匹配:
wa了幾次

代碼:
1
#include<iostream>
2
#include<string>
3
#include<stack>
4
using namespace std;
5
int 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
}
posted on 2010-05-11 10:52 田兵 閱讀(1529) 評論(2) 編輯 收藏 引用 所屬分類: 算法筆記