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. Unity3D MainCamera和NGUI UICamera的小插曲

    集成NGUI 在实际的项目中,经常会使用NGUI来制作UI,用Main Camera来表现3D,但是NGUI的Camer的投射是正交视图而非透视,它绑定UICamer的脚本而且它的Tag默认是Unta ...

  2. 正则基础之——环视(Lookaround)

    环视基础 环视只进行子表达式的匹配,不占有字符,匹配到的内容不保存到最终的匹配结果,是零宽度的.环视匹配的最终结果就是一个位置. 环视的作用相当于对所在位置加了一个附加条件,只有满足这个条件,环视子表 ...

  3. The Geometry has no Z values 解决办法

    from:http://dufan20086.blog.163.com/blog/static/6616452320145269343675/ 我们在创建要素时,简单的IFeatureClass.Cr ...

  4. Android 画布绘图

    我们已经介绍了Canvas,在那里,已经学习了如何创建自己的View.在第7章中也使用了Canvas来为MapView标注覆盖. 画布(Canvas)是图形编程中一个很普通的概念,通常由三个基本的绘图 ...

  5. Scrapy

    Scrapy 从Python的Urllib.Urlllib2到scrapy,当然,scrapy的性能且效率是最高的,自己之前也看过一些资料,在此学习总结下. Scrapy介绍 关于scrapy scr ...

  6. php基础09:提取表单数据

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. xp-win7-win8的基础到精通教程-系统优化减肥教程-windos装mac

    是否还在使用别人封装的系统?是否还在担心下载后的系统是有病毒的?还在为 安装好新系统后,里面安装的软件全是自己不需要的?担心流氓软件绑定浏览器主页?担心 系统重装后,自己所有的编程软件都需要重新安装? ...

  8. iOS中UIMenuController的使用

    不知你有没有发现,在你的微信朋友中,长按一段文字(正文或者评论),会弹出这样子的玩意: 要想在你的view或者viewController中实现长按弹出菜单栏你必须要调用becomeFirstResp ...

  9. 九幽2015年Q3 WP市场份额细分报告

    上周,被微软称为史诗级别的Win10纽约新品发布会已落下帷幕,90分钟.六款新品齐发的庞大阵势,相信让大家眼花缭乱.兴奋不已吧.而在新品发布会上微软也晒出了Win10发布以来的可喜成绩单:Win10系 ...

  10. 拥Bootstrap入怀——模态框(modal)篇

    置顶文章:<纯CSS打造银色MacBook Air(完整版)> 上一篇:<CSS绘制Android Robot> 作者主页:myvin 博主QQ:851399101(点击QQ和 ...