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

正解:DP

解题报告:

  DP题,乍一看我居然不会做,也是醉了。开始想用贪心水过,发现会gi烂。

  详细博客:http://blog.csdn.net/lijiecsu/article/details/7589877

  不详细说了,见代码:

 #include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<vector>
using namespace std;
const int MAXN = ;
char ch[MAXN];
int l;
int f[MAXN][MAXN],c[MAXN][MAXN]; inline void output(int l,int r){
if(l>r) return ;
if(l==r) {
if(ch[l]=='(' || ch[l]==')') printf("()");
else printf("[]");
}
else{
if(c[l][r]>=) {
output(l,c[l][r]);
output(c[l][r]+,r);
}
else{
if(ch[l]=='(') {
printf("(");
output(l+,r-);
printf(")");
}
else{
printf("[");
output(l+,r-);
printf("]");
}
}
}
} inline void solve(){
scanf("%s",ch);
int len=strlen(ch);
for(int i=;i<len;i++) f[i][i]=;
for(int i=;i<len;i++) for(int j=;j<len;j++) c[i][j]=-;
for(int l=;l<=len-;l++)
for(int i=;i+l<=len-;i++){
int j=i+l;
int minl=f[i][i]+f[i+][j];
c[i][j]=i;
for(int k=i+;k<j;k++){
if(minl>f[i][k]+f[k+][j]) {
minl=f[i][k]+f[k+][j];
c[i][j]=k;
}
}
f[i][j]=minl; if(( ch[i]=='(' && ch[j]==')' ) || ( ch[i]=='[' && ch[j]==']' )) {
if(f[i][j]>f[i+][j-]) {
f[i][j]=f[i+][j-];
c[i][j]=-;
}
}
} output(,len-);
printf("\n");
} int main()
{
solve();
return ;
}

POJ1141 Brackets Sequence的更多相关文章

  1. [原]POJ1141 Brackets Sequence (dp动态规划,递归)

    本文出自:http://blog.csdn.net/svitter 原题:http://poj.org/problem?id=1141 题意:输出添加括号最少,并且使其匹配的串. 题解: dp [ i ...

  2. POJ 题目1141 Brackets Sequence(区间DP记录路径)

    Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 27793   Accepted: 788 ...

  3. POJ 1141 Brackets Sequence

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

  4. 记忆化搜索(DP+DFS) URAL 1183 Brackets Sequence

    题目传送门 /* 记忆化搜索(DP+DFS):dp[i][j] 表示第i到第j个字符,最少要加多少个括号 dp[x][x] = 1 一定要加一个括号:dp[x][y] = 0, x > y; 当 ...

  5. ZOJ1463:Brackets Sequence(间隙DP)

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

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

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

  7. [poj P1141] Brackets Sequence

    [poj P1141] Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K   Special Judge Description ...

  8. CSUOJ 1271 Brackets Sequence 括号匹配

    Description ]. Output For each test case, print how many places there are, into which you insert a ' ...

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

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

随机推荐

  1. java 21-11 数据输入、输出流和内存操作流

    IO数据流: 可以读写基本数据类型的数据 数据输入流:DataInputStream DataInputStream(InputStream in)   数据输出流:DataOutputStream ...

  2. java 16 -7 泛型方法和泛型接口(泛型类相似)

    写一个ObjectTool类 泛型方法:把泛型定义在方法上 格式 public <泛型类型> 返回类型 方法名(泛型类型) 这样的好处是: 这个泛型方法可以接收任意类型的数据 public ...

  3. android初学问题集

    1. Manifest中的Application tag用途? 2. java中的组件设计模型是指什么? 3. java Bean是指什么? 4. Proxy-Stub设计模式又指的是什么? 有要的网 ...

  4. Xcode中文乱码问题

    老师给拷贝的程序用Xcode打开中文显示是乱码,而预览里面是正常显示的,Xcode默认编码UTF-8没错的,怎么办呢? 解决办法:用自带的文本编辑器打开,全选,复制,Xcode中打开文件,粘贴,ok~ ...

  5. 05Spring_Bean属性的集合类型的注入

  6. 爱奇艺招聘uwp开发

    招聘链接:https://job.cnblogs.com/offer/53380/ 工作地点:北京-海淀 工作年限:1年 学历要求:本科 招聘分类:移动开发工程师 工资范围:面议 职位要求 1.扎实的 ...

  7. [tools]tcp/udp连通性测试

    一 端口连通性测试意义 测试网络端口可达性,确保给某些使用特定端口的app做链路连通性检测.使它们能够正常的运行起来.   二 法1 使用newclient发包,彼端tcpdump抓包观察是否能收到包 ...

  8. C语言 文件操作4--文件结构体FILE的理解以及缓冲区再讲

    //文件结构体FILE的理解以及缓冲区再讲 #include<stdio.h> #include<stdlib.h> //要点:文件结构 //struct _iobuf { / ...

  9. EBS 用户及其联系人的失效时间

    联系人失效时间还有一个SQL,从页面的联系人详情简化取得的,不如直接用pos_supplier_users_v视图 SELECT * FROM (SELECT NULL contact_req_id, ...

  10. 实现可以滑动的GrildView,类似美团网首页的GrildView功能菜单

    首先上实现效果图,不会做动态图,就先凑合着看吧 使用了网上的一个开源控件viewpagerindicator,可以自定义切换时候显示的标记,圆点,或者下划线. GrildView显示的是手机上的全部a ...