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. AC日记——铺地毯 洛谷 P1003(水水水水水~)

    题目描述 为了准备一个独特的颁奖典礼,组织者在会场的一片矩形区域(可看做是平面直角坐标系的第一象限)铺上一些矩形地毯.一共有 n 张地毯,编号从 1 到n .现在将这些地毯按照编号从小到大的顺序平行于 ...

  2. HASHKILL

    6ac66ed89ef9654cf25eb88c21f4ecd0是flag的MD5码,(格式为ctf{XXX_XXXXXXXXXXX_XXXXX})由一个0-1000的数字,下划线,纽约的一个区,下划 ...

  3. 10SpringMvc_springmvc快速入门小案例(注解版本)

    第一步:新建案例工程:

  4. 一个DOM元素绑定多个事件时,先执行冒泡还是捕获

    绑定在被点击元素的事件是按照代码顺序发生,其他元素通过冒泡或者捕获“感知”的事件,按照W3C的标准,先发生捕获事件,后发生冒泡事件.所有事件的顺序是:其他元素捕获阶段事件 -> 本元素代码顺序事 ...

  5. Contains Duplicate II

    Given an array of integers and an integer k, find out whether there there are two distinct indices i ...

  6. tiff或tif文件的读取

    以下是VC下读取TIFF文件的代码 char* szFileName = "K:\\地图\\fujian-DEM\\fujian1.tif"; TIFF* tiff = TIFFO ...

  7. 重新理解:ASP.NET 异步编程(转)

    http://www.cnblogs.com/xishuai/p/asp-net-async-await-and-exception-handling.html 相关博文: 异步编程 In .NET( ...

  8. 每日一SQL-善用DATEADD和DATEDIFF

    转自:http://www.dotblogs.com.tw/lastsecret/archive/2010/10/04/18097.aspx 上個星期去Tech-Day聽了幾場有趣的課,其中一堂是楊志 ...

  9. MVC3学习:利用mvc3+ajax实现登录

    用到的工具或技术:vs2010,EF code first,JQuery ajax,mvc3. 第一步:准备数据库. 利用EF code first,先写实体类,然后根据实体类自动创建数据库:或者先创 ...

  10. Spring系列: 使用aop报错:nested exception is java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$Refle

    写了个最简单的aop例子 配置文件如下 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns ...