#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>(括号匹配)
最新推荐文章于 2022-11-06 21:29:29 发布