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. Python-10行代码实现3个数据可视化

    阅读本文约“1分钟” 最近将Python作为第二编程语言,进行了了解与学习,可以说它的包是很强大的.这次的demo仅仅不到10行代码就可以实现三个数据可视化的小实例. 我们将要使用到matplotli ...

  2. SpringBoot登录登出切面开发

    阅读本文约“2.5分钟” 本文开发环境是SpringBoot2.X版本. 对于系统而言(这里多指管理系统或部分具备登录登出功能的系统),登录登出是一个类权限验证的过程,现在一般是以token进行校验, ...

  3. Reinforcement Learning: An Introduction读书笔记(4)--动态规划

     > 目  录 <  Dynamic programming Policy Evaluation (Prediction) Policy Improvement Policy Iterat ...

  4. mapper代理开发步骤

    1:先写Mapper接口,UserMapper.java 2:然后遵循4条开发规范,写映射文件,UserMapper.xml 3:将映射文件,UserMapper.xml加入到SqlMapConfig ...

  5. 远程连接centos7 上的mysql报(ERROR 2003 (HY000): Can't connect to MySQL server on '168.x.x.x' (10060) )

    1.MySQL端口 因为上一篇文章我就已经给MySQL新建了一新用户,且赋予了远程连接数据库的所有权限(GRANT ALL PRIVILEGES ON *.* TO 'newuser' @ '%' I ...

  6. 初学HTML-1

    HTML:Hyper Text Markup Language的缩写    超文本标记语言,用来描述文本的语义,这些文本———超文本,也叫标签. 基本格式: <html> <head ...

  7. 二进制安装 kubernetes 1.12(三) - 部署 Master 节点组件

    在Master节点部署组件 在部署Kubernetes之前一定要确保etcd.flannel.docker是正常工作的,否则先解决问题再继续. 创建 CA 证书 mkdir -p /iba/master ...

  8. 学习 Docker 操作系统版本选择

    近来有时间一直在捣鼓 Docker.因为服务器选择的是 CentOS 版本,所以实验的环境选择的一直是 CentOS.如果是个人玩 Docker,优先选择 ubuntu.如果需要选择 CentOS 的 ...

  9. 自动排版工具——XML自动排版生成工具

    ——支持全球化/多语言/符合W3C标准的XML自动排版工具 Boxth XML/XSL Formatter是专为XML数据或其他结构化数据源自动输出排版文件(如: PDF等)而设计的集数据格式化.版式 ...

  10. iOS----------The app's Info.plist must contain an NSPhotoLibraryUsageDescription key

    This app has crashed because it attempted to access privacy-sensitive data without a usage descripti ...