标签:
#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; }
标签:
原文地址:http://www.cnblogs.com/zufezzt/p/4441283.html