• codeforces 612C Replace To Make Regular Bracket Sequence


    C. Replace To Make Regular Bracket Sequence

     

    You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >.

    The following definition of a regular bracket sequence is well-known, so you can be familiar with it.

    Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s1 and s2 be a RBS then the strings <s1>s2, {s1}s2, [s1]s2,(s1)s2 are also RBS.

    For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not.

    Determine the least number of replaces to make the string s RBS.

    Input

    The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 106.

    Output

    If it's impossible to get RBS from s print Impossible.

    Otherwise print the least number of replaces needed to get RBS from s.

    Sample test(s)
    input
    [<}){}
    output
    2
    input
    {()}[]
    output
    0
    input
    ]]
    output
    Impossible

    #include<cstdio>
    #include<cstring>
    #include<stack>
    #include<map>
    #include<algorithm>
    using namespace std;
    const int maxn=1e6+5;
    typedef long long ll;
    stack<char>S;
    int check(char x,char y)
    {
        if((x=='['&&y==']')||(x=='<'&&y=='>')||(x=='{'&&y=='}')||(x=='('&&y==')'))return 0;
        int tx=((x=='['||x=='<'||x=='{'||x=='(')?1:2);
        int ty=((y=='['||y=='<'||y=='{'||y=='(')?1:2);
        if(tx==1&&ty==2)return 1;
        else return 2;
    }
    int main()
    {
        char s[maxn];
        scanf("%s",s);
        int len=strlen(s);
        if(len&1)
        {
            puts("Impossible");
            return 0;
        }
        int ans=0;
        for(int i=0;i<len;i++)
        {
            if(S.empty()){S.push(s[i]);continue;}
            int tt=check(S.top(),s[i]);
            if(tt==2){S.push(s[i]);continue;}
            else ans+=tt,S.pop();
        }
        if(S.empty())
            printf("%d
    ",ans);
        else
            puts("Impossible");
        return 0;
    }
  • 相关阅读:
    【转】MySQL导入数据乱码的分析与解决
    查看MySQL的warning
    修改MySQL字符集
    【转】PHP乱码问题,UTF8(乱码)
    马哥Linux——第一周作业
    马哥Linux——第二周作业
    .Net内部运行过程
    Html那些事
    面向对象图解、类型转换图解,写给.Net新手
    javascript对于DOM加强
  • 原文地址:https://www.cnblogs.com/homura/p/5087823.html
Copyright © 2020-2023  润新知