当前位置: 代码迷 >> 综合 >> ZOJ 2704 括号匹配栈
  详细解决方案

ZOJ 2704 括号匹配栈

热度:34   发布时间:2024-01-11 16:42:44.0
/*这道题因为int stack[]  写成了  char stack[]  导致   括号超过255就出问题了。。。。。。让我纠结了好几天,真悲剧,以后检查题一定要从变量类型,开始检查*/
#include <stdio.h>
#include <string.h>
char str[100100];
int stack[100100];
int flag[100100];
int len, i, num, max, b, top;
int main()
{memset( flag, 0, sizeof(flag) );while( scanf( "%s", str ) != EOF  ){len = strlen(str);for( i = 0, top = 0; i < len; i++ ){if( str[i] == '(' || str[i] == '[' ){stack[top] = i;top++;}if( str[i] == ')' ){if( top == 0 )continue;if(str[stack[top-1]] == '(' ){flag[ stack[top-1] ] = 1;flag[i] = 1;top--;}elsetop = 0;}if( str[i] == ']' ){if(top == 0 )continue;if( str[stack[top-1]] == '[' ){flag[ stack[top-1] ] = 1;flag[i] = 1;top--;}elsetop = 0;}}num = 0;max = 0;b = 0;for( i = 0; i < len; i++ ){if( flag[i] == 1 )num++;if( num > max ){max = num;b = i + 1 - max;}if( flag[i] == 0 )num = 0;}for( i = b; i < b + max; i++ )printf( "%c", str[i] );printf( "\n\n" );}return 0;
}