您的位置:首页 > 其它

括号匹配(栈和队列)

2016-02-29 21:27 351 查看
#include<iostream>
#include<stack>
#include<string>
using namespace std;
int main ()
{
string s;
int i;
char a;
stack<char>st;
while(cin>>s){
st.empty();
for(i=0;i<s.size();i++){
a=s[i];
if(a=='{'||a=='['||a=='(')
st.push(a);
else if(a=='}'){
if(st.top()=='{')
st.pop();
}
else if(a==')'){
if(st.top()=='(')
st.pop();
}
else if(a==']'){
if(st.top()=='[')
st.pop();
}
}
if(!st.empty())cout<<"NO"<<endl;
else cout<<"YES"<<endl;
}
return 0;
}
粗心大意了,浪费了太多时间,st.top()写成了a,,,,,,,
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: