A bottom-up DP. To be honest, it is not easy to relate DP to this problem. Maybe, all "most"\"least" problems can be solved using DP..

Reference: http://blog.sina.com.cn/s/blog_8e6023de01014ptz.html

There's an important details to AC: in case of "())", there are 2 solutions: ()() and (()). For the AC code, the former one is preferred.

  1. // 1141
  2. // http://blog.sina.com.cn/s/blog_8e6023de01014ptz.html
  3. //
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <memory.h>
  7.  
  8. #define MAX_LEN 101
  9.  
  10. bool isMatched(char *in, int i, int j)
  11. {
  12. return (in[i] == '(' && in[j] == ')') || (in[i] == '[' && in[j] == ']');
  13. }
  14. void printPath(int path[MAX_LEN][MAX_LEN], int i, int j, char in[MAX_LEN])
  15. {
  16. int sInx = path[i][j];
  17. if (sInx == -)
  18. {
  19. if (i == j)
  20. {
  21. //printf("Complete @ %d\n", i);
  22. switch (in[i])
  23. {
  24. case '(':
  25. case ')': printf("()"); break;
  26. case '[':
  27. case ']': printf("[]"); break;
  28. }
  29. return;
  30. }
  31. else if (i + == j)
  32. {
  33. //printf("Already matched: [%d, %d]\n", i, j);
  34. printf("%c%c", in[i], in[j]);
  35. return;
  36. }
  37. else if ((i+) < j)
  38. {
  39. printf("%c", in[i]);
  40. printPath(path, i + , j - , in);
  41. printf("%c", in[j]);
  42. }
  43. }
  44. else
  45. {
  46. printPath(path, , path[i][j], in);
  47. //printf("Break @ %d\n", path[i][j], in);
  48. printPath(path, path[i][j] + , j, in);
  49. }
  50. }
  51.  
  52. void calc(char in[MAX_LEN])
  53. {
  54. unsigned slen = strlen(in);
  55. if (slen == )
  56. {
  57. printf("\n");
  58. return;
  59. }
  60. else if (slen > )
  61. {
  62. int dp[MAX_LEN][MAX_LEN];
  63. int path[MAX_LEN][MAX_LEN];
  64.  
  65. // Init
  66. for (int i = ; i < MAX_LEN; i ++)
  67. for (int j = ; j < MAX_LEN; j++)
  68. {
  69. dp[i][j] = 0xFFFFFF;
  70. path[i][j] = -;
  71. }
  72.  
  73. // Def: dp[i][j] = min num of chars to add, from i to j
  74. // Recurrence relation:
  75. // 1. dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j]), where k in (i..j)
  76. // 2. dp[i][j] = dp[i+1][j-1], <IF in[i]:in[j] = [] or ()>
  77.  
  78. // i..j is range, k is interval
  79. for (int k = ; k < slen; k++) // NOTE: this is a bottom-up DP. We have to go from 0 to slen as interval
  80. for (int i = , j = i + k; i < slen - k; i ++, j ++)
  81. {
  82. if (i == j)
  83. {
  84. dp[i][j] = ;
  85. path[i][j] = -;
  86. continue;
  87. }
  88. bool bIsMatched = isMatched(in, i, j);
  89. if (bIsMatched) // eq 2
  90. {
  91. if (k == ) // length == 2
  92. {
  93. dp[i][j] = ;
  94. path[i][j] = -;
  95. continue;
  96. }
  97. else if (k > ) // length > 2
  98. {
  99. dp[i][j] = dp[i + ][j - ];
  100. path[i][j] = -; // we don't split matched pair
  101. // A: we still go ahead with eq1
  102. }
  103. }
  104. //else // eq1
  105. {
  106. // t is iterator of split index
  107. for (int t = i; t < j; t++)
  108. {
  109. int newVal = dp[i][t] + dp[t + ][j];
  110. if (newVal <= dp[i][j]) // Label A: we prefer splitted solution
  111. {
  112. dp[i][j] = newVal;
  113. path[i][j] = t;
  114. }
  115. }
  116. }
  117. }
  118. printPath(path, , slen - , in);
  119. } // if (slen > 0)
  120. }
  121.  
  122. int main()
  123. {
  124. char in[MAX_LEN] = { };
  125. gets(in);
  126. calc(in);
  127. printf("\n");
  128. return ;
  129. }

