题意:
括号匹配。注意只能变为同的情况的括号。
之前写没注意如果右括号入栈就应该判为Impos
#include <iostream>
#include <stack>
#include <stdio.h>
using namespace std;
char s[10000005];
int tb[1000];
int main()
{
tb['[']=1;
tb['(']=2;
tb['{']=3;
tb['<']=4;
tb[']']=-1;
tb[')']=-2;
tb['}']=-3;
tb['>']=-4;
stack<int >q;
scanf("%s",s);
int ans=0;
for(int i=0; s[i]; i++)
{
if(q.empty())
{
q.push(i);
}
else
{
int t=q.top();
if(tb[s[t]]>0)
{
if(tb[s[i]]>0)
q.push(i);
else
{
if(tb[s[i]]==-tb[s[t]])
{
q.pop();
continue;
}
ans++;
q.pop();
}
}
else
{
break;
}
}
}
if(!q.empty())
{
printf("Impossible\n");
}
else
{
printf("%d\n",ans);
}
return 0;
}