poj1141 Brackets Sequence
来源:互联网 发布:印度巴基斯坦 知乎 编辑:程序博客网 时间:2024/06/06 02:54
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[i][j]为区间[i,j]中所有括号全部匹配需要的最少括号数,c[i][j]表示区间[i,j]中断开的坐标,便于待会递归输出。当i==j时,c[i][j]=-1,dp[i][j]=1;当str[i]==str[j]时,dp[i][j]=dp[i+1][j-1];再进行状态转移方程dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]).同时更新c[i][j].
#include<iostream>#include<stdio.h>#include<stdlib.h>#include<string.h>#include<math.h>#include<vector>#include<map>#include<set>#include<queue>#include<stack>#include<string>#include<algorithm>using namespace std;#define inf 99999999int dp[106][106],c[106][106];char str[106];int ok(char a,char b){ if( (a=='('&&b==')') || (a=='[' && b==']'))return 1; return 0;}void shuchu(int l,int r){ int i,j; if(l>r)return; if(l==r){ if(str[l]=='(' || str[l]==')')printf("()"); else printf("[]"); return; } if(c[l][r]!=-1){ shuchu(l,c[l][r]); shuchu(c[l][r]+1,r); return; } else{ if(str[l]=='('){ printf("("); shuchu(l+1,r-1); printf(")"); } else{ printf("["); shuchu(l+1,r-1); printf("]"); } return; }}int main(){ int n,m,i,j,len,len1,k,l,r; while(gets(str)>0) { memset(c,-1,sizeof(c)); len1=strlen(str); for(i=0;i<len1;i++){ dp[i][i]=1; } for(i=0;i<len1-1;i++){ if(ok(str[i],str[i+1]))dp[i][i+1]=0; else { dp[i][i+1]=2; c[i][i+1]=i; } } for(len=3;len<=len1;len++){ for(i=0;i+len-1<len1;i++){ j=i+len-1; dp[i][j]=inf; if(ok(str[i],str[j])){ dp[i][j]=dp[i+1][j-1]; c[i][j]=-1; } for(k=i;k<j;k++){ if(dp[i][j]>dp[i][k]+dp[k+1][j]){ dp[i][j]=dp[i][k]+dp[k+1][j]; c[i][j]=k; } } } } shuchu(0,len1-1); printf("\n"); } return 0;}
0 0
- POJ1141 Brackets Sequence
- poj1141 Brackets Sequence
- POJ1141--Brackets Sequence--DP
- POJ1141 Brackets Sequence
- POJ1141 Brackets Sequence
- POJ1141 Brackets Sequence
- poj1141 Brackets Sequence
- poj1141 Brackets Sequence
- POJ1141 Brackets Sequence(DP)
- Brackets Sequence poj1141
- POJ1141-Brackets Sequence
- poj1141 Brackets Sequence
- poj1141 Brackets Sequence 括号序列
- poj1141 Brackets Sequence 区间dp
- POJ1141 Brackets Sequence 区间DP
- POJ1141 Brackets Sequence 题解&代码
- poj1141 Brackets Sequence--区间dp
- poj1141 Brackets Sequence 动态规划 输出路径
- 第四章 编写结构化程序
- 名字的漂亮度
- Mysql 创建查询用户
- 密码 加密跟解密
- 工作流表结构设计
- poj1141 Brackets Sequence
- run as maven build的时候报错
- android 制作.9图片
- eclipse 插件打包发布
- 腾达W308r改64M内存8M闪存CPU加散热片,刷磊科NR235W/NR236W的软件
- 源码编译安装LAMP环境及配置基于域名访问的多虚拟主机
- Uva 1484 Alice and Bob's Trip(树形dp)
- OC第一个程序的解释
- ViewPager简单使用步骤