POJ 1141(Bracket Sequence)-区间DP
来源:互联网 发布:mac装虚拟机 编辑:程序博客网 时间:2024/05/21 12:45
Brackets Sequence
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 23158 Accepted: 6508 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
经典区间DP。。。
/************************************************* author:crazy_石头* Pro:POJ 1141 Bracket Sequence* algorithm:区间DP* Time:110ms* Judge Status:Accepted* 解题思路:* dp[i][j]表示i~j间最少添加的字符能使所有括号匹配* 那么当i和j匹配时,i~j的匹配数(dp[i][j])等于i+1~j-1* 间的匹配数,即dp[i][j]=dp[i+1][j-1];* 对于一般情况下,dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]* 不断枚举k即可;找出最小值,用path记录路径,path=-1* 表示i~j间括号得到匹配,输出递归输出即可;**************************************************/#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>using namespace std;#define LL long long#define rep(i,h,n) for(int i=(h);i<=(n);i++)#define ms(a,b) memset((a),(b),sizeof(a))#define INF 1<<29const int maxn=1500+5;const int maxm=32000+5;const LL mod=10000007 ;int dp[maxn][maxn];//i,j间最少添加几个字符串才匹配;int path[maxn][maxn];char str[maxn];inline bool match(int i,int j){ if((str[i]=='['&&str[j]==']')||(str[i]=='('&&str[j]==')')) return true; return false;}inline void output(int i,int j){ if(i>j)return; if(i==j) { if(str[i]=='['||str[i]==']')printf("[]"); else printf("()"); return ; } if(path[i][j]==-1)//path[i][j]==-1时表示匹配; { printf("%c",str[i]); output(i+1,j-1); printf("%c",str[j]); return ; } output(i,path[i][j]); output(path[i][j]+1,j);}inline void solve(){ int len=strlen(str); rep(i,0,len-1) dp[i][i]=1; for(int i=1;i<len;i++) { for(int j=0;j+i<len;j++) { int t=j+i; dp[j][t]=INF; if(match(j,t)) { dp[j][t]=dp[j+1][t-1]; path[j][t]=-1; } for(int k=j;k<t;k++) { if(dp[j][t]>dp[j][k]+dp[k+1][t]) { dp[j][t]=dp[j][k]+dp[k+1][t]; path[j][t]=k; } } } }}int main(){ while(gets(str)) { ms(dp,0); ms(path,0); int len=strlen(str)-1; solve(); output(0,len); printf("\n"); } return 0;}
- POJ 1141(Bracket Sequence)-区间DP
- POJ 1141 Brackets Sequence(区间DP)
- poj 1141Brackets Sequence[区间dp]
- POJ 1141 Brackets Sequence ( 区间DP )
- POJ 1141 - Brackets Sequence 区间DP
- POJ-1141 Brackets Sequence 经典区间DP
- POJ 1141 Brackets Sequence 区间dp
- POJ 1141 Brackets Sequence (区间DP)
- poj 1141 Brackets Sequence (区间DP)
- poj 1141 Brackets Sequence(区间dp)
- poj 1141 Brackets Sequence(区间dp)
- POJ 1141 Brackets Sequence (区间DP)
- POJ 1141 - Brackets Sequence(区间DP)
- poj 1141 Brackets Sequence(区间dp)
- POJ 1141 Brackets Sequence(区间DP)
- poj 1141 Brackets Sequence 区间dp入门
- poj 1141 Brackets Sequence(区间dp)
- POJ 1141 Brackets Sequence 区间dp
- Codeforces Round #216 (Div. 2)部分题解
- 有关数学的一些基础题
- A+B for Input-Output Practice (III)
- centos5.6 python2.4.3升级2.7教程
- unity shader
- POJ 1141(Bracket Sequence)-区间DP
- C语言笔试面试常见编程题目(更新中)
- 网络流量 pv、uv、ip 各代表的含义
- Hibernate的实例的三种状态
- 用GDB调试程序以及makefile编写
- A+B for Input-Output Practice (IV)
- 动态设置LinearLayout的高度,获取手机屏幕的大小
- struts2中result type介绍
- java 修饰符的作用一(public protected default private 组)