代码:
利用栈
#include <iostream>
#include <cstring>
#include <stack>
#include <algorithm>
using namespace std;
int main(){
int t;
string a;
cin>>t;
cin.get();
while(t--){
stack<char> s;
cin>>a;
int tag=1;
int l=a.size();
for(int i=0;i<l;i++){
if(a[i]=='(')
s.push(a[i]);
else{
if(s.empty()){
tag=0;
break;
}
char c=s.top();
if(c=='('){
s.pop();
}
else{
tag=0;
break;
}
}
}
if(!s.empty()){
tag=0;
}
if(tag)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
return 0;
}
2098

被折叠的 条评论
为什么被折叠?



