区间dp - 括号匹配并输出方案
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
')', '[' and ']') that are situated on a single line without any other
characters among them.
Output
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 - 括号匹配并输出方案的更多相关文章
- 区间dp 括号匹配问题
这道题目能用区间dp来解决,是因为一个大区间的括号匹配数是可以由小区间最优化选取得到(也就是满足最优子结构) 然后构造dp 既然是区间类型的dp 一般用二维 我们定义dp[i][j] 表示i~j这个区 ...
- poj 2955 Brackets (区间dp 括号匹配)
Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...
- 区间dp括号匹配
POJ2955 匹配则加一,不需要初始化 //#include<bits/stdc++.h> #include<iostream> #include<cstdio> ...
- Codeforces 5C Longest Regular Bracket Sequence(DP+括号匹配)
题目链接:http://codeforces.com/problemset/problem/5/C 题目大意:给出一串字符串只有'('和')',求出符合括号匹配规则的最大字串长度及该长度的字串出现的次 ...
- poj2955括号匹配 区间DP
Brackets Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5424 Accepted: 2909 Descript ...
- 括号匹配 区间DP (经典)
描述给你一个字符串,里面只包含"(",")","[","]"四种符号,请问你需要至少添加多少个括号才能使这些括号匹配起来 ...
- poj 2955 括号匹配 区间dp
Brackets Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6033 Accepted: 3220 Descript ...
- [NYIST15]括号匹配(二)(区间dp)
题目链接:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=15 经典区间dp,首先枚举区间的大小和该区间的左边界,这时右边界也可计算出来.首先初 ...
- NYOJ 题目15 括号匹配(二)(区间DP)
点我看题目 题意 : 中文题不详述. 思路 : 本来以为只是个小模拟,没想到是个区间DP,还是对DP不了解. DP[i][j]代表着从字符串 i 位置到 j 位置需要的最小括号匹配. 所以初始化的DP ...
随机推荐
- css3图片展示方式
<view class='img_block' id='mjltest'> <view class='text_view'> <view class='{{cell_cl ...
- vs code 如何修改默认主题的注释颜色
平时喜欢将注释的颜色调成绿色,既不刺眼,也比较醒目,在大型项目中,能很容易的根据注释找道想要的部分:但是,每次修改完使用一段时间后,当vs code 自动更新了,又变成默认的颜色了,为了方便每次快速修 ...
- centos7中安装R之前yum依赖的包
#!/bin/bash echo "#########################开始安装依赖环境#####################" yum -y install g ...
- Java中try catch finally执行
直接上代码实例: public static void main(String[] args) { System.out.println(test1()); } static int test1 ...
- POJ 2253 Frogger(SPFA运用)
Description Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Fro ...
- linux 一个使用整页的 scull: scullp
为了真实地测试页分配, 我们已随其他例子代码发布了 scullp 模块. 它是一个简化的 scull, 就像前面介绍过的 scullc. scullp 分配的内存量子是整页或者页集合: scullp_ ...
- 2019前端学习路线心得-黑马程序员pink老师
在规划之前先给大家分享几点心得哈: 1. 学习,特别是在线学习,是非常辛苦的事情,为了少走弯路, 所以一定要系统学习,多借鉴与前辈们总结出来的经验. 2. 不要相信任何说 一周掌握 css, 一周学完 ...
- FreeNOS学习1——系统安装和使用
官网安装教程:http://www.freenos.org/doxygen/index.html 整体思路:在Ubuntu操作系统下,安装qemu虚拟机,然后用虚拟机运行FreeNOS的镜像.以下是详 ...
- C语言图形界面常用函数集锦
(以下函数均应在图形方式初始之后使用(initgraph(a,b)),在win-tc中使用BGI图形程序模板时,其中已经定义有一个initgr函数,在main函数中应在执行initgr函数之后再使用这 ...
- Excel基本功能
公式基础: 比较运算符的种类 flase对应0 而ture对应1 连接运算 利用之前提到的ture就是1 乘以100 注意用括号区分优先级 函数应用基础: 系统已经列好这几个常用的函数 右键单击状态栏 ...