POJ #1141 - Brackets Sequence - TODO: POJ website issue的更多相关文章

  1. 区间DP POJ 1141 Brackets Sequence

    Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29520   Accepted: 840 ...

  2. POJ 1141 Brackets Sequence

    Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29502   Accepted: 840 ...

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

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

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

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

  5. POJ 1141 Brackets Sequence (区间DP)

    Description Let us define a regular brackets sequence in the following way: 1. Empty sequence is a r ...

  6. poj 1141 Brackets Sequence (区间dp)

    题目链接:http://poj.org/problem?id=1141 题解:求已知子串最短的括号完备的全序列 代码: #include<iostream> #include<cst ...

  7. poj 1141 Brackets Sequence(区间DP)

    题目:http://poj.org/problem?id=1141 转载:http://blog.csdn.net/lijiecsu/article/details/7589877 定义合法的括号序列 ...

  8. POJ 1141 Brackets Sequence(括号匹配二)

    题目链接:http://poj.org/problem?id=1141 题目大意:给你一串字符串,让你补全括号,要求补得括号最少,并输出补全后的结果. 解题思路: 开始想的是利用相邻子区间,即dp[i ...

  9. POJ 1141 Brackets Sequence(DP)

    题目链接 很早 很早之前就看过的一题,今天终于A了.状态转移,还算好想,输出路径有些麻烦,搞了一个标记数组的,感觉不大对,一直wa,看到别人有写直接输出的..二了,直接输出就过了.. #include ...

随机推荐

  1. wddm 部署问题解决

    在把wddm部署到一台老的服务上的 windows server 2003 上时遇到了问题,之前也在 windows server 2003 上装过,但是并没有遇到问题,估计和服务器比较老有关系. 问 ...

  2. url截取判断(实现同级列表)

    <script> var dUrl=window.location.href; var cUrl=(dUrl.substring(0, dUrl.indexOf('list_'))); v ...

  3. debug进入线程中

    今天debug调试程序时,怎么也进入不了线程中,f5直接进源码,f6直接跳过了,后来把断点打在线程的Run()方法里面,按f8(可能要多按几次)就可以了.

  4. Nginx技巧:灵活的server_name,Nginx配置一个服务器多个站点 和 一个站点多个二级域名

    http://www.cnblogs.com/buffer/archive/2011/08/17/2143514.html Nginx强大的正则表达式支持,可以使server_name的配置变得很灵活 ...

  5. CI 框架隐藏index.php-ubuntu

    和朋友在做一个小网站,用到了CI框架,之前测试都是在windows上,隐藏index.php也相对比较简单.但服务器是ubuntu系统,需要配置一下,根据网上看到的一些教程,结合自己电脑的特点,记录步 ...

  6. Sqlserver CheckPoint 在三种恢复模式中的不同表现

    准备: 日志截断在下列情况下发生: 1.执行完 BACKUP LOG 语句时.2.在每次处理检查点时(如果数据库使用的是简单恢复模式).这包括 CHECKPOINT 语句所产生的显式检查点和系统生成的 ...

  7. Queue及Stack

    Queue 她是一个接口,有多冢实现方式(LinkedList.ArrayDeque等) 类别 方法 入队 add.offer(优先级高) 出队 remove.poll 查询 element.peek ...

  8. Spring AOP 实现写事件日志功能

    什么是AOP?AOP使用场景?AOP相关概念?Spring AOP组件?如何使用Spring AOP?等等这些问题请参考博文:Spring AOP 实现原理 下面重点介绍如何写事件日志功能,把日志保存 ...

  9. API读取和处理的文件

    1.FileList对象  FileList对象是File对象的一个集合,设置multiple就可以多文件上传.2.Blob对象 Blob对象就是一个二进制原始数据对象,它提供了slice方法可以读取 ...

  10. URAL 1320 Graph Decomposition(并查集)

    1320. Graph Decomposition Time limit: 0.5 secondMemory limit: 64 MB There is a simple graph with an ...