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

()[()]
    
 /*对于DP题目需要记录到底是怎样得到结果的,不一定了可以通过记录信息直接得到,可以选择数组记录其他有用信息,可以写递归来找出最后的答案。*/
#define N 111
#include<iostream>
using namespace std;
#include<cstring>
#include<cstdio>
#define inf (1<<31)-1
int f[N][N],pos[N][N],lens;
char s[N];
void print(int l,int r)/*输出序列的过程*/
{
if(l<=r)/*一定要有这一句,否则对于相邻的‘()’,就会死循环了*/
{
if(l==r)/*能到这一步,说明只能补上括号了*/
{
if(s[l]=='('||s[l]==')') printf("()");
if(s[l]=='['||s[l]==']') printf("[]");
}
else
{
if(pos[l][r]==-)/*说明该区间最左括号与最右匹配*/
{
printf("%c",s[l]);
print(l+,r-);/**/
printf("%c",s[r]);
}
else
{
print(l,pos[l][r]);
print(pos[l][r]+,r);
}
}
}
}
int main()
{
// freopen("bracket.in","r",stdin);
// freopen("bracket.out","w",stdout);
scanf("%s",s+);
lens=strlen(s+);
for(int i=;i<=lens;++i)
f[i][i]=;
/* for(int i=lens-1;i>=1;--i)
for(int j=i+1;j<=lens;++j)
{
f[i][j]=inf;
if(((s[i]=='('&&s[j]==')')||(s[i]=='['&&s[j]==']')))
{
f[i][j]=f[i+1][j-1];
pos[i][j]=-1;
} for(int k=i;k<=j-1;++k)
{
if(f[i][j]>f[i][k]+f[k+1][j])
{
f[i][j]=f[i][k]+f[k+1][j];
pos[i][j]=k;
} } }这两种都是可以得出正确答案的,但是我建议使用下面的,对于区间DP,最外层循环最好枚举区间长度,内层枚举区间*/
for(int k=;k<lens;++k)
for(int i=,j=i+k;j<=lens&&i<=lens;++j,++i)
{
f[i][j]=inf;
if((s[i]=='('&&s[j]==')')||(s[i]=='['&&s[j]==']'))
{
f[i][j]=f[i+][j-];
pos[i][j]=-;
}
/*不要加else,因为即使当前区间的最左和最右匹配,也不一定比放弃他们匹配优*/
for(int k=i;k<=j-;++k)
{
if(f[i][j]>f[i][k]+f[k+][j])
{
f[i][j]=f[i][k]+f[k+][j];
pos[i][j]=k;
} } }
print(,lens);
printf("\n");/*坑爹的POJ,没有这句,一直没对*/
//fclose(stdin);fclose(stdout);
return ;
}

区间DP POJ 1141 Brackets Sequence的更多相关文章

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

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

  2. POJ 1141 Brackets Sequence (区间DP)

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

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

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

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

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

  5. POJ 1141 Brackets Sequence

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

  6. poj 1141 Brackets Sequence (区间dp)

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

  7. poj 1141 Brackets Sequence(区间DP)

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

  8. poj 1141 Brackets Sequence ( 区间dp+输出方案 )

    http://blog.csdn.net/cc_again/article/details/10169643 http://blog.csdn.net/lijiecsu/article/details ...

  9. POJ 1141 Brackets Sequence(DP)

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

随机推荐

  1. queue_delayed_work和queue_work区别 (转http://blog.csdn.net/dosculler/article/details/7968101)

    queue_delayed_work和queue_work 一.参考文献: 1)http://www.linuxidc.com/Linux/2011-08/41655.htm queue_delaye ...

  2. Nginx部署部分https与部分http【转】

    转自 Nginx部署部分https与部分http - na_tion的专栏 - 博客频道 - CSDN.NEThttp://blog.csdn.net/na_tion/article/details/ ...

  3. MySQL sleep过多解决方法

    睡眠连接过多,会对mysql服务器造成什么影响? 严重消耗mysql服务器资源(主要是cpu, 内存),并可能导致mysql崩溃. 造成睡眠连接过多的原因? 1. 使用了太多持久连接(个人觉得,在高并 ...

  4. 011 CountDownLatch,CyclicBarrier和Semaphore

    CountDownLatch(闭锁,有译倒计数,锁寄存): public class CountDownLatchTest { /*** 比如有一个任务A,它要等待其他4个任务执行完毕之后才能执行,此 ...

  5. c++动态规划dp算法题

    问题1:找硬币,换钱的方法 输入: penny数组代表所有货币的面值,正数不重复 aim小于等于1000,代表要找的钱 输出:换钱的方法总数 解法1:经典dp,空间复杂度O(n*aim) class ...

  6. MySQL三种备份

    一)备份分类 1 2 3 4 5 6 7 8 9 10 11 12 冷备:cold backup数据必须下线后备份 温备:warm backup全局施加共享锁,只能读,不能写 热备:hot backu ...

  7. C语言花括号

    由于C语言本身就是函数式语言,说白了,C程序就是由函数构成的! 所以花括号肯定是用在函数之中,包括函数中的各种流程控制语句中. 实际上,C程序中花括号{}的作用:就是把多个单条语句用花括号{}括起来组 ...

  8. java经典面试题大全

    基本概念 操作系统中 heap 和 stack 的区别 什么是基于注解的切面实现 什么是 对象/关系 映射集成模块 什么是 Java 的反射机制 什么是 ACID BS与CS的联系与区别 Cookie ...

  9. admin组件详解

    admin组件详解 先根据admin组件启动流程复习下django项目启动至请求过来发生的事 1将admin组件注册进app 2django项目启动 3在运行到定制的admin时执行其下面的apps文 ...

  10. HDU 2053 Switch Game(开灯问题,完全平方数)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2053 题目大意:灯开始是关着的,有n盏灯,i从1数到n每当灯的序号是i的倍数的时候就对灯进行一次操作( ...