[poj P1141] Brackets Sequence

Time Limit: 1000MS   Memory Limit: 65536K   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

()[()]

Source

Northeastern Europe 2001

括号匹配的变形题。

这题的范围,非常的适合区间dp。

那么我们来设计一个dp。设f[i][j]为将原串中i~j全部匹配好需要增加的字符数量。

则:

先赋值正无穷。

对于j-i+1==1 ---> f[i][j]=1

对于j-i+1==2 ---> f[i][j]=cmp(a[i],a[j])?0:2

对于j-i+1>=3 ---> f[i][j]=min(f[i][j],f[i][k]+f[k+1][j]),if (cmp(a[i],a[j])) f[i][j]=min(f[i][j],f[i+1][j-1])

其中cmp代表两个字符是否匹配。

那么这样,就轻松计算出了f[1][n]。

这一题要让我们输出方案。

一般dp的题都可以用dfs递归输出方案。

具体实现应该很好想吧!但是有些细节和顺序容易搞错。

下面先给出几组数据(不一定完全相同,有spj没关系):

in1:

[]]]]

out1:

[][][][]

in2:

)))(((

out2:

()()()()()()

in3:

“空串”

out3:

“空串”

in4:

([][]([]()))[()]([])

out4:

([][]([]()))[()]([])

code:

 #include<cstdio>
 #include<cstring>
 #include<algorithm>
 #include<iostream>
 using namespace std;
 ;
 int n,f[N][N]; char a[N]; bool vis[N];
 int cmp(int x,int y) {
     ;
     ;
     ;
 }
 void dfs(int l,int r) {
     if (l>r) return;
     if (l==r) {
         if (vis[l]||vis[r]) return;
         if (a[l]=='(') printf("()"); else
         if (a[l]==')') printf("()"); else
         if (a[l]=='[') printf("[]"); else
         if (a[l]==']') printf("[]");
         vis[l]=vis[r]=;
         return;
     }
     for (int i=l; i<r; i++)
         ][r]==f[l][r]) {dfs(l,i),dfs(i+,r); return;}
     if (cmp(l,r)) {
         vis[l]=vis[r]=;
         printf("%c",a[l]);
         dfs(l+,r-);
         printf("%c",a[r]);
         return;
     }
 }
 int main() {
     scanf(),n=strlen(a+);
     ) ;
     memset(f,,sizeof f);
     ; i<=n; i++) f[i][i]=;
     ; i<n; i++)
         )) f[i][i+]=; ]=;
     ; l<=n; l++) {
         ; i<=n-l+; i++) {
             ;
             ][j-];
             ][j]);
         }
     }
     dfs(,n);
     cout<<endl;
     ;
 }

[poj P1141] Brackets Sequence的更多相关文章

  1. 区间DP POJ 1141 Brackets Sequence

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

  2. POJ 1141 Brackets Sequence

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

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

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

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

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

  5. POJ 1141 Brackets Sequence (区间DP)

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

  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(括号匹配二)

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

  9. POJ 1141 Brackets Sequence(DP)

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

随机推荐

  1. docker小结

    docker命令汇总 https://www.cnblogs.com/andy9468/p/10762742.html docker概述 https://www.cnblogs.com/andy946 ...

  2. 检测到包降级: Microsoft.Extensions.Configuration.Abstractions 从 2.1.1 降 2.1.0

    解决方法:工具-nuget管理包-程序管理控制台-选择 项目- 执行 -Install-Package Microsoft.Extensions.Configuration.Abstractions ...

  3. pip 更改国内镜像

    2 pip 更改国内镜像 pip 默认不使用国内镜像,但是我们可以自己设置 -[pypi 镜像使用帮助] 临时使用 pip install -i https://pypi.tuna.tsinghua. ...

  4. iDigital2019数字营销广告主峰会

    iDigital 数字营销品牌广告主峰会是定向邀请形式的闭门社交峰会, 现已成长为享誉亚洲的营销广告行业活动, 致力于帮助品牌营销官, 决策者和业界同行在日益整合的数字商业时代下获得战略远景.组委会邀 ...

  5. Eclipse出现:An internal error occurred during: "Retrieving archetypes:". GC overhead limit exceeded的问题解决

    网上说修改虚拟内存的方式,其实不太可行,最直接的方式就是删除以前的workspace,重新使用一个新的workspace.

  6. Java IO--NIO(二)

    在我的上一篇文章JavaNIO详解(一)中介绍了关于标准输入输出NIO相关知识, 本篇将重点介绍基于网络编程NIO(异步IO). 异步IO 异步 I/O 是一种没有阻塞地读写数据的方法.通常,在代码进 ...

  7. 利用js和JQuery定义一个导航条菜单

    利用js和JQuery定义一个导航条 效果: 一.html代码: <div class="Maintenance"> <div class="Title ...

  8. skywalking学习之路---skywalking环境从零搭建部署

    介绍 SkyWalking项目是由华为大牛吴晟开源的个人项目,目前已经加入Apache孵化器.SkyWalking项目的核心目标是针对微服务.Cloud Native.容器化架构提供应用性能监控和分布 ...

  9. mybatis源码解析12---ResultSetHandler解析

    说完了StatementHandler和ParameterHandler,接下来就需要对查询的结果进行处理了,而对于sql结果的处理是由ResultSetHandler处理的,ResultHandle ...

  10. vue移动端金融UI组件库滴滴MandMobile面向金融场景设计附功能思维导图

    vue移动端金融UI组件库滴滴MandMobile面向金融场景设计附功能思维导图 Mand Mobile是面向金融场景设计的移动端组件库,基于Vue.js实现.目前已实际应用于滴滴四大金融业务板块的1 ...