题目链接1

题目链接2

题目大意

给出一个括号序列,添加最少的括号使序列正确

解题思路

先将问题简单化,从求序列退化为求最小添加括号数的问题

用区间dp n³解决

f[l][r]表示使第l个到r个区间正确的最小添加数

1 :当l = r时, f[l][r] = f[l+1][r-1]

2 :  在l到j中,枚举中间点k,则f[l][r] = min (f[l][r], f[l][k] + f[k+1][r])

求出了最小添加括号数后,再来思考完整的问题

用递归解决输出方案

假设有一个 从 l 到 r 的区间

这个区间的最优解有两种情况:

1:有上述第1种情况求得

2:由上述第2种情况求得

对于 1, 先输出最左边的字符,再递归中间部分,再输出最右边的字符

对于2, 用w数组记录此时最优方案的分割点k,分别递归左半边和右半边

特别情况,当l = r 则需在这个地方添加一个括号与其配对

注意

要对读入序列长度为0进行特判

不然会很惨

几个小时也调不出来qwq

完整代码加注释

(突然发现我的代码好短)

#include <bits/stdc++.h>
using namespace std;
char ch[105];
int f[105][105], w[105][105];
void out (int x) {
if (ch[x] == '(' or ch[x] == ')') cout << "()";
else cout << "[]";
}//输出与单个括号配对的函数
void print (int l, int r) {
if (l > r) return;
else if (l == r) out (l); //若l = r 则需在这个地方添加一个括号与其配对
else if (w[l][r] == 0) cout << ch[l], print (l + 1, r - 1), cout << ch[r]; //上述情况1
else print (l, w[l][r]), print (w[l][r] + 1, r); //上述情况2
} //输出方案递归函数
int main(){
scanf ("%s", ch + 1);
int len = strlen (ch + 1);
if (!len) puts(""); //当长度为0的特判
memset (f, 0x3f, sizeof (f));
for (int i = 1; i <= 101; i++) f[i][i] = 1; //长度为1的区间初值赋值为1
for (int l = 2; l <= len; l++) //区间dp
for (int i = 1; i <= len - l + 1; i++) {
int j = i + l - 1;
if ((ch[i] == '[' and ch[j] == ']') or (ch[i] == '(' and ch[j] == ')'))
if (l != 2) f[i][j] = f[i+1][j-1]; else f[i][j] = 0; //当这两个可以匹配的情况,注意:当长度为2时要特判
for (int k = i; k < j; k++)
if (f[i][j] > f[i][k] + f[k+1][j]) f[i][j] = f[i][k] + f[k+1][j], w[i][j] = k; //枚举并记录下最优方案的分割点
}
print (1, len);
return 0;
}

 

POJ1141Brackets Sequence 解题报告的更多相关文章

  1. USACO Section2.1 Sorting a Three-Valued Sequence 解题报告

    sort3解题报告 —— icedream61 博客园(转载请注明出处)---------------------------------------------------------------- ...

  2. timus 1175. Strange Sequence 解题报告

    1.题目描述: 1175. Strange Sequence Time limit: 1.0 secondMemory limit: 2 MB You have been asked to disco ...

  3. Ducci Sequence解题报告

    A Ducci sequence is a sequence of n-tuples of integers. Given an n-tuple of integers (a1, a2, ... ,  ...

  4. 【LeetCode】842. Split Array into Fibonacci Sequence 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  5. 【LeetCode】60. Permutation Sequence 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. LeetCode: Permutation Sequence 解题报告

    Permutation Sequence https://oj.leetcode.com/problems/permutation-sequence/ The set [1,2,3,…,n] cont ...

  7. USACO Section 2.1 Sorting a Three-Valued Sequence 解题报告

    题目 题目描述 给N个整数,每个整数只能是1,2,或3.现在需要对这个整数序列进行从小到大排序,问最少需要进行几次交换.N(1 <= N <= 1000) 样例输入 9 2 2 1 3 3 ...

  8. LeetCode: Longest Consecutive Sequence 解题报告

    Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...

  9. BZOJ 1367 [Baltic2004]sequence 解题报告

    BZOJ 1367 [Baltic2004]sequence Description 给定一个序列\(t_1,t_2,\dots,t_N\),求一个递增序列\(z_1<z_2<\dots& ...

随机推荐

  1. 关于DLL调试的两个工具(dependency walker和dumpbin.exe)

    Dependency Walker工具: 该工具用来查看dll依赖库和依赖库内的函数. 左上角:模块依赖树,表示该dll所依赖的其它dll文件 右上角:导入函数,即与当前根模块有关系的函数(即根模块或 ...

  2. leetcode 6 ZigZag Conversion(水题)

    就是简单的模拟一下就可以了.但是我一开始是用一个二维char数组来存的,这样在最终扫全体时会扫很多空的位置,浪费了很多时间,所以就time limit error了. 所以改进一下就用string数组 ...

  3. [原]NYOJ-无线网络覆盖-199

    大学生程序代写 /*无线网络覆盖 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 我们的乐乐同学对于网络可算得上是情有独钟,他有一个计划,那就是用无线网覆盖郑州大学. 现 ...

  4. IE input 去掉文本框的叉叉和密码输入框的眼睛图标

    ::-ms-clear, ::-ms-reveal{display: none;}

  5. DP小合集

    1.Uva1625颜色的长度 dp[i][j]表示前一个串选到第i个 后一个串选到第j个 的最小价值 记一下还有多少个没有结束即dp2 记一下每个数开始和结束的位置 #include<cstdi ...

  6. 1022 Digital Library (30)(30 分)

    A Digital Library contains millions of books, stored according to their titles, authors, key words o ...

  7. [BZOJ1396&2865]识别子串

    bzoj1396 bzoj2865 dbzoj1396 dbzoj2865 题面 XX在进行字符串研究的时候,遇到了一个十分棘手的问题. 在这个问题中,给定一个字符串\(S\),与一个整数\(K\), ...

  8. bzoj 3881: [Coci2015]Divljak AC自动机

    题目大意: http://www.lydsy.com/JudgeOnline/problem.php?id=3881 题解: 这道题我想出了三种做法,不过只有最后一种能过. 第一种: 首先我们把所有的 ...

  9. 第 七 课 go的运算符

    http://www.runoob.com/go/go-operators.html 算术运算符 关系运算符 逻辑运算符 位运算符 赋值运算符 其他运算符 返回变量存储地址: & 指针变量:  ...

  10. AD9各种布线总结

    1.常规布线:不详细说了,是个人就知道怎么弄.需要说明的是在布线过程中,可按小键盘的*键或大键盘的数字2键添加一个过孔:按L键可以切换布线层:按数字3可设定最小线宽.典型线宽.最大线宽的值进行切换. ...