[ACM] poj 1141 Brackets Sequence (动态规划)
来源:互联网 发布:分时数据下载 编辑:程序博客网 时间:2024/06/05 07:21
Brackets Sequence
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 23884 Accepted: 6727 Special Judge
Description
Let us define a regular brackets sequence in the following way:
1. Empty sequence is a regular sequence.
2. If S is a regular sequence, then (S) and [S] are both regular sequences.
3. If A and B are regular sequences, then AB is a regular sequence.
For example, all of the following sequences of characters are regular brackets sequences:
(), [], (()), ([]), ()[], ()[()]
And all of the following character sequences are not:
(, [, ), )(, ([)], ([(]
Some sequence of characters '(', ')', '[', and ']' is given. You are to find the shortest possible regular brackets sequence, that contains the given character sequence as a subsequence. Here, a string a1 a2 ... an is called a subsequence of the string b1 b2 ... bm, if there exist such indices 1 = i1 < i2 < ... < in = m, that aj = bij for all 1 = j = n.
1. Empty sequence is a regular sequence.
2. If S is a regular sequence, then (S) and [S] are both regular sequences.
3. If A and B are regular sequences, then AB is a regular sequence.
For example, all of the following sequences of characters are regular brackets sequences:
(), [], (()), ([]), ()[], ()[()]
And all of the following character sequences are not:
(, [, ), )(, ([)], ([(]
Some sequence of characters '(', ')', '[', and ']' is given. You are to find the shortest possible regular brackets sequence, that contains the given character sequence as a subsequence. Here, a string a1 a2 ... an is called a subsequence of the string b1 b2 ... bm, if there exist such indices 1 = i1 < i2 < ... < in = m, that aj = bij for all 1 = j = n.
Input
The input file contains at most 100 brackets (characters '(', ')', '[' and ']') that are situated on a single line without any other characters among them.
Output
Write to the output file a single line that contains some regular brackets sequence that has the minimal possible length and contains the given sequence as a subsequence.
Sample Input
([(]
Sample Output
()[()]
Source
Northeastern Europe 2001
参考资料:
http://www.cnblogs.com/android-html5/archive/2010/05/30/2534035.html
http://blog.csdn.net/lijiecsu/article/details/7589877
dp[i][j]更新的顺序
假设字符串的长度为6
(0,1) (1,2) (2,3) (3,4) (4,5)
(0,2) (1,3) (2,4) (3,5)
(0,3) (1,4) (2,5)
(0,4) (1,5)
(0,5)
任何可能的区间都在里面,而且区间长度由小到大,这也符合动规把大问题转化为小问题的思想。
代码:
#include <iostream>#include <algorithm>#include <stdio.h>#include <string.h>#include <stdlib.h>using namespace std;const int maxn=250;const int inf=0x7fffffff;char str[maxn];int dp[maxn][maxn],ps[maxn][maxn];//dp[i][j]代表串从i到j需要添加几个字符才能合法,ps[i][j],记录的从i到j在哪里断成两部分int len;void print(int i,int j)//递归输出{ if(i>j) return; if(i==j) { if(str[i]=='('||str[i]==')') cout<<"()"; else cout<<"[]"; } else if(ps[i][j]==-1)//从i到j括号匹配 { cout<<str[i]; print(i+1,j-1); cout<<str[j]; } else//不匹配分成两部分输出 { print(i,ps[i][j]); print(ps[i][j]+1,j); }}int main(){ cin>>str; len=strlen(str); memset(dp,0,sizeof(dp)); for(int i=0;i<len;i++) dp[i][i]=1; for(int k=1;k<len;k++)//区间从小到大 for(int i=0;i+k<len;i++) { int j=i+k; dp[i][j]=inf; if((str[i]=='('&&str[j]==')')||(str[i]=='['&&str[j]==']')) { dp[i][j]=dp[i+1][j-1]; ps[i][j]=-1;//括号匹配,不用添加另外的括号 } for(int mid=i;mid<j;mid++)//看是否能划分为两部分 { if(dp[i][j]>(dp[i][mid]+dp[mid+1][j])) { dp[i][j]=dp[i][mid]+dp[mid+1][j]; ps[i][j]=mid;//mid为串i到j区间断开的位置 } } } print(0,len-1); cout<<endl; return 0;}
0 0
- [ACM] poj 1141 Brackets Sequence (动态规划)
- [ACM] POJ 1141 Brackets Sequence (区间动态规划)
- POJ 1141 Brackets Sequence 动态规划
- 动态规划 Brackets Sequence poj 1141
- 动态规划 Brackets Sequence poj 1141
- POJ 1141 Brackets Sequence 动态规划
- poj 1141(Brackets Sequence 动态规划)
- [POJ 1141]Brackets Sequence(划分型动态规划)
- 【UVA】1626-Brackets sequence(动态规划)
- Brackets Sequence (poj 1141)
- Brackets Sequence (POJ-1141)
- 【动态规划】POJ 1161 & ZOJ1463 & XMU 1033 Brackets sequence
- POJ 2955 Brackets(动态规划)
- POJ 1141 Brackets Sequence
- POJ 1141 Brackets Sequence
- Brackets Sequence--poj--1141
- poj 1141 Brackets Sequence
- poj 1141 Brackets Sequence
- 微软来校讲课
- IAR Goto definition of不可用的解决方法
- Codeforces Round #152 (Div. 2)
- 我和我的输入提示----XHTree.h
- 折半查找
- [ACM] poj 1141 Brackets Sequence (动态规划)
- “iOS 推送通知”详解:从创建到设置到运行
- 如何更好的深入学习andorid
- [leetcode] Sort List
- Linux U盘挂载
- NTFS文件系统分析
- 代码重构步骤
- Android开发之自定义控件-YouKu圆盘菜单
- C_关于qsort和sort排序方法的相应整理