CodeForces 612CReplace To Make Regular Bracket Sequence

来源:互联网 发布:linux ssh限制ip 编辑:程序博客网 时间:2024/06/01 23:15

题意:

括号匹配。注意只能变为同的情况的括号。

之前写没注意如果右括号入栈就应该判为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;}


阅读全文
0 0
原创粉丝点击