Brackets Sequence
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 27793   Accepted: 7885   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

ac代码
#include<stdio.h>
#include<string.h>
char str[330];
int a[330][330],b[330],dp[330][330];
void print(int l,int r)
{
if(l>=r)
return;
if(a[l][r]==-1)
{
print(l+1,r);
}
if(a[l][r]>0)
{
b[l]=1;
b[a[l][r]]=1;
print(l+1,a[l][r]-1);
print(a[l][r],r);
}
}
int main()
{
while(gets(str+1))
{
int i,j,k;
memset(dp,0,sizeof(dp));
memset(a,-1,sizeof(a));
memset(b,0,sizeof(b));
int len=strlen(str+1);
for(i=1;i<=len;i++)
{
dp[i][i]=1;
}
for(i=len-1;i>=1;i--)
{
for(j=i+1;j<=len;j++)
{
dp[i][j]=dp[i+1][j]+1;
//a[i][j]=-1;
for(k=i+1;k<=j;k++)
{
if((str[i]=='('&&str[k]==')')||(str[i]=='['&&str[k]==']'))
{
if(dp[i][j]>dp[i+1][k-1]+dp[k][j]-1)
{
dp[i][j]=dp[i+1][k-1]+dp[k][j]-1;
a[i][j]=k;
// b[i]=1;
// b[a[i][j]]=1;
}
}
}
}
}
print(1,len);
for(i=1;i<=len;i++)
{
if(b[i]==1)
{
printf("%c",str[i]);
}
else
if(str[i]=='('||str[i]==')')
printf("()");
else
printf("[]");
}
printf("\n");
}
}

  

POJ 题目1141 Brackets Sequence(区间DP记录路径)的更多相关文章

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

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

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

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

  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. POJ 2955:Brackets(区间DP)

    http://poj.org/problem?id=2955 题意:给出一串字符,求括号匹配的数最多是多少. 思路:区间DP. 对于每个枚举的区间边界,如果两边可以配对成括号,那么dp[i][j] = ...

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

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

  7. POJ 题目3661 Running(区间DP)

    Running Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5652   Accepted: 2128 Descripti ...

  8. 【POJ】1141 Brackets Sequence

    经典DP问题,注意输入不要使用while(xxx != EOF),否则WA,测试数据只有一组.同样的测试数据可能有多种答案.但最小长度唯一.一定不能用while,切记. #include <io ...

  9. UVA 1626 Brackets sequence 区间DP

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

随机推荐

  1. 浏览器Range,Selection等选中文本对象

    Range对象 Range 对象表示文档的连续范围区域,如用户在浏览器窗口中用鼠标拖动选中的区域. 最常见的Range是用户文本选择范围(user text selection).当用户选择了页面上的 ...

  2. JavaScript中关于地址的获取

    //取当前页面名称(不带后缀名) function pageName(){ var a = location.href; var b = a.split("/"); var c = ...

  3. PHP Fatal Error: call to undefined function mysql_connect() [duplicate]

    You shouldn't use mysql_* functions to start with. They are deprecated as of PHP 5.5. Use mysqli or ...

  4. 如何将本地文件上传到github托管

    Github开源代码库以及版本控制系统,可以托管各种git库,可以将个人Blog或小型项目托管到github,方便看起来又高大上的样子,哈哈哈,下面就看一下实践过程和成果吧!一:注册账号:地址: ([ ...

  5. SSIS excel2003文件导入列名显示为F1,F2 - FN

    问题现象: 数据源2003版本的EXCEL文件,第一列是空白,数据是从第二列开始的.所以SSIS中用EXCEL数据源倒入这个EXCEL时,所有的列显示不出来,显示的是F1,F2  - FN. 解决方法 ...

  6. PHP 底层的运行机制与原理

    PHP说简单,但是要精通也不是一件简单的事.我们除了会使用之外,还得知道它底层的工作原理. PHP是一种适用于web开发的动态语言.具体点说,就是一个用C语言实现包含大量组件的软件框架.更狭义点看,可 ...

  7. Alpha版本十天冲刺--Day6

    会议总结 队员 今天完成 遇到的问题 明天要做 感想 鲍亮 获取帖子接口,url图片解析 无 获取帖子详情接口,发帖接口 这两天都是白天睡大觉,晚上敲代码,感觉得调一下作息了,假期更加爱赖床了,还得继 ...

  8. MySQL 解压包 安装

    1.解压 2.配置mysql bin的环境变量 3.配置my-defalue.ini 配置文件 或自己建立一个my.ini文件 [mysqld] basedir=F:\mysql\mysql-5.6. ...

  9. 关于css

    已经学了四天的css.现在对于css的了解还很肤浅,首先,我对基础的还不是很了解. 级联样式表(Cascading Style Sheet)简称“CSS”,通常又称为“风格样式表(Style Shee ...

  10. touch的属性

    touch命令:建立文件   touch的功能并不是用来创建新文件的,创建文件是touch命令的一个特殊情况,touch是用来修改指定的文件的访问和修改时间属性,如果指定的文件不存在,将建立一个新的空 ...