原题链接
#include<iostream>
#include<stack>
#include<map>
#include<string>
#include<cstdio>
using namespace std;
map<char,int> t;
void init(){
t['(']=1,t[')']=-1; //匹配括号为一对相反数
t['[']=2,t[']']=-2; //正数代表左括号,负数代表右括号
t['{']=3,t['}']=-3;
t['<']=4,t['>']=-4;
}
int main(){
init();
int n;
cin>>n;
while(n--){
bool ok=true;
stack<int> s;
string str;
cin>>str;
int len=str.length();
for(int i=0;i<len && ok;i++){
int temp=t[str[i]]; //找到这个符合是左括号还是右括号
if(temp>0) //如果是左括号
s.push(temp);
else{ //如果是右括号
if(s.empty()||temp+s.top()!=0) //如果匹配不成功
ok=false;
else //如果成功
s.pop();
}
}
if(s.empty() && ok)
cout<<"TRUE"<<endl;
else
cout<<"FALSE"<<endl;
}
}
#include<iostream>
#include<stack>
#include<map>
#include<string>
#include<cstdio>
using namespace std;
map<char,int> t;
void init(){
t['(']=1,t[')']=-1; //匹配括号为一对相反数
t['[']=2,t[']']=-2; //正数代表左括号,负数代表右括号
t['{']=3,t['}']=-3;
t['<']=4,t['>']=-4;
}
int main(){
init();
int n;
cin>>n;
while(n--){
bool ok=true;
stack<int> s;
string str;
cin>>str;
int len=str.length();
for(int i=0;i<len && ok;i++){
int temp=t[str[i]]; //找到这个符合是左括号还是右括号
if(temp>0) //如果是左括号
s.push(temp);
else{ //如果是右括号
if(s.empty()||temp+s.top()!=0) //如果匹配不成功
ok=false;
else //如果成功
s.pop();
}
}
if(s.empty() && ok)
cout<<"TRUE"<<endl;
else
cout<<"FALSE"<<endl;
}
}