7 solutions
-
1
#include <bits/stdc++.h> using namespace std; int main() { int n; cin>>n; while(n--) { stack <char> s; string x;//字符串x用于最开始的存储 cin>>x; getchar(); int ret=1,l=x.size();//ret用于记录可行性 for(int i=0;i<l;i++) { if(s.empty()&&(x[i]=='}'||x[i]=='>'||x[i]==']'||x[i]==')')) {ret=0;break;}//栈为空的时候如果匹配到反括号肯定是不行的 if(x[i]=='{'||x[i]=='<'||x[i]=='('||x[i]=='[') s.push(x[i]); else { switch(x[i])//开始匹配 { case '}':if('{'==s.top()) { s.pop(); } else ret=0; break; case ')':if('('==s.top()) { s.pop(); } else ret=0; break; case '>':if('<'==s.top()) { s.pop(); } else ret=0; break; case ']':if('['==s.top()) { s.pop(); } else ret=0; break; } } } ret==1&&s.empty()?cout<<"YES"<<endl:cout<<"NO"<<endl;//根据ret判断 } return 0; }
Information
- ID
- 339
- Time
- 1000ms
- Memory
- 256MiB
- Difficulty
- 3
- Tags
- # Submissions
- 148
- Accepted
- 39
- Uploaded By