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

  1. ([(]

Sample Output

  1. ()[()]

Source

ac代码
  1. #include<stdio.h>
  2. #include<string.h>
  3. char str[330];
  4. int a[330][330],b[330],dp[330][330];
  5. void print(int l,int r)
  6. {
  7. if(l>=r)
  8. return;
  9. if(a[l][r]==-1)
  10. {
  11. print(l+1,r);
  12. }
  13. if(a[l][r]>0)
  14. {
  15. b[l]=1;
  16. b[a[l][r]]=1;
  17. print(l+1,a[l][r]-1);
  18. print(a[l][r],r);
  19. }
  20. }
  21. int main()
  22. {
  23. while(gets(str+1))
  24. {
  25. int i,j,k;
  26. memset(dp,0,sizeof(dp));
  27. memset(a,-1,sizeof(a));
  28. memset(b,0,sizeof(b));
  29. int len=strlen(str+1);
  30. for(i=1;i<=len;i++)
  31. {
  32. dp[i][i]=1;
  33. }
  34. for(i=len-1;i>=1;i--)
  35. {
  36. for(j=i+1;j<=len;j++)
  37. {
  38. dp[i][j]=dp[i+1][j]+1;
  39. //a[i][j]=-1;
  40. for(k=i+1;k<=j;k++)
  41. {
  42. if((str[i]=='('&&str[k]==')')||(str[i]=='['&&str[k]==']'))
  43. {
  44. if(dp[i][j]>dp[i+1][k-1]+dp[k][j]-1)
  45. {
  46. dp[i][j]=dp[i+1][k-1]+dp[k][j]-1;
  47. a[i][j]=k;
  48. // b[i]=1;
  49. // b[a[i][j]]=1;
  50. }
  51. }
  52. }
  53. }
  54. }
  55. print(1,len);
  56. for(i=1;i<=len;i++)
  57. {
  58. if(b[i]==1)
  59. {
  60. printf("%c",str[i]);
  61. }
  62. else
  63. if(str[i]=='('||str[i]==')')
  64. printf("()");
  65. else
  66. printf("[]");
  67. }
  68. printf("\n");
  69. }
  70. }

  

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. static 作用

    静态,定义静态变量或者静态函数的时候使用该关键字. 被定义为static的函数,可以不需要new一个新类别而直接调用 比如Math类里有一个,public static sub()方法,那么你可以直接 ...

  2. VMware Ubuntu Kaldi

    1.VMware10.0 秘钥网上搜就好了 2.ubuntu12.04 (1)安了几次14.04,16.04,12.04,最后窗口和分辨率的bug 还是没有解决 (2)终端显示菱形乱码的解决:直接用英 ...

  3. win7配置ftp服务

    1.首先开启ftp服务 2.配置ftp站点 3.让ftp服务器通过防火墙 4.编辑ftp访问权限,使用户能通过账号密码访问ftp,当然,在此之前,需要创建一个新的用户 到此,就可以远程访问ftp了

  4. Could not resolve placeholder 解决方案

    spring 配置加载properties文件的时候,报 Could not resolve placeholder 错误. 经过仔细查找,排除文件路径,文件类容错误的原因,经过查找相关资料,出现&q ...

  5. 使用JavaMail实现发送邮件功能

    package com.dunn.model; import java.util.Properties; import javax.mail.Address; import javax.mail.Me ...

  6. Sprint1(第三天11.16)

    Sprint1第一阶段 1.类名:软件工程-第一阶段 2.时间:11.14-11.23 3.选题内容:web版-餐厅到店点餐系统 4.团队博客地址: http://www.cnblogs.com/qu ...

  7. JAVA GUI

    JAVA GUI中的事件处理:   委托事件模型:事件源对象和监听器对象具有绑定关系   一个监听器可以绑定多个事件源 一个事件源也可以绑定多个监听器 监听器有各自监听的事件类型   设置容器的布局管 ...

  8. guava学习--Function、Predicate

    Function用于同步转换. Predicate用于过滤. import java.util.Collection; import java.util.Iterator; import java.u ...

  9. POJ 2528 Mayor's posters

    Mayor's posters Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub ...

  10. Hammer.js

    一.前言 移动端框架当前还处在初级阶段,但相对于移动端的应用来说已经有很长时间了.虽然暂时还没有PC端开发的需求量大,但移动端的Web必然是一种趋势,在接触移动端脚本的过程中,最开始想到的是juqer ...