UESTC 10 In Galgame We Trust

用栈来进行括号配对,配对成功的位置都标号为1。最后在扫一遍flag数组,即可得到最多有几个1连在一起了。

 

 

#include<stdio.h>

#include<string.h>

#include<algorithm>

using namespace std;

const int maxn= 1000010;

char s[maxn];

char stack[maxn];

int sta[maxn];

int flag[maxn];

int main()

{

    int sb;

    int uu;

    scanf("%d", &sb);

    for (uu = 1; uu <= sb; uu++)

    {

        scanf("%s", s);

        int i;

        int b = -1;//栈底

        int y = strlen(s);

        memset(flag, 0, sizeof(flag));

        for (i = 0; i < y; i++)

        {

            if (b == -1)

            {

                b++;

                stack[b] = s[i];

                sta[b] = i;

            }

            else

            {

                if (s[i] == '{' || s[i] == '(' || s[i] == '[')

                {

                    b++;

                    stack[b] = s[i];

                    sta[b] = i;

                }

                else if (s[i] == ')')

                {

                    if (stack[b] == '(')

                    {



                        flag[i] = 1;

                        flag[sta[b]] = 1;

                        b--;

                    }

                    else

                    {

                        b++;

                        stack[b] = s[i];

                        sta[b] = i;

                    }

                }

                else if (s[i] == ']')

                {

                    if (stack[b] == '[')

                    {

                        flag[i] = 1;

                        flag[sta[b]] = 1;

                        b--;



                    }

                    else

                    {

                        b++;

                        stack[b] = s[i];

                        sta[b] = i;

                    }

                }

                else if (s[i] == '}')

                {

                    if (stack[b] == '{')

                    {

                        flag[i] = 1;

                        flag[sta[b]] = 1;

                        b--;



                    }

                    else

                    {

                        b++;

                        stack[b] = s[i];

                        sta[b] = i;

                    }

                }

            }

        }

        int sum = 0;

        int ans = 0;

        for (i = 0; i <= y; i++)

        {

            if (flag[i] == 1) sum++;

            else

            {

                if (sum > ans) ans = sum;

                sum = 0;

            }

        }

        printf("Case #%d: ", uu);

        if(ans!=0) printf("%d\n", ans);

        else printf("I think H is wrong!\n");

    }

    return 0;

}

 

你可能感兴趣的:(game)