Brackets Sequence
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 29502   Accepted: 8402   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.

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

题目大意: 给你一贯括号序列(只包含小括号和中括号),让你找出长度最小的regular brackets sequence包含此子序列.其中的regular brackets sequence定义如下:

1)空序列是一个regular brackets sequence;

2)如果s是一个regular brackets sequence,那么[s] 也是一个regular brackets sequence,(s)也是一个regular brackets sequence.

3)如果A,B都是regular brackets sequence,那么AB也是一个regular brackets sequence.

例如:()、[]、()[] 、([]) 、([])()[()]都是regular brackets sequence。

而[[[、 (((((、 ([)] 则都不是regular brackets sequence。其中以“([)]”为例,包含它最小的regular brackets sequence有两个:()[()]或者是([])[].而你只要输出其中一个就行。

//f[i][j]表示区间i~j内需要最少的字符数能够匹配,path[i][j]表示到达该状态是哪种情况,
// -1表示第一个和最后一个,其他表示中间的分段点,然后递归输出(递归改变次序)
#include<cstdio>
#include<cstring>
using namespace std;
#define N 1010
char s[N];
int f[N][N],path[N][N];
void out(int l,int r){
if(l>r) return ;
if(l==r){//到达了最后
if(s[l]=='('||s[l]==')')
printf("()");
else
printf("[]");
return ;
}
if(path[l][r]==-){//首尾,先输出开始,然后递归输出中间,最后输出结尾
putchar(s[l]);
out(l+,r-);
putchar(s[r]);
}
else{
out(l,path[l][r]);
out(path[l][r]+,r);
}
}
int main(){
while(gets(s+)){//有空串,scanf("%s"),不能读空串,然后少一个回车,会出错
int n=strlen(s+);
for(int i=;i<=n;i++) f[i][i]=;//一个的话只需一个就可以匹配
for(int x=;x<n;x++){//枚举区间长度
for(int i=;i<=n-x;i++){//枚举区间开始位置
int j=i+x;
f[i][j]=0x3f3f3f3f;
if((s[i]=='('&&s[j]==')')||(s[i]=='['&&s[j]==']')){
if(f[i+][j-]<f[i][j]){
f[i][j]=f[i+][j-];path[i][j]=-;
}
}
for(int k=i;k<j;k++){//中间分隔情况
if(f[i][k]+f[k+][j]<f[i][j]){
f[i][j]=f[i][k]+f[k+][j];path[i][j]=k;
}
}
}
}
out(,n);
putchar('\n');
}
return ;
}
//update 2.0
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int N=;
int n,f[N][N];char s[N];
bool match(char a,char b){
if(a=='('&&b==')') return ;
if(a=='['&&b==']') return ;
return ;
}
void print(int i,int j){
if(i>j) return ;
if(i==j){
if(s[i]=='('||s[i]==')') printf("()");
else printf("[]");
return ;
}
int ans=f[i][j];
if(match(s[i],s[j])&&ans==f[i+][j-]){
printf("%c",s[i]);
print(i+,j-);
printf("%c",s[j]);
return ;
}
for(int k=i;k<j;k++){
if(ans==f[i][k]+f[k+][j]){
print(i,k);
print(k+,j);
return ;
}
}
}
int main(){
while(gets(s+)){
n=strlen(s+);
memset(f,0x3f3f3f3f,sizeof f);
for(int i=;i<=n;i++){
f[i+][i]=;
f[i][i]=;
}
for(int i=n-;i>=;i--){
for(int j=i+;j<=n;j++){
f[i][j]=n;
if(match(s[i],s[j])) f[i][j]=min(f[i][j],f[i+][j-]);
for(int k=i;k<j;k++){
f[i][j]=min(f[i][j],f[i][k]+f[k+][j]);
}
}
}
//printf("%d\n",f[1][n]);
print(,n);
putchar('\n');
}
return ;
}

POJ 1141 Brackets Sequence的更多相关文章

  1. 区间DP POJ 1141 Brackets Sequence

    Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29520   Accepted: 840 ...

  2. poj 1141 Brackets Sequence 区间dp,分块记录

    Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 35049   Accepted: 101 ...

  3. POJ 1141 Brackets Sequence(区间DP, DP打印路径)

    Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...

  4. POJ 1141 Brackets Sequence (区间DP)

    Description Let us define a regular brackets sequence in the following way: 1. Empty sequence is a r ...

  5. poj 1141 Brackets Sequence (区间dp)

    题目链接:http://poj.org/problem?id=1141 题解:求已知子串最短的括号完备的全序列 代码: #include<iostream> #include<cst ...

  6. poj 1141 Brackets Sequence(区间DP)

    题目:http://poj.org/problem?id=1141 转载:http://blog.csdn.net/lijiecsu/article/details/7589877 定义合法的括号序列 ...

  7. POJ 1141 Brackets Sequence(括号匹配二)

    题目链接:http://poj.org/problem?id=1141 题目大意:给你一串字符串,让你补全括号,要求补得括号最少,并输出补全后的结果. 解题思路: 开始想的是利用相邻子区间,即dp[i ...

  8. POJ #1141 - Brackets Sequence - TODO: POJ website issue

    A bottom-up DP. To be honest, it is not easy to relate DP to this problem. Maybe, all "most&quo ...

  9. POJ 1141 Brackets Sequence(DP)

    题目链接 很早 很早之前就看过的一题,今天终于A了.状态转移,还算好想,输出路径有些麻烦,搞了一个标记数组的,感觉不大对,一直wa,看到别人有写直接输出的..二了,直接输出就过了.. #include ...

随机推荐

  1. sqlserver 死锁原因及解决方法

    其实所有的死锁最深层的原因就是一个:资源竞争 表现一: 一个用户A 访问表A(锁住了表A),然后又访问表B,另一个用户B 访问表B(锁住了表B),然后企图访问表A,这时用户A由于用户B已经锁住表B,它 ...

  2. Android Studio 第一次新建Android Gradle项目超级慢的解决方案

    大家有什么问题,欢迎问我! 注:Android Studio在第一次新建一个Gradle项目时需要下载Gradle,所以启动很慢(Gradle-bin大约三十几兆),所以我们应该事先帮他下载好. 首先 ...

  3. 搭建Android 5.0开发环境

    1.Android SDK的安装 下载地址:http://developer.android.com/index.html 访问网站的话请自备梯子 选择:adt-bundle-windows-x86_ ...

  4. IOS xib在tableview上的简单应用(通过xib自定义cell)

    UITableView是一种常用的UI控件,在实际开发中,由于原生api的局限,自定义UITableViewCell十分重要,自定义cell可以通过代码,也可以通过xib. 这篇随笔介绍的是通过xib ...

  5. 斯坦福iOS7公开课7-9笔记及演示Demo

    这一部分主要介绍了iOS的绘图.手势.协议.block.力学特效动画(包括重力.碰撞.吸附等)以及自动布局的内容. 1.绘图.手势 (1)调用一个自定义的UIView时,可以使用awakeFromNi ...

  6. SQL Server智能感知如何更新

    经常用sql server发现一个问题,比如说我刚刚添加个表或者字段,这时候在sqlserver里面写sql语句时,没有智能提示,这个问题我以前一直不是太注意.今天好好找了下解决方法,这里做下分享. ...

  7. android 进程/线程管理(二)----关于线程的迷思

    一:进程和线程的由来 进程是计算机科技发展的过程的产物. 最早计算机发明出来,是为了解决数学计算而发明的.每解决一个问题,就要打纸带,也就是打点. 后来人们发现可以批量的设置命令,由计算机读取这些命令 ...

  8. Android平台二维码之生成,扫描 & 识别

    1.二维码的前世今生 “二维条码/二维码(2-dimensional bar code)是用某种特定的几何图形按一定规律在平面(二维方向上)分布的黑白相间的图形记录数据符号信息的:在代码编制上巧妙地利 ...

  9. Monyer's Game 0~5关过关方法

    自从Monyer编写了这个通关小游戏,可谓是好事坏事参半吧! 好事是Monyer认识了许多电脑高手,包括netpatch.luoluo等,连LCX这种骨灰级选手也过来了,可谓是收获不小(所以既然我已经 ...

  10. 页面间(窗口间)的取值赋值及获取iframe下的window对象

    ①同一个窗口中,获取某个iframe的信息 <body> <iframe id="PAID" name="PA" src="Item ...