题解 UVA673 【平衡的括号 Parentheses Balance】
posted on 2021-07-16 16:44:09 | under 题解 | source
【模板】括号匹配
#include <string>
#include <iostream>
using namespace std;
char mp[128],s[100010];
int top=0;
bool check(string a){
s[top=1]='~';
for(int i=0;i*1u<a.size();i++){
if(mp[a[i]*1u]) s[++top]=a[i];
else if(mp[s[top--]*1u]!=a[i]) return 0;
}
return s[top]=='~';
}
int T;
string a;
int main(){
mp['('*1u]=')';
mp['['*1u]=']';
cin>>T;
getline(cin,a);
while(T--){
getline(cin,a);
while(*a.end()=='\r') a.erase(a.end());
cout<<(check(a)?"Yes":"No")<<endl;
}
return 0;
}
本文来自博客园,作者:caijianhong,转载请注明原文链接:https://www.cnblogs.com/caijianhong/p/solution-UVA673.html
浙公网安备 33010602011771号