首页 > 代码库 > 区间DP [POJ 1141] Brackets Sequence
区间DP [POJ 1141] Brackets Sequence
Brackets Sequence
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
()[()]
区间DP、重点在输出
#include<iostream>#include<cstdio>#include<cstring>using namespace std;#define max(a,b) ((a)>(b)?(a):(b))#define INF 0x7fffffff#define N 110char s[N];int ss[N][N]; //ss[i][j]=k,i到j从k位置分开添加的括号数最少 int dp[N][N]; //dp[i][j]是在i~j区间最多括号匹配数int judge(char c1,char c2){ if(c1==‘(‘ && c2==‘)‘) return 1; if(c1==‘[‘ && c2==‘]‘) return 1; return 0;}void print(int i,int j){ if(i>j) return; else if(i==j) { if(s[i]==‘(‘ || s[i]==‘)‘) cout<<"()"; else cout<<"[]"; } else { if(ss[i][j]==-1) { cout<<s[i]; print(i+1,j-1); cout<<s[j]; } else { print(i,ss[i][j]); print(ss[i][j]+1,j); } }}int main(){ int n,i,j,k,len; gets(s+1); n=strlen(s+1); for(i=1;i<=n;i++) { dp[i][i]=1; } for(len=2;len<=n;len++) { for(i=1;i<=n-len+1;i++) { j=i+len-1; dp[i][j]=INF; for(k=i;k<j;k++) { if(dp[i][j]>dp[i][k]+dp[k+1][j]) { ss[i][j]=k; dp[i][j]=dp[i][k]+dp[k+1][j]; } } if(judge(s[i],s[j]) && dp[i][j]>dp[i+1][j-1]) { ss[i][j]=-1; dp[i][j]=dp[i+1][j-1]; } } } //cout<<dp[1][n]<<endl; print(1,n); printf("\n"); return 0;}
区间DP [POJ 1141] Brackets Sequence
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。