题目链接

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. WebApi系列~基于单请求封装多请求的设计~请求的安全性设计与实现

    回到目录 对于一个Http请求如何保证它的安全,这已经不是一个新的话题,对于请求的安全我们通常考虑的无非就是"请求的被篡改性"和"请求的被复制性",第一个问题我们很容易实现,可以通过参数+密钥的方式, ...

  2. Atitit 理解Monad attilax总结

    Atitit 理解Monad attilax总结 但函数式编程最大的一个问题是,函数是一个数学抽象,在现实世界中不存在,1 那既然这样就够用了,还要 Monad 干嘛?Monad 的作用在这里就体现出 ...

  3. react4 props 解析

    <body><!-- React 真实 DOM 将会插入到这里 --><div id="example"></div> <!- ...

  4. KnockoutJS 3.X API 第四章 表单绑定(8) submit、enable、disable绑定

    submit绑定目的 submit绑定即为提交绑定,通常用于form元素.这种绑定方式会打断默认的提交至服务器的操作.转而提交到你设定好的提交绑定回调函数中.如果要打破这个默认规则,只需要在回调函数中 ...

  5. 快速入门系列--MVC--05行为

    Action执行包含内容比较多,主要有同步/异步Action的概念和执行过程,Authorationfilter, ActionFiltor, ResultFilter, ExceptionFilte ...

  6. 数据访问模式:数据并发控制(Data Concurrency Control)

    1.数据并发控制(Data Concurrency Control)简介 数据并发控制(Data Concurrency Control)是用来处理在同一时刻对被持久化的业务对象进行多次修改的系统.当 ...

  7. UML基础系列:用例图

    1. 概述 用例图(Use Case Diagram)描述“用户.需求.系统功能单元”之间的关系,是参与者所能观察和使用到的系统功能模型图. 用例图用于需求分析阶段 用例图包含6个基本元素:参与者(A ...

  8. audio和video元素

    目录 [1]HTML元素 audio video source track[2]API 方法 属性 事件 audio专有 前面的话 HTML5新增了两个与媒体相关的标签,让开发人员不必依赖任何插件就能 ...

  9. PhoneGap介绍及简单部署

    一.什么是PhoneGap: PhoneGap是一个自由开放源码的开发工具和框架,允许利用HTML + JavaScript + CSS的强大功能在多个手机平台上开发程序,开发出来的程序经过在各自的平 ...

  10. 编译原理:正规式转变成DFA算法

    //将正规式转变成NFApackage hjzgg.formal_ceremony_to_dfa; import java.util.ArrayList; class Edge{ public int ...