【FZU】Problem 1411 最长配对子串
来源:互联网 发布:巨鹰飞行网络 编辑:程序博客网 时间:2024/06/05 20:30
题目链接:http://acm.fzu.edu.cn/problem.php?pid=1411
题目描述:
Problem Description
给定由(,),[,],{,},<,>构成的一个括号字符串,输出它的一个最长子串,该子串有正确的括号配对。
Input
本题有多组输入数据,每组数据只有一行括号字符串,其长度不大于10000。
Output
对于每组数据,输出该括号字符串的最长配对子串。若答案不唯一,则输出最左边一个;如果答案不存在,则输出“No Solution”。
Sample Input
[()()]][]
Sample Output
[()()]
解题思路:考察栈的知识。
参考代码:
#include<iostream>#include<string.h>#include<stack>using namespace std;struct node{int pos;char ch;}Node[10010];int main(){char s[10010];int sign[10010];int i; int pos;char ch;while(cin>>s){stack<node> sta;memset(sign,0,sizeof(sign));for(i=0; i<strlen(s); i++){if(s[i]=='(' || s[i]=='[' || s[i]=='{' || s[i]=='<'){Node[i].ch = s[i];Node[i].pos = i;sta.push(Node[i]);}else{if(!sta.empty()){pos = sta.top().pos;ch = sta.top().ch;if(ch=='('&&s[i]==')' || ch=='['&&s[i]==']' || ch=='{'&&s[i]=='}' || ch=='<'&&s[i]=='>'){sign[i] = 1;sign[pos] = 1;sta.pop();}else{ Node[i].ch = s[i];Node[i].pos = i;sta.push(Node[i]);}}}}while(!sta.empty())sta.pop();int begin,end,max=0;for(i=0; i<strlen(s); i++){if(sign[i]==0) continue;else{int j=i;int temp = 0;while(sign[j] == 1){temp ++;j++;}if(temp > max){begin = i;end = j-1;max = temp;}i = j-1;}}if(max != 0){for(i=begin; i<=end; i++)cout<<s[i];}elsecout<<"No Solution";cout<<endl;}return 0;}
- 【FZU】Problem 1411 最长配对子串
- fuzhuo------ Problem 1411 最长配对子串
- fzu Problem 2128 最长子串
- fzu Problem 2128 最长子串
- fzu Problem 2128 最长子串
- FOJ 1411 最长配对子串
- FZU 2128 最长子串
- fzu 2128 最长子串
- fzu Problem 2128 最长子串 (注意strstr函数的使用)
- 福州大学第十届校赛 & fzu 2128最长子串
- leetcode Problem 5 最长子回文串
- fzu——Problem 2088 最长队名
- fzu Problem 2129 子序列个数
- 给定一个字符串,求出最长的连续配对括号子串的长度
- Problem 2128 最长子串 (KMP+字符串 好题)
- 【最长回文子串】【平衡树】[HDU5371]Hotaru's problem
- fzu—— Problem 2129 子序列个数
- 【CodeCraft比赛】Problem 7——X-man(最长公共子串LCS变种)
- tomcat OutOfMemoryError
- webscarab安装方法
- java中final修饰的局部变量
- 百度2012年研发校园招聘
- 有很久都没传文章上来了,写了但是没传,今天全传上来
- 【FZU】Problem 1411 最长配对子串
- cocos2d-x绑lua的开发环境
- 实现分数的加减乘除
- NEFU 207 dijkstra邻接表
- POJ 2251 Dungeon Master (广度搜索)
- Android 嵌入式 linux 去掉启动时闪烁的光标 和制作启动LOGO
- 2012年百度软件研发工程师(上海)笔试题-杭州站
- windows操作系统
- hdu—2832