2 solutions
-
1
#include<bits/stdc++.h> using namespace std; int main(){ int t; cin>>t; while(t--){ int st=1; string s; cin>>s; stack<int> q; int n=s.size(); map<char,int> h={{'{',1},{'[',2},{'(',3},{'<',4},{'>',5},{')',6},{']',7},{'}',8}}; for(int i=0;i<n;i++){ if(h[s[i]]<=4){ if(q.size()&&h[q.top()]>h[s[i]]){ st=0; } else q.push(s[i]); } else{ if(q.size()&&h[s[i]]+h[q.top()]==9){ q.pop(); continue; } else{ st=0; } } } if(q.size()) st=0; if(st) cout<<"YES"<<endl; else cout<<"NO"<<endl; } return 0; }
-
-1
#include<bits/stdc++.h> using namespace std; int main() { int t; cin>>t; map<char,int> h={{'{',1},{'[',2},{'(',3},{'<',4},{'>',5},{')',6},{']',7},{'}',8}}; //定义符号优先级 while(t--) { string s; cin>>s; int n=s.size(); stack<char> stk; bool st=true; for(int i=0;i<n;i++) { if(h[s[i]]<=4) //左括号 { if(stk.size()&&h[stk.top()]>h[s[i]]) //左边有元素,且左边的优先级大于当前元素 { st=false; } else //符合要求 stk.push(s[i]); } else { if(stk.size()&&h[stk.top()]+h[s[i]]==9) //符合要求且匹配 { stk.pop(); //删除栈顶 continue; } else st=false; //不匹配 } } if(stk.size()) st=false; //遍历完了还有字符串 if(st==true) cout<<"YES"<<endl; else cout<<"NO"<<endl; } return 0; }
- 1
Information
- ID
- 970
- Time
- 1000ms
- Memory
- 256MiB
- Difficulty
- 5
- Tags
- (None)
- # Submissions
- 19
- Accepted
- 7
- Uploaded By