从键盘读入一个字符串,其中只含有() {} [ ] ,判断该字符串中的每种括号是否成对出现。
提示:可借助栈来实现,括号必须配对出现,如()[ ]{},这是匹配的括号,如([{])},这是不匹配的括号(中间无空格)。
输入描述
输入一个字符串(中间不包含空格)
输出描述
匹配输出yes,否则输出no
输入样例
(([{}]))
输出样例
yes
#include<iostream>
#include<stack>
#include<string>
using namespace std;
int main(){
string st;
cin>>st;
stack<char> s;
for(int i = 0;i < st.length();i ++){
if(st[i]=='('||st[i]=='['||st[i]=='{'){
s.push(st[i]);
}else{
if(s.empty()){
cout<<"no";
return 0;
}
if(s.top()=='('&&st[i]!=')'){
cout<<"no";
return 0;
}else if(s.top()=='['&&st[i]!=']'){
cout<<"no";
return 0;
}else if(s.top()=='{'&&st[i]!='}'){
cout<<"no";
return 0;
}
s.pop();
}
}
cout<<"yes";
return 0;
}
STL知识总结<--点击这里学习STL