(POJ
来源:互联网 发布:怎么用mac编程 编辑:程序博客网 时间:2024/06/05 21:51
(POJ - 2955)Brackets
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 8871 Accepted: 4757
Description
We give the following inductive definition of a “regular brackets” sequence:
the empty sequence is a regular brackets sequence,
if s is a regular brackets sequence, then (s) and [s] are regular brackets sequences, and
if a and b are regular brackets sequences, then ab is a regular brackets sequence.
no other sequence is a regular brackets sequence
For instance, all of the following character sequences are regular brackets sequences:
(), [], (()), ()[], ()[()]
while the following character sequences are not:
(, ], )(, ([)], ([(]
Given a brackets sequence of characters a1a2 … an, your goal is to find the length of the longest regular brackets sequence that is a subsequence of s. That is, you wish to find the largest m such that for indices i1, i2, …, im where 1 ≤ i1 < i2 < … < im ≤ n, ai1ai2 … aim is a regular brackets sequence.
Given the initial sequence ([([]])], the longest regular brackets subsequence is [([])].
Input
The input test file will contain multiple test cases. Each input test case consists of a single line containing only the characters (, ), [, and ]; each input test will have length between 1 and 100, inclusive. The end-of-file is marked by a line containing the word “end” and should not be processed.
Output
For each input case, the program should print the length of the longest possible regular brackets subsequence on a single line.
Sample Input
((()))
()()()
([]])
)[)(
([][][)
end
Sample Output
6
6
4
0
6
题目大意:求一个括号序列的最长匹配括号序列的长度。
思路:设f[i][j]表示区间[i,j]中的最长合法括号序列,s为输入的字符数组,考虑当前区间最优解的可能构成情况:①s[i],s[j]构成一对括号,那么最优解可能为f[i+1][j-1]+2;②最优解由两个括号序列拼成。枚举中间断点k来考虑所有可能的每一种情况中的最优解为f[i][k]+f[k+1][j]。
所以最后在所有情况中选择最大值得状态转移方程为
ps:这种括号的题很可能是区间dp或者普通dp题。
递推写法:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int INF=0x3f3f3f3f;const int maxn=105;char s[maxn];int f[maxn][maxn];int main(){ while(scanf("%s",s+1)!=EOF&&strcmp(s+1,"end")) { int len=strlen(s+1); for(int i=1;i<=len;i++) f[i][i]=0; for(int l=2;l<=len;l++) for(int i=1;i+l-1<=len;i++) { int L=i,R=i+l-1; f[L][R]=-INF; if((s[L]=='('&&s[R]==')')||(s[L]=='['&&s[R]==']')) f[L][R]=f[L+1][R-1]+2; for(int k=L;k<R;k++) f[L][R]=max(f[L][R],f[L][k]+f[k+1][R]); } printf("%d\n",f[1][len]); } return 0;}
记忆化搜索写法:
#include<cstdio>#include<cstring>#include<string>#include<iostream>using namespace std;const int INF=0x3f3f3f3f;const int maxn=205;int f[maxn][maxn];string s;int dfs(int i,int j){ if(f[i][j]!=-1) return f[i][j]; if(i>=j) return 0; if((s[i]=='('&&s[j]==')')||(s[i]=='['&&s[j]==']')) f[i][j]=dfs(i+1,j-1)+2; for(int k=i;k<j;k++) f[i][j]=max(f[i][j],dfs(i,k)+dfs(k+1,j)); return f[i][j];}int main(){ while(cin>>s) { if(s=="end") break; memset(f,-1,sizeof(f)); int len=s.size(); printf("%d\n",dfs(0,len-1)); } return 0;}
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- 简单web主页面开发
- seetaface安装配置
- jenkins学习笔记-5-清理和通知
- layer的学习与使用
- Cookie/Session机制详解
- (POJ
- win10+ubuntu双系统下,ubuntu不能访问windows的磁盘分区
- MyBatis源码分析(1)-MapConfig文件的解析
- jquery-weui的学习与使用
- 神奇的图像处理算法 【转】
- 一些图像处理相关的常用算法链接地址
- 找出200-300之间的质数并存入数组中,遍历打印数组
- 自动化简单网页测试
- 盘点8种CSS实现垂直居中水平居中的绝对定位居中技术