POJ 题目1141 Brackets Sequence(区间DP记录路径)
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 27793 | Accepted: 7885 | Special Judge |
Description
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
(characters '(', ')', '[' and ']') that are situated on a single line without
any other characters among them.
Output
some regular brackets sequence that has the minimal possible length and contains
the given sequence as a subsequence.
Sample Input
([(]
Sample Output
()[()]
Source

#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记录路径)的更多相关文章
- poj 1141 Brackets Sequence 区间dp,分块记录
Brackets Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 35049 Accepted: 101 ...
- POJ 1141 Brackets Sequence(区间DP, DP打印路径)
Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...
- poj 1141 Brackets Sequence (区间dp)
题目链接:http://poj.org/problem?id=1141 题解:求已知子串最短的括号完备的全序列 代码: #include<iostream> #include<cst ...
- poj 1141 Brackets Sequence ( 区间dp+输出方案 )
http://blog.csdn.net/cc_again/article/details/10169643 http://blog.csdn.net/lijiecsu/article/details ...
- POJ 2955:Brackets(区间DP)
http://poj.org/problem?id=2955 题意:给出一串字符,求括号匹配的数最多是多少. 思路:区间DP. 对于每个枚举的区间边界,如果两边可以配对成括号,那么dp[i][j] = ...
- Ural 1183 Brackets Sequence(区间DP+记忆化搜索)
题目地址:Ural 1183 最终把这题给A了.. .拖拉了好长时间,.. 自己想还是想不出来,正好紫书上有这题. d[i][j]为输入序列从下标i到下标j最少须要加多少括号才干成为合法序列.0< ...
- POJ 题目3661 Running(区间DP)
Running Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5652 Accepted: 2128 Descripti ...
- 【POJ】1141 Brackets Sequence
经典DP问题,注意输入不要使用while(xxx != EOF),否则WA,测试数据只有一组.同样的测试数据可能有多种答案.但最小长度唯一.一定不能用while,切记. #include <io ...
- UVA 1626 Brackets sequence 区间DP
题意:给定一个括号序列,将它变成匹配的括号序列,可能多种答案任意输出一组即可.注意:输入可能是空串. 思路:D[i][j]表示区间[i, j]至少需要匹配的括号数,转移方程D[i][j] = min( ...
随机推荐
- 【转】linux下安装ssh服务器端及ssh的安全配置
一.在服务器上安装ssh的服务器端. $ sudo apt-get install openssh-server 2. 启动ssh-server. $ /etc/init.d/sshrestart 3 ...
- Linux内核创建一个新进程
张雨梅 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-10000 创建新进程 如果同一个程序被多 ...
- JavaScript中的方法重载
对js有些了解的人都知道,在js中根本就不存在像C#中的那种方法重载,而有的只是方法的覆盖,当你在js中敲入两个或多个同名的方法的时候,不管方法(函数)的参数个数怎么个不同,这个方法名只能属于最后定义 ...
- DOM常用操作总结
一.getElementById() 寻找一个有着给定 id 属性值的元素,返回值是一个有着给定 id 属性值的元素节点.如果不存在,这样的元素,它返回 null. 二.getElementsByNa ...
- jQuery 遍历方法
http://www.runoob.com/jquery/jquery-ref-traversing.html
- jq点击小图 弹出大图(更新版)
$(function(){ $(".fj1-consult").on("click",function(){ //设置弹框中图片的路径 $(".lay ...
- The ProgID of the WorkspaceName's workspace factory
The ProgID of the WorkspaceName's workspace factory [C#]public stringWorkspaceFactoryProgID {get; se ...
- Emacs下的中文输入
Emacs如此优秀的编辑器,如果输入中文不顺畅,不免遗憾.可惜现实是折腾很久也未必用得称心如意,作为一个重度(也许是中毒) Emacs使用者,根据个人经验写下此文,希望对同道中人有所帮助. 在Wind ...
- Css Js Loader For Zencart
Css Js Loader 描述:这个插件很早就出来了,可能知道人非常少 这个插件的功能是整合所有的网站的CSS和JS内容到一个文件里边. 因为CSS和JS文件到了一个文件,加快了程序的运行 在配合其 ...
- Eclipse 创建maven项目
新建maven project