括号匹配问题(区间dp)】的更多相关文章

题意:给你一些括号,问匹配规则成立的括号的个数. 思路:这题lrj的黑书上有,不过他求的是添加最少的括号数,是的这些括号的匹配全部成立. 我想了下,其实这两个问题是一样的,我们可以先求出括号要匹配的最少数量,那么设原来括号的数量为l , 添加了l' . 那么其实原来括号匹配成功的括号数就是((l + l') / 2 - l') * 2. #define N 105 char a[N] ; int dp[N][N] ; int f[N][N] ; int check(int i ,int j) {…
题目大意: 给一个由,(,),[,]组成的字符串,其中(),[]可以匹配,求最大匹配数 题解:区间dp: dp[i][j]表示区间 [i,j]中的最大匹配数 初始状态 dp[i][i+1]=(i,i+1可以匹配)?2:0 状态转移见代码 代码: #include <iostream> #include <stdio.h> #include<string.h> #include<algorithm> #include<string> #includ…
题意:给一段左右小.中括号串,求出这一串中最多有多少匹配的括号. 解法:此问题具有最优子结构,dp[i][j]表示i~j中最多匹配的括号,显然如果i,j是匹配的,那么dp[i][j] = dp[i+1][j-1]+2; 否则我们可以分区间取最值.dp[i][j] = max(dp[i][j],dp[i][k]+dp[k+1][j]); k在i,j之间. 代码: #include <iostream> #include <cstring> #include <algorithm…
Brackets We give the following inductive definition of a “regular brackets” sequence: the empty sequence is a regular brackets sequence, if s is a regular brackets sequence, then (s) and [s] are regular brackets sequences, and if a and b are regular…
括号序列(区间dp) 输入一个长度不超过100的,由"(",")","[",")"组成的序列,请添加尽量少的括号,得到一个规则的括号序列.如有多解,输出任意一个序列即可. 括号序列是这样定义而成的: 空序列是括号序列 如果S是括号序列,那么(S)和[S]也是正规括号序列 如果A和B都是正规括号序列,那么AB也是正规括号序列. 所以,只要一个括号序列不是空序列,我们一定可以把它从两端剥开,或者把它划分成两个小括号序列.设\(f[…
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5193 解决:2248 题目描述: 在某个字符串(长度不超过100)中有左括号.右括号和大小写字母:规定(与常见的算数式子一样)任何一个左括号都从内到外与在它右边且距离最近的右括号匹配.写一个程序,找到无法匹配的左括号和右括号,输出原来字符串,并在下一行标出不能匹配的括号.不能匹配的左括号用"$"标注,不能匹配的右括号用"?"标注. 输入: 输入包括多组数据,每组数据一行,包含一个字符串,只包含左右括号和大…
pid=15">http://acm.nyist.net/JudgeOnline/problem.php? pid=15 dp[i][j]表示从i到j至少须要加入多少个括号才干满足匹配条件. 初始化: if(i == j) dp[i][j] = 1; else dp[i][j] = INF; 状态转移: 当i < j时; if(match(str[i], str[j])) dp[i][j] = min(dp[i][j], d[i + 1][j - 1]); 然后切割区间, 找最优切割…
51 Nod 1021 石子归并 模板题,敲就完事了,注意一下这种状态转移方程有个四边形的优化(时间) #include <cstdio> #include <iostream> #include <cstring> using namespace std; int n; ; int f[maxn][maxn], s[maxn][maxn], a[maxn], sum[maxn]; void solve_sim() { memset(f, 0x3f, sizeof(f)…
Description We give the following inductive definition of a “regular brackets” sequence: the empty sequence is a regular brackets sequence, if s is a regular brackets sequence, then (s) and [s] are regular brackets sequences, and if a and b are regul…
Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5424   Accepted: 2909 Description We give the following inductive definition of a “regular brackets” sequence: the empty sequence is a regular brackets sequence, if s is a regular…