题目链接

http://poj.org/problem?id=1141

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[i][j]表示区间i~j匹配添加括号后区间最小长度,dp[i][j]=dp[i][k]+dp[k+1][j] ,注意当s[i]=='('&&s[j]==')' || s[i]=='['&&s[j]==']' 时,特判一下dp[i][j]=min(dp[i][j],dp[i+1][j-1]+2);  这样可以找出匹配后的序列最小长度,但是题目要求输出匹配的序列,那么可以在定义一个数组v[i][j] 标记i~j区间的断开位置,如果s[i]=='('&&s[j]==')' || s[i]=='['&&s[j]==']' 时 v[i][j]==-1, 然后在递归调用输出即可;
 
代码如下:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
const int inf=0x3f3f3f3f;
char s[];
int v[][];
int dp[][]; void print(int l,int r)
{
if(r<l) return;
if(l==r)
{
if(s[l]=='('||s[l]==')')
printf("()");
else
printf("[]");
return;
}
if(v[l][r]==-)
{
if(s[l]=='(')
{
printf("(");
print(l+,r-);
printf(")");
}
else
{
printf("[");
print(l+,r-);
printf("]");
}
}
else
{
print(l,v[l][r]);
print(v[l][r]+,r);
}
} int main()
{
scanf("%s",s);
int len=strlen(s);
memset(dp,,sizeof(dp));
for(int i=; i<len; i++)
dp[i][i]=; for(int l=; l<len; l++)
{
for(int i=; i+l<len; i++)
{
dp[i][i+l]=inf;
for(int k=i; k<i+l; k++)
{
if(dp[i][i+l]>dp[i][k]+dp[k+][i+l])
{
dp[i][i+l]=dp[i][k]+dp[k+][i+l];
v[i][i+l]=k;
}
}
if(s[i]=='('&&s[i+l]==')'||s[i]=='['&&s[i+l]==']')
{
if(dp[i][i+l]>dp[i+][i+l-]+)
{
dp[i][i+l]=dp[i+][i+l-]+;
v[i][i+l]=-;
}
}
}
}
print(,len-);
printf("\n");
return ;
}

HDU 1141---Brackets Sequence(区间DP)的更多相关文章

  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,分块记录

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

  3. poj 1141 Brackets Sequence (区间dp)

    题目链接:http://poj.org/problem?id=1141 题解:求已知子串最短的括号完备的全序列 代码: #include<iostream> #include<cst ...

  4. poj 1141 Brackets Sequence ( 区间dp+输出方案 )

    http://blog.csdn.net/cc_again/article/details/10169643 http://blog.csdn.net/lijiecsu/article/details ...

  5. UVA 1626 Brackets sequence 区间DP

    题意:给定一个括号序列,将它变成匹配的括号序列,可能多种答案任意输出一组即可.注意:输入可能是空串. 思路:D[i][j]表示区间[i, j]至少需要匹配的括号数,转移方程D[i][j] = min( ...

  6. Ural 1183 Brackets Sequence(区间DP+记忆化搜索)

    题目地址:Ural 1183 最终把这题给A了.. .拖拉了好长时间,.. 自己想还是想不出来,正好紫书上有这题. d[i][j]为输入序列从下标i到下标j最少须要加多少括号才干成为合法序列.0< ...

  7. 区间DP POJ 1141 Brackets Sequence

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

  8. POJ 1141 Brackets Sequence (区间DP)

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

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

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

  10. ZOJ1463:Brackets Sequence(间隙DP)

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

随机推荐

  1. 爱上MVC~为非法进行Action的用户提供HttpStatusCodeResult

    回到目录 对一MVC来说,它有Controller和Action,其中Action用来为页面提供数据和相关逻辑,并最后将页面渲染出来,而有些action是需要一些参数的,如文章的最终页,可能需要一个I ...

  2. rewrite规则写法及nginx配置location总结

    rewrite只能放在server{},location{},if{}中,并且只能对域名后边的除去传递的参数外的字符串起作用. 例如http://seanlook.com/a/we/index.php ...

  3. atitit  opencv apiattilax总结 约500个函数 .xlsx

    atitit  opencv apiattilax总结 约500个函数 .xlsx 1.1. CxCore中文参考手册 1 1.2. 机器学习中文参考手册  knn  svm  1 1.3. CvAu ...

  4. Android笔记——了解SDK,数据库sqlite的使用

    一.adb是什么? adb的全称为Android Debug Bridge,就是起到调试桥的作用.通过adb我们可以在Eclipse中方面通过DDMS来调试Android程序,说白了就是debug工具 ...

  5. 转载:Spring AOP (上)

    工 作忙,时间紧,不过事情再多,学习是必须的.记得以前的部门老大说过:“开发人员不可能一天到晚只有工作,肯定是需要自我学习.第一:为了更充实自己,保 持进步状态.第二:为了提升技术,提高开发能力.第三 ...

  6. mac下网页中文字体优化

    最近某人吐槽某门户网站在mac下chrome字体超丑,然后发现虽然现在mac用户越来越多,但是大家依然无视mac下的字体差异,于是研究了下mac下网页中的中文字体,和大家分享. 看了一遍国内各大门户和 ...

  7. Java多线程系列--“JUC线程池”05之 线程池原理(四)

    概要 本章介绍线程池的拒绝策略.内容包括:拒绝策略介绍拒绝策略对比和示例 转载请注明出处:http://www.cnblogs.com/skywang12345/p/3512947.html 拒绝策略 ...

  8. js让程序暂停一段时间再执行

    function sleep(d){ for(var t = Date.now();Date.now() - t <= d;); } sleep(5000); //当前方法暂停5秒

  9. IP,路由,交换基础培训记录

    IP 掩码  子网划分 vlan划分(有助于减少广播压力) vlan之间互通通过交换机打通. 路由,静态路由,动态路由(学习到的),路由表,下一跳,网络位长的优先级高. 交换机,hub集线器. hub ...

  10. [git]merge和rebase的区别

    前言 我从用git就一直用rebase,但是新的公司需要用merge命令,我不是很明白,所以查了一些资料,总结了下面的内容,如果有什么不妥的地方,还望指正,我一定虚心学习. merge和rebase ...