括弧匹配检验

来源:互联网 发布:麦博煲音箱软件 编辑:程序博客网 时间:2024/05/22 18:23
 

#include<stack>

#include<string>
#include<iostream>using namespace std;string s;stack<int>str;int main(){ cin>>s; for(int i=0;i<s.size();++i) { if(s[i]=='(')str.push(1); if(s[i]=='[')str.push(2); if(s[i]==')') { if(str.top()==2) { cout<<"Wrong"; return 0; } else str.pop(); } if(s[i]==']') { if(str.top()==1) { cout<<"Wrong"; return 0; } else str.pop(); } } if(!str.empty())cout<<"Wrong"; else cout<<"OK"; return 0;}
0 0