zoj 2704 Brackets

输出最长的配对序列,brackets—括号可怜

忘了当时怎么讨论的了,总之用栈来存储

序列的对应的标号,标号差值与序列的

度相关,然后从small 到big输出,只知道

当时很纠结可怜

#include<stdio.h> #include<string.h> #include<stdlib.h> #include<math.h> #define maxn 100000 char target[maxn+10]; int stack[maxn+10]; int top; //栈顶的元素的下标 void push(int x) //入栈 { stack[top] = x; } void pop() //出栈 { stack[top] = 0; } int match(int m,int n) //检查是否匹配 { if(target[ stack[m] ] == '(' && target[n] == ')') return 1; if(target[ stack[m] ] == '[' && target[n] == ']') return 1; else return 0; } int main() { int i,len,max,big,small; while(scanf("%s",target)==1) { len = strlen(target); top = 0; push(len); //栈底元素,判断栈是否为空 top = 1; push(0); //target的首号元素下标 for(i=1;i<len;i++) { if(match(top,i)) { pop(); top--; } else { top++; push(i); } } if(top == 0) printf("%s\n\n",target); else if(top == len) printf("\n\n"); else if(top>0&&top<len) { stack[0] = -1; max = 0; big = 0; small = 0; int m; stack[top+1] = len; for(i=1;i<=top+1;i++) { if((m = stack[i] - stack[i-1]) > max) { max = m; big = stack[i]; small = stack[i-1]; } } for(i=small+1;i<big;i++) printf("%c",target[i]); printf("\n\n"); } } return 0; }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值