zoukankan      html  css  js  c++  java
  • 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
    ", ans);
            else printf("I think H is wrong!
    ");
        }
        return 0;
    }
  • 相关阅读:
    OSI模型白话
    并发
    初始化与清理
    多线程
    recyclerview Adapter
    recyclerview刷新
    surfaceview
    viewgroup绘制流程
    view配置
    项目遇到的问题
  • 原文地址:https://www.cnblogs.com/zufezzt/p/4441283.html
Copyright © 2011-2022 走看看