Let us define a regular brackets sequence in the following way:

1. Empty sequence is a regular sequence.

2. If S is a regular sequence, then (S) and [S] are both regular sequences.

3. If A and B are regular sequences, then AB is a regular sequence.

For example, all of the following sequences of characters are regular brackets sequences:

(), [], (()), ([]), ()[], ()[()]

And all of the following character sequences are not:

(, [, ), )(, ([)], ([(]

Some sequence of characters '(', ')', '[', and ']' is given.
You are to find the shortest possible regular brackets sequence, that
contains the given character sequence as a subsequence. Here, a string
a1 a2 ... an is called a subsequence of the string b1 b2 ... bm, if
there exist such indices 1 = i1 < i2 < ... < in = m, that aj =
bij for all 1 = j = n.

Input

The input file contains at most 100 brackets (characters '(',
')', '[' and ']') that are situated on a single line without any other
characters among them.

Output

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

([(]

Sample Output

()[()]

题意:给你一个不完整的括号序列,要求你添加最少的括号,使其可以构成一个左右互相匹配的完整的序列。
思路分析:开始想了个贪心,但是是不对
    正解是区间 dp,dp[i][j]表示区间 i - j 内添加最小数量的括号可以使其匹配,然后在转移的过程中判断一下当前区间的括号是否可以匹配上,如果可以此时的值则等于其内部区间的值,否则则在加一层 for去判断
    输出的地方采用递归的方式去输出,比较经典的一个题
代码示例:
char s[105];
int dp[105][105], path[105][105]; void print(int l, int r){
if (l > r) return; if (l == r){
if (s[l] == '(' || s[l] == ')') printf("()");
if (s[l] == '[' || s[l] == ']') printf("[]");
return;
} if (path[l][r] == -1){
putchar(s[l]);
print(l+1, r-1);
putchar(s[r]);
}
else{
print(l, path[l][r]);
print(path[l][r]+1, r);
}
} int main() {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout); while(gets(s+1) != NULL){
int n = strlen(s+1);
memset(dp, 0, sizeof(dp));
for(int i = 1; i <= n; i++) dp[i][i] = 1; for(int len = 2; len <= n; len++){ // 区间长度
for(int i = 1; i <= n; i++){
int j = i+len-1;
if (j > n) break;
dp[i][j] = inf;
if ((s[i]=='('&&s[j]==')') || (s[i]=='['&&s[j]==']')){
dp[i][j] = dp[i+1][j-1];
path[i][j] = -1;
}
for(int k = i; k < j; k++){
if (dp[i][k]+dp[k+1][j] < dp[i][j]){
dp[i][j] = dp[i][k]+dp[k+1][j];
path[i][j] = k;
}
}
// printf("+++ %d %d %d \n", i, j, dp[i][j]);
}
}
print(1, n);
//printf("%d\n", dp[1][n]);
printf("\n");
}
return 0;
}
/*
([(]
*/

区间dp - 括号匹配并输出方案的更多相关文章

  1. 区间dp 括号匹配问题

    这道题目能用区间dp来解决,是因为一个大区间的括号匹配数是可以由小区间最优化选取得到(也就是满足最优子结构) 然后构造dp 既然是区间类型的dp 一般用二维 我们定义dp[i][j] 表示i~j这个区 ...

  2. poj 2955 Brackets (区间dp 括号匹配)

    Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...

  3. 区间dp括号匹配

    POJ2955 匹配则加一,不需要初始化 //#include<bits/stdc++.h> #include<iostream> #include<cstdio> ...

  4. Codeforces 5C Longest Regular Bracket Sequence(DP+括号匹配)

    题目链接:http://codeforces.com/problemset/problem/5/C 题目大意:给出一串字符串只有'('和')',求出符合括号匹配规则的最大字串长度及该长度的字串出现的次 ...

  5. poj2955括号匹配 区间DP

    Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5424   Accepted: 2909 Descript ...

  6. 括号匹配 区间DP (经典)

    描述给你一个字符串,里面只包含"(",")","[","]"四种符号,请问你需要至少添加多少个括号才能使这些括号匹配起来 ...

  7. poj 2955 括号匹配 区间dp

    Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6033   Accepted: 3220 Descript ...

  8. [NYIST15]括号匹配(二)(区间dp)

    题目链接:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=15 经典区间dp,首先枚举区间的大小和该区间的左边界,这时右边界也可计算出来.首先初 ...

  9. NYOJ 题目15 括号匹配(二)(区间DP)

    点我看题目 题意 : 中文题不详述. 思路 : 本来以为只是个小模拟,没想到是个区间DP,还是对DP不了解. DP[i][j]代表着从字符串 i 位置到 j 位置需要的最小括号匹配. 所以初始化的DP ...

随机推荐

  1. Educational Codeforces Round 54 (Rated for Div. 2) D Edge Deletion (SPFA + bfs)

    题目大意:给定你一个包含n个点m条边的无向图,现在最多在图中保留k条边,问怎么删除多的边,使得图中良好的节点数最多,求出保留在图中的边的数量和编号. 良好的节点定义为:删除某条边后该点到点1的最短距离 ...

  2. 2019-8-31-How-to-fix-nuget-Unrecognized-license-type-MIT-when-pack

    title author date CreateTime categories How to fix nuget Unrecognized license type MIT when pack lin ...

  3. es6笔记 day2---字符串模板及字符串新增

    字符串连接案例 注意:引号变了,为键盘数字1旁边的飘花键 以前的老写法是在字符串中加入“+”号,给几个字符串给串起来,那种写法是要死人的. 现在只需加上一对``即可将字符串连接起来 --------- ...

  4. 【37.48%】【hdu 2587】How far away ?(3篇文章,3种做法,LCA之树上倍增)

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s) ...

  5. error:cannot load file (code:5555h);bootauto.ini

    最近发现有的网友在使用Ghost XP盘安装系统的时候,选择一键ghost到C盘出现下面的错误: error:cannot load file (code:5555h);bootauto.ini(或b ...

  6. IPv4数据报格式及其语义

    一.IP数据报的格式如下图所示 版本 首部长度 服务类型 数据报长度 16比特标识 标志 13比特片偏移 寿命 上层协议 首部检验和 32比特源IP地址 32比特目的IP地址 选项(如果有的话) 数据 ...

  7. 关于C#异步编程你应该了解的几点建议

    前段时间写了一篇关于C#异步编程入门的文章,你可以点击<C#异步编程入门看这篇就够了>查看.这篇文章我们来讨论下关于C#异步编程几个不成文的建议,希望对你写出高性能的异步编程代码有所帮助. ...

  8. 0029 定位:position(相对、绝对、固定、绝对定位盒子居中、z-index、绝对定位改变display属性)

    目标 理解 能说出为什么要用定位 能说出定位的4种分类 能说出四种定位的各自特点 能说出我们为什么常用子绝父相布局 应用 能写出淘宝轮播图布局 1. CSS 布局的三种机制 网页布局的核心 -- 就是 ...

  9. 如何修改eclipse中Dynamic web module的 version

    我们直接在eclipse中修改Dynamic Web Module的话会报错,改不了的 所以我们可以找到项目文件中的.setting文件下的org.eclipse.wst.common.project ...

  10. Docker常用命令速查手册(华贵铂金版)

    原创声明:作者:Arnold.zhao  博客园地址:https://www.cnblogs.com/zh94 Docker常用命令速查手册 搜索仓库镜像 docker search nginx 获取 ...