Problem UVA1626-Brackets sequence

Time Limit: 4500 mSec

Problem Description

Input

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs. The input file contains at most 100 brackets (characters ‘(’, ‘)’, ‘[’ and ‘]’) that are situated on a single line without any other characters among them.

 Output

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line. 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

()[()]

题解:这个题挺好的,区间dp,可以写成记忆化搜索,容易忽略的地方是如果区间两边的括号匹配,那么可以用中间的部分转移,然后就是普通的分成左区间和右区间进行转移,这个题比较有价值的地方在于打印解的过程,应该学习一下,就是根据结果,逆推回去,这个方便在不用中间记录转移路径,代价就是时间上会有额外的开销,不过一般不至于因此就TLE,因为解一般很少。输入输出有坑,需要用fgets,并且注意fgets会把最后的'\n'读进来,因此真实串的长度需要-1.

  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int maxn = + ;
  6.  
  7. int iCase, dp[maxn][maxn];
  8. char bra[maxn];
  9. bool vis[maxn][maxn];
  10.  
  11. bool match(char a, char b) {
  12. if ((a == '(' && b == ')') || (a == '[' && b == ']')) return true;
  13. return false;
  14. }
  15.  
  16. int DP(int l, int r) {
  17. if (dp[l][r] >= ) return dp[l][r];
  18. if (l == r) return dp[l][r] = ;
  19. if (l > r) return dp[l][r] = ;
  20.  
  21. int &ans = dp[l][r];
  22. ans = r - l + ;
  23. if (match(bra[l], bra[r])) {
  24. ans = min(ans, DP(l + , r - ));
  25. }
  26. for (int k = l; k < r; k++) {
  27. ans = min(ans, DP(l, k) + DP(k + , r));
  28. }
  29. return ans;
  30. }
  31.  
  32. void ans_print(int l, int r) {
  33. if (l > r) return;
  34. if (l == r) {
  35. if (bra[l] == '(' || bra[l] == ')') {
  36. printf("()");
  37. }
  38. else {
  39. printf("[]");
  40. }
  41. return;
  42. }
  43.  
  44. int &ans = dp[l][r];
  45. if (match(bra[l], bra[r]) && ans == dp[l + ][r - ]) {
  46. printf("%c", bra[l]);
  47. ans_print(l + , r - );
  48. printf("%c", bra[r]);
  49. return;
  50. }
  51. else {
  52. for (int k = l; k < r; k++) {
  53. if (ans == dp[l][k] + dp[k + ][r]) {
  54. ans_print(l, k);
  55. ans_print(k + , r);
  56. return;
  57. }
  58. }
  59. }
  60. }
  61.  
  62. int main()
  63. {
  64. //freopen("input.txt", "r", stdin);
  65. scanf("%d\n", &iCase);
  66. while (iCase--) {
  67. fgets(bra, maxn, stdin);
  68. memset(dp, -, sizeof(dp));
  69. int len = strlen(bra);
  70. int ans = DP(, len - );
  71. ans_print(, len - );
  72. printf("\n");
  73. if (iCase) printf("\n");
  74. fgets(bra, maxn, stdin);
  75. }
  76. return ;
  77. }

UVA1626-Brackets sequence(动态规划基础)的更多相关文章

  1. UVa 1626 Brackets sequence (动态规划)

    题意:用最少的括号将给定的字符串匹配,输出最优解.可能有空行. 思路:dp. dp[i][j]表示将区间i,j之间的字符串匹配需要的最少括号数,那么 如果区间左边是(或[,表示可以和右边的字符串匹配, ...

  2. UVA1626 - Brackets sequence(区间DP--括号匹配+递归打印)

    题目描写叙述: 定义合法的括号序列例如以下: 1 空序列是一个合法的序列 2 假设S是合法的序列.则(S)和[S]也是合法的序列 3 假设A和B是合法的序列.则AB也是合法的序列 比如:以下的都是合法 ...

  3. UVA-1626 Brackets sequence (简单区间DP)

    题目大意:给一个有小括号和中括号组成的序列,满足题中的三个条件时,是合法的.不满足时是不合法的,问将一个不合法的序列最少添加几个括号可以使之变成合法的.输出最短合法序列. 题目分析:这是<入门经 ...

  4. uva1626 Brackets sequence

    题目大意: 给一个有小括号和中括号组成的序列,满足题中的三个条件时,是合法的.不满足时是不合法的,问将一个不合法的序列最少添加几个括号可以使之变成合法的.输出最短合法序列. /* 比较坑的一道题,wa ...

  5. 1626 - Brackets sequence——[动态规划]

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

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

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

  7. poj 2955 Brackets (区间dp基础题)

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

  8. POJ 题目1141 Brackets Sequence(区间DP记录路径)

    Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 27793   Accepted: 788 ...

  9. POJ 1141 Brackets Sequence

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

随机推荐

  1. Java设计模式 - 单例模式详解(扩展)

    单例模式引发相关整理 如何破坏单例模式 示例: /** * 如果破坏单例模式 * * @author sunyang * @date 2018/11/13 20:14 */ public class ...

  2. viewer.js 视图预览demo

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  3. JS单体内置对象之Math常用方法(min,max,ceil,floor,round,random等)

    1.min()和max()方法 Math.min()用于确定一组数值中的最小值.Math.max()用于确定一组数值中的最大值. alert(Math.min(2,4,3,6,3,8,0,1,3)); ...

  4. 详解Vue.js 技术

    本文主要从8个章节详解vue技术揭秘,小编觉得挺有用的,分享给大家. 为了把 Vue.js 的源码讲明白,课程设计成由浅入深,分为核心.编译.扩展.生态四个方面去讲,并拆成了八个章节,如下: 准备工作 ...

  5. 浅谈pc和移动端的响应式

    身为一个前端攻城狮,是不是经常遇到各种各样的响应式问题?下面我们来说一下: 1.响应式跟自适应有什么区别? 有些人可能还不知道响应式跟自适应的区别,甚至认为他们是同一个东西,其实不是的. 自适应是最早 ...

  6. Spring学习之旅(四)Spring工作原理再探

    上篇博文对Spring的工作原理做了个大概的介绍,想看的同学请出门左转.今天详细说几点. (一)Spring IoC容器及其实例化与使用 Spring IoC容器负责Bean的实例化.配置和组装工作有 ...

  7. JMeter 后置处理器之正则表达式提取器详解

    后置处理器之正则表达式提取器详解   by:授客 QQ:1033553122 1. 添加正则表达式提取器 右键线程组->添加->后置处理器->正则表达式提取器 2. 提取器配置介绍 ...

  8. 「Android」GreenDao

    译文 版本:greenDAO 3.2.2 官网:http://greenrobot.org/greendao/ GitHub:https://github.com/greenrobot/greenDA ...

  9. FreeSWITCH1.6安装教程Centos

    介绍 FreeSWITCH是个网络电话转化器,相当与网络转换器,不过这个是用于IP电话,构建公司的电话通讯系统需要用该技术,因为按照官方文档无法成功安装. 本文目的:FreeSWITCH安装步骤 官方 ...

  10. 【爬虫】在使用xpath时,排除指定标签

    xpath排除某个节点 主要时应用name()这个函数获取便签名 res = html.xpath("//*[name(.)!='style']")