#include<iostream> #include<stack> #include<cstring> using namespace std; char str[1000005]; int main() { stack<char>s; int i,l,flag,ans; while(cin>>str) { l=strlen(str); flag=1;ans=0; for(i=0;i<l;i++) { if(str[i]=='{'||str[i]=='<'||str[i]=='('||str[i]=='[') s.push(str[i]); else { if(s.empty()) { flag=0; break; } else { if(str[i]=='>') { if(s.top()!='<') ans++; } else if(str[i]=='}') { if(s.top()!='{') ans++; } else if(str[i]==')') { if(s.top()!='(') ans++; } else if(str[i]==']') { if(s.top()!='[') ans++; } s.pop(); } } } if(!s.empty()||flag==0) cout<<"Impossible"<<endl; else cout<<ans<<endl; } return 0; }
栈<stack>(括号匹配)
猜你喜欢
转载自blog.csdn.net/luojiushenzi/article/details/80184784
今日推荐
周排行