View Code
#include<iostream>
#include<stack>
#include<cstdio>
#include<cstring>
using namespace std;
struct node
{
int id1;
char b;
};
node bracket[1000010];
char str[
1000010];
int cnt[
1000010];
int main()
{
char te,ch;
int t,cases=
1,k,temp;
stack<node>
q;
scanf("%d",&
t);
while(t--
)
{
memset(cnt,0,
sizeof(cnt));
scanf("%s",str);
while(!
q.empty())
q.pop();
for(
int i=
0;str[i]!=
'\0';i++
)
{
bracket[i].id1=
i;
bracket[i].b=
str[i];
}
q.push(bracket[0]);
int i,num,tem;
temp=
0,num=
0;
for(i=
1;str[i]!=
'\0';i++
)
{
if(!
q.empty())
{
te=
q.top().b;
if(te==
'{' && str[i]==
'}')
{
cnt[i]+=
2;
tem=
q.top().id1;
q.pop();
if(!
q.empty())
{
node a=
q.top();
temp=
a.id1;
if(tem==
temp)
cnt[temp]+=
cnt[i];
cnt[temp]+=cnt[i]+
cnt[tem];
}
else
{
if(tem==
0) cnt[
0]+=
cnt[i];
else cnt[
0]+=cnt[i]+
cnt[tem];
}
cnt[i]=
0;
continue;
}
if(te==
'(' && str[i]==
')')
{
cnt[i]+=
2;
tem=
q.top().id1;
q.pop();
if(!
q.empty())
{
node a=
q.top();
temp=
a.id1;
if(temp==
tem)
cnt[temp]+=
cnt[i];
cnt[temp]+=cnt[i]+
cnt[tem];
}
else
{
if(tem==
0) cnt[
0]+=
cnt[i];
else cnt[
0]+=cnt[i]+
cnt[tem];
}
cnt[i]=
0;
continue;
}
if(te==
'[' && str[i]==
']')
{
cnt[i]+=
2;
tem=
q.top().id1;
q.pop();
if(!
q.empty())
{
node a=
q.top();
temp=
a.id1;
if(tem==
temp)
cnt[temp]=cnt[i]+
cnt[tem];
cnt[temp]+=cnt[i]+
cnt[tem];
}
else
{
if(tem==
0) cnt[
0]+=
cnt[i];
else cnt[
0]+=cnt[i]+
cnt[tem];
}
cnt[i]=
0;
continue;
}
q.push(bracket[i]);
}
else
{
q.push(bracket[i]);
}
}
int max=
0;
for(k=
0;k<=strlen(str);k++
)
if(max<
cnt[k])
max=
cnt[k];
if(max)
printf("Case #%d: %d\n",cases++
,max);
else
printf("Case #%d: I think H is wrong!\n",cases++
);
}
return 0;
}
转载于:https://www.cnblogs.com/ray007great/archive/2013/05/05/3061082.html
转载请注明原文地址: https://win8.8miu.com/read-1557952.html