今天比赛时一直没写出来的一道水题,题目很简单,比赛的时候心态不对,一直没写对
其实就是栈进行括号匹配再加一个数组记录层次即可
/*
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!");
}
}