zoj 2704 Brackets

/* 此题用cin cout TLE了n次 后来基本上原封不动用了一下C的输入输出方式 结果一次AC 可见C++输入输出方式之慢。。。 此题用了一种比较简便的做法 就是用一个sub[]记录匹配成功的序列 然后从中查找最长的序列 然后输出对应的字符串序列即可 用此种方法处理的一个很好的优点就是 可以很好的处理形如[()()][]的序列 而且不用考虑最长序列在主串的哪个位置 */ #define LOCAL #include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<cstdlib> #include<iomanip> #include<string> #include<algorithm> #include<ctime> #include<stack> #include<queue> #include<vector> #define N 100005 using namespace std; char str[N]; int match(int a,int b) {if((str[a]=='['&&str[b]==']')||(str[a]=='('&&str[b]==')')) return 1;return 0;} int main() { #ifdef LOCAL freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif int sub[N],len,i,j,l,start,end; while(gets(str)) { stack<int> s; len=strlen(str); memset(sub,0,sizeof(sub)); s.push(0); for(i=1;i<len;i++)//序号入栈 { if(s.empty()) s.push(i); else if(match(s.top(),i)){sub[s.top()]=1;sub[i]=1;s.pop();} else s.push(i); } i=start=end=l=0; while(i<len)//查找最长的1的序列 { j=0; while(i<len&&sub[i]==0) i++;j=i; while(i<len&&sub[i]) i++; if(i-j>l){l=i-j;start=j;end=i;} }//输出 for(i=start;i<end;i++)putchar(str[i]);printf("\n\n"); getchar();//接受实例间的空行 } return 0; }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值