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. Codeforces Round #466

    A. Points on the line 题意 给定一条直线上\(n\)个点,要求去掉最少的点,使得直线上相距最远的两个点的距离\(\leq d\). 思路 枚举长度为\(d\)的区间. Code ...

  2. django【ORM】model字段类型

    1.AutoField 一个自增的IntegerField,一般不直接使用,Django会自动给每张表添加一个自增的primary key. 2.BigIntegerField 64位整数, -922 ...

  3. SQLite3 安装、基本操作

    1. 安装SQLite3 sudo apt-get install sqlite3 2. 安装Sqlite3编译需要的工具包 如果,你需要的话可以安装该工具包.只是为了体验一把,可以不安装.该项是可选 ...

  4. elk系列3之通过json格式采集Nginx日志【转】

    转自 elk系列3之通过json格式采集Nginx日志 - 温柔易淡 - 博客园http://www.cnblogs.com/liaojiafa/p/6158245.html preface 公司采用 ...

  5. Spring源码解读Spring IOC原理

    一.什么是Ioc/DI? IoC 容器:最主要是完成了完成对象的创建和依赖的管理注入等等. 先从我们自己设计这样一个视角来考虑: 所谓控制反转,就是把原先我们代码里面需要实现的对象创建.依赖的代码,反 ...

  6. vundle+vim8+php+xdebug

    vundle 一开始容易被坑,vundle的运行方法是自己下载,不是自己下载,在安装上vundle后,直接修改~/.vimrc中,Plugin增加插件,然后运行PluginInstall即可安装 vi ...

  7. .htaccess教程:简介、访问控制、验证、目录浏览控制

    一..htaccess简介 1.什么是.htaccess .htaccess是一个纯文本文件,里面存放着Apache服务器配置相关的一些指令,它类似于Apache的站点配置文件,如httpd.conf ...

  8. Photon3Unity3D.dll 解析二——EventData

    EventData 包含Photon事件的所有内容 Code           用于表示事件,相当于主键ID,LiteEventCode定义了一部分服务端普遍事件事件: Parameters   事 ...

  9. MVC – 7.Razor 语法

    7.1 Razor视图引擎语法 Razor通过理解标记的结构来实现代码和标记之间的顺畅切换. @核心转换字符,用来 标记-代码 的转换字符串. 语境A: @{ string rootName=&quo ...

  10. 【严蔚敏】【数据结构题集(C语言版)】1.16 自大至小依次输出读入的三个整数X,Y,Z

    #include <stdio.h> #include<stdlib.h> int main() { int x,y,z,temp; scanf("%d%d%d&qu ...