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.

 #include <bits/stdc++.h>

 using namespace std;

 const int maxn =  + ;

 int iCase, dp[maxn][maxn];
char bra[maxn];
bool vis[maxn][maxn]; bool match(char a, char b) {
if ((a == '(' && b == ')') || (a == '[' && b == ']')) return true;
return false;
} int DP(int l, int r) {
if (dp[l][r] >= ) return dp[l][r];
if (l == r) return dp[l][r] = ;
if (l > r) return dp[l][r] = ; int &ans = dp[l][r];
ans = r - l + ;
if (match(bra[l], bra[r])) {
ans = min(ans, DP(l + , r - ));
}
for (int k = l; k < r; k++) {
ans = min(ans, DP(l, k) + DP(k + , r));
}
return ans;
} void ans_print(int l, int r) {
if (l > r) return;
if (l == r) {
if (bra[l] == '(' || bra[l] == ')') {
printf("()");
}
else {
printf("[]");
}
return;
} int &ans = dp[l][r];
if (match(bra[l], bra[r]) && ans == dp[l + ][r - ]) {
printf("%c", bra[l]);
ans_print(l + , r - );
printf("%c", bra[r]);
return;
}
else {
for (int k = l; k < r; k++) {
if (ans == dp[l][k] + dp[k + ][r]) {
ans_print(l, k);
ans_print(k + , r);
return;
}
}
}
} int main()
{
//freopen("input.txt", "r", stdin);
scanf("%d\n", &iCase);
while (iCase--) {
fgets(bra, maxn, stdin);
memset(dp, -, sizeof(dp));
int len = strlen(bra);
int ans = DP(, len - );
ans_print(, len - );
printf("\n");
if (iCase) printf("\n");
fgets(bra, maxn, stdin);
}
return ;
}

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. 今天给大家补充一下 background 用法

    补充一个知识点 1,浏览器默认字体大小是font-size:16px:谷歌最小字体是10px,其他浏览器是12px 2. 选择器 通配符选择器     *   表示 3.background  背景 ...

  2. JS预解析机制

    JS的预解析过程: 1,预解析 2,再逐行解读代码, 实例: ---------------------------- <script>        var name="xm& ...

  3. es6 语法 (let 和const)

    一.let 和const 1.let 只在自己声明的块作用域中有效: function test(){ let a = 'a'; var b = 'b'; for(let i =1;i<3;i+ ...

  4. 51NOD 1185 威佐夫游戏 V2(威佐夫博弈)

    1185 威佐夫游戏 V2  基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 有2堆石子.A B两个人轮流拿,A先拿.每次可以从一堆中取任意个或从2堆中取 ...

  5. Python 捕捉traceback异常栈信息

    捕捉traceback异常栈信息   by:授客 QQ:1033553122 相关函数简介 sys.exc_info() 返回包含3个元素(type, value, traceback)的元组,提供关 ...

  6. 微信小程序开发之初探

    本文是以一个简单的小例子,来简要讲解微信小程序开发步骤,希望促进学习分享. 概念 微信小程序,简称小程序,缩写xcx,英文mini program.是一种不需要下载安装即可使用的应用,它实现了应用“触 ...

  7. WPF窗体程序入口 自定义窗体启动页面

    一张图体现一切:

  8. DAY4(python)打印字符串以及增删改查

    用while循环打印字符串 #if i in s: # print ( i ) s='nanfjkhndaol' index = 0 while 1 : print (s[index]) index+ ...

  9. springboot 学习之路 5(打成war包部署tomcat)

    目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...

  10. IntelliJ IDEA安装后需要必须做的一件事

    把Alt+斜杆 删除 Ctrl+空格修改成 Alt+斜杆 Ctrl+空格用过输入法的人都应该知道为什么要做上面一件事