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. Java 控制类的引用类型,合理使用内存

    Java提供了 java.lang.ref包,该包下的类均与垃圾回收机制相关 先介绍Java对象的集中引用类型 1.强引用 强引用是最常见的,创建对象就是强引用,如 String a = new St ...

  2. 10个最佳Node.js企业应用案例:从Uber到LinkedIn

    译者按: Node.js 8已经发布了,NPM模块每周下载量早已超过10亿,从Uber到LinkedIn都在使用Node.js,谁说JavaScript不能写后台? - 原文: 10 best Nod ...

  3. mapper接口方法参数

    mapper接口中的方法只有一个参数,是不影响程序员开发的可以将参数指定为 pojo类型 或 map

  4. SQL查询,关联查询的区别 (LEFT JOIN 、LEFT OUTER JOIN、INNER JOIN)

    ), f2 ) ) ), f2 ) ) ------------------------------------------------ ','a1') ','a2') ','a3') ','a4') ...

  5. input file图片上传

    <div class="div-title"> <h5>图片上传</h5> <div class="photo-box" ...

  6. layui 弹窗的iframe 父子界面相互传值

    1.父界面向子界面传值 [1].父界面打开子界面: function show_layer(){ layer.open({ type: 2, area: [w+'px', h +'px'], fix: ...

  7. 安装docker17.06.0版本报错和解决方法

    本人在自己电脑的虚拟机里安装docker ce 17.06.0版本的时候报如下错误: [root@manager2 yum.repos.d]# yum install docker-ce-17.06. ...

  8. 嵌套RecyclerView左右滑动替代自定义view

    以前的左右滑动效果采用自定义scrollview或者linearlayout来实现,recyclerview可以很好的做这个功能,一般的需求就是要么一个独立的左右滑动效果,要么在一个列表里的中间部分一 ...

  9. Android Studio手动打包

    项目写完了,现在需要把应用上传到市场上面,那么怎么把项目打包成apk?(Android的可安装文件). 1. 创建签名文件 2. 填写好签名参数 3. 生成APK 注意:签名的密码和密匙的密码注意保管 ...

  10. windows网络编程中文 笔记(一)

    OSI网络模型 OSI(Open System Interconnection)开放系统互联 第七层 应用层 为用户提供相应的界面,以便使用提供的连网功能 第六层 表示层 完成数据的格式化 第五层 会 ...