题目链接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. uoj279温暖会指引我们前行

    暖气来啦~ 动态树维护最大生成树裸题 #include<iostream> #include<cstdio> #include<cstdlib> #include& ...

  2. tomcat可以访问默认页面,但是无法访问webapp下的指定项目

     tomcat可以访问默认页面,但是无法访问webapp下的指定项目 1.注意下安装tomcat时的默认端口,8005,8009,8080,我这边没有修改,根据需要自行修改,确保tomcat可以启动 ...

  3. 【LeetCode】010. Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  4. centos 6.7 (UDEV,、etc/hosts)安装 RAC 11.2.0.4 报错处理

    环境说明: ​db 11.2.0.4        os: centos 6.7    UDEV管理ASM      没有配置DNS服务器,采用/etc/hosts​报错: ​1.    CVU(Cl ...

  5. Python:struct模块的pack、unpack

    mport struct pack.unpack.pack_into.unpack_from 1 # ref: http://blog.csdn<a href="http://lib. ...

  6. PG peered实验

    标签(空格分隔): ceph,ceph实验,pg 1. 创建一个文件,并把该文件作为对象放到集群中: [root@node1 ~]# echo "this is test! " & ...

  7. Webpack打包之后[-webkit-box-orient: vertical]样式丢失

    背景:项目是用的vue全家桶套餐 今天在工作中遇到一个问题,需求是要求文字只能显示3行,超过3行则隐藏且显示 '...', 于是我加了如下样式在标签里面: display: -webkit-box;- ...

  8. java基础知识(3)----面向对象

    三:面向对象:特点: 1:将复杂的事情简单化.2:面向对象将以前的过程中的执行者,变成了指挥者.3:面向对象这种思想是符合现在人们思考习惯的一种思想. 过程和对象在我们的程序中是如何体现的呢?过程其实 ...

  9. JSP相关背景

    -----------------siwuxie095                             Sun Microsystems             SUN 即 Stanford ...

  10. DBVisualizer Pro for mac

    公司使用的是DB2数据库,支持DB2的数据库客户端常用的有DBeaver和DBVisualizer.DBeaver是免费的,但本人电脑安装后,启动一直报错,问题一直没解决就放弃了.改用DBVisual ...