閱讀179 返回首頁    go 阿裏雲 go 技術社區[雲棲]


UESTC 1823 In Galgame We Trust 模擬

今天比賽時一直沒寫出來的一道水題,題目很簡單,比賽的時候心態不對,一直沒寫對

其實就是棧進行括號匹配再加一個數組記錄層次即可

/*
author:jxy
lang:C/C++
university:China,Xidian University
**If you need to reprint,please indicate the source**
*/
#include <iostream>
#include <cstdio>
#include <stack>
using namespace std;
stack<char> st;
int ans[1000005],now;
int Max;
int main()
{
    int T,C=0;
    scanf("%d",&T);
    getchar();
    while(T--)
    {
        Max=now=0;
        char s,aim;
        ans[now]=0;
        while(!st.empty())st.pop();
        while(1)
        {
            s=getchar();
            if(s=='\n') break;
            if(s=='{'||s=='['||s=='(')
            {
                if(s=='(')s++;
                else s+=2;
                st.push(s);
                ans[++now]=0;
            }
            else
            {
                if(st.empty())
                {
                    now=0;
                    ans[now]=0;
                }
                else if(s==st.top())
                {
                    st.pop();
                    ans[now]+=2;
                    ans[now-1]+=ans[now];
                    now--;
                    Max=max(Max,ans[now]);
                }
                else
                {
                    while(!st.empty())st.pop();
                    now=0;
                    ans[now]=0;
                }
            }
        }
        printf("Case #%d: ",++C);
        if(Max)printf("%d\n",Max);
        else puts("I think H is wrong!");
    }
}


最後更新:2017-04-03 18:51:45

  上一篇:go ConfigHelper 配置文件輔助類
  下一篇:go 數碼管移動