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. python成长之路【第十二篇】:RabbitMQ入门

    一.RabbitMQ介绍 解释RabbitMQ,就不得不提到AMQP(Advanced Message Queuing Protocol)协议. AMQP协议是一种基于网络的消息传输协议,它能够在应用 ...

  2. 【Hector slam】A Flexible and Scalable SLAM System with Full 3D Motion Estimation

    作者总结了SLAM前端和后端的区别 While SLAM frontends are used to estimate robot movement online in real-time, the ...

  3. contiki-rime-单跳单播

    rucb是单跳单播的最顶层,将数据以块为单位进行传输(Bulk transfer). ruc,Reliable communication,保证可靠通信,主要实现确认和序列功能. suc,Stubbo ...

  4. m.Tomcat使用openssl走APR通道配置单向和双向认证

    引用自: http://blog.csdn.net/gtuu0123/article/details/5827800(Tomcat的SSL单向认证)  http://blog.csdn.net/gtu ...

  5. Android NDK 初探,生成so文件以及调用so文件方法

    因为最近业务上涉及安全的问题 然后有一些加密解密的方法和key的存储问题 本来想存储到手机里面,但是网上说一般敏感信息不要存储到手机Sdcard上 而且我这个如果从网络建立通信获取的话,又太耗时,所以 ...

  6. 初始Java 第一课程DVD项目

    DVDSet 类: DVD DVD    删除功能 实现DVD借出功能 DVD还回功能

  7. 3d旋转

    <!DOCTYPE html><html lang="zh-cmn-Hans"><head><meta charset="utf ...

  8. Linux 系统把英文修改成中文界面

    1.一般安装后的linux系统都是英文的界面,网上查了一下各种说法都有,我只做了如下的配置就好了,下载个中文包,改一下i18n就完事了,并没有那么复杂 下面上图文: 目前是英文的界面 2.下载个中文包 ...

  9. ES5——函数,对象,方法,this

    JS由表达式和语句组成 表达式:计算出一个值,但并不进行任何操作,不改变计算机运行状态 语句:包括 声明语句,赋值语句,控制结构 函数,对象,方法,this 数组和对象:是两个非常重要的数据类型 函数 ...

  10. aliyun source.list

    电信的网络越来越不靠普.ubuntu环境使用下面的source.list deb http://mirrors.aliyun.com/ubuntu/ trusty main restricted un ...