区间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 ...
随机推荐
- SQL 常见出现错误(附件、保存表、脱机、自增序列号 )
一.问题如图所示: 当填了某些数据,按“保存”时出现这个问题怎么解决? 1.打开“工具”-“选项”-“Designers” , 2.选择如下去勾: 二.当附加数据库的时候出现如下错误: 在附件文件上选 ...
- git clone和download zip的区别
采用git clone的项目包含.git目录,这里面有历史版本信息 采用下载zip文件的是没有版本历史信息的.只是当前分支的最新版本 克隆指令: $ git clone git://github.co ...
- SRTE测试
网络拓扑: XRV1 ======================================================================= hostname XRV1expl ...
- 关于instanface的问题
nstanceof关键字来判断某个对象是否属于某种数据类型.报错 代码如下 package cn.lijun.demo3; import cn.lijun.demo.Person;import cn ...
- Cortex-A8/A76
Cortex-A8 关于Cortex-A8的微处理架构参考<ARM_Cortex-A8微处理器的架构和实现> 其中关于NEON有两段话摘录如下: NEON媒体引擎拥有自己的10段流水线,它 ...
- Hibernate映射文件详解(News***.hbm.xml)二
转自 http://blog.csdn.net/a9529lty/article/details/6454924 一.hibernate映射文件的作用: Hibernate映射文件是Hibernate ...
- 006.MFC_对话框_复选框_单选钮
对话框和控件复选框单选框分组框示例:三原色画图 一.建立名为Demo2的MFC工程,按照下图添加控件 并修改2个Group Box Caption属性分别为颜色.外观 修改3个Check Box Ca ...
- 第二阶段:2.商业需求文档MRD:2.MRD-目标市场分析
版本管理的变更人,属性,时间以及审核人都要严格的写清楚. MRD主要面向的是参与这个需求同级别的同时或主管,让大家更好的了解这个产品的各个方面,达成共识. 现在互联网的发展周期很短,不需要看4.5年, ...
- javaweb项目部署到tomcat之后java文件没有编译
1.选中你的项目==>选择Project 2.将Build Automatcally前的对号去掉后再Clean一下你的项目 这样就可以了,
- mysql主从之多元复制
实验环境: 192.168.132.121 master1 192.168.132.122 master2 192.168.132.123 slave 使用gtid的方式 两个主分别是19 ...