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. 一个真实的Async/Await示例

    译者按: 通过真实的代码示例感受Async/Await的力量. 原文: Async/await - A thorough example 译者: Fundebug 为了保证可读性,本文采用意译而非直译 ...

  2. angular 用拦截器统一处理http请求和响应 比如加token

    想使用angularjs里的htpp向后台发送请求,现在有个用户唯一识别的token想要放到headers里面去,也就是{headres:{'token':1}} index.html里引入以下js: ...

  3. 2018-12-16 VS Code英汉词典进化效果演示: 翻译文件所有命名

    续VS Code英汉词典插件v0.0.7-尝试词性搭配, 下一个功能打算实现文件的批量命名翻译: 批量代码汉化工具 · Issue #86 · program-in-chinese/overview ...

  4. spring boot maven打包可运行jar包

    普通打包之后在程序目录运行,或者编写bat运行时会提示“没有主清单属性”,这是因为并没有找到main()方法,需要我们指明告诉java程序 我bat中的代码 @echo off title mytit ...

  5. 如何用ABP框架快速完成项目 - 自动化测试 - 前端angular e2e protractor

    要想快速完成一个项目, 自动化是很关键很有用的一块. 自动化测试比人工测试快很多. 特别是在回归测试中. 实践证明, 虽然投入了时间在写自动化测试代码上, 但是在回归测试中节省了大量的时间,同时及时发 ...

  6. IDEA基于Maven Struts2搭建配置及示例

    1.web.xml加载struts框架即过滤器,要注意struts版本不同过滤器配置也不同. <!DOCTYPE web-app PUBLIC "-//Sun Microsystems ...

  7. 【Spring源码解读】bean标签中的属性

    说明 今天在阅读Spring源码的时候,发现在加载xml中的bean时,解析了很多标签,其中有常用的如:scope.autowire.lazy-init.init-method.destroy-met ...

  8. Python绘图工具Plotly的简单使用

    1.Plotly被称为史上最好的绘图工具之一,为了更好的展示金融数据的复杂性. Plotly的官方网站为:https://plot.ly/ python量化的关键是金融数据可视化,无论是传统的K线图, ...

  9. 淘宝开放平台使用WebClient,WebRequest访问时的错误提示导致麻烦

    淘宝开放平台(TOP)提供OAuth2.0支持 通过C#的WebClient/WebRequest直接访问时会提示grant type is empty,这是一个非常恼人的错误,你会发现即使传了这个参 ...

  10. 【redis专题(10)】KEY设计原则与技巧

    对比着关系型数据库,我们对redis key的设计一般有以下两种格式: 表名:主键名:主键值:列名 表名:主键值:列名 在所有主键名都是id的情况下(其实我个人不喜欢这种情况,比如user表,它的主键 ...