poj 2955 Brackets (区间dp 括号匹配)】的更多相关文章

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…
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 brackets…
传送门 https://www.cnblogs.com/violet-acmer/p/9852294.html 题意: 给你一个只由 '(' , ')' , '[' , ']' 组成的字符串s[ ],求最大匹配? 题解: 定义dp[ i ][ j ] : 从第i个字符到第j个字符的最大匹配. 步骤: (1) : 如果s[ i ] 与 s[ j ]匹配,那么dp[ i ][ j ] =  2+dp[ i+1 ][ j-1 ];反之,dp[ i ][ j ] = 0; (2) : 接下来,从 i 到…
Brackets My Tags (Edit) Source : Stanford ACM Programming Contest 2004 Time limit : 1 sec Memory limit : 32 M Submitted : 188, Accepted : 113 5.1 Description We give the following inductive definition of a "regular brackets" sequence: • the empt…
http://blog.csdn.net/libin56842/article/details/9673239 http://www.cnblogs.com/ACMan/archive/2012/08/09/2630497.html http://blog.csdn.net/chaiyuan414/article/details/5448699 #include <iostream> #include <string> #include <cstring> #inclu…
dp[i][j]代表i->j区间内最多的合法括号数 if(s[i]=='('&&s[j]==')'||s[i]=='['&&s[j]==']') dp[i][j]=dp[i+1][j-1]+2; dp[i][j]=max{dp[i][k]+dp[k+1][j]}; 注意要对于区间的最值合并 ac代码: #include<cstdio> #include<cstring> #include<algorithm> using names…
题目链接 #include <iostream> #include <cstdio> #include <cstring> #include <vector> #include <cmath> #include <algorithm> using namespace std; ][]; ]; int judge(int x,int y) { if(str[x] == '['&&str[y] == ']') ; else…
POJ 2995 Brackets 区间DP 题意 大意:给你一个字符串,询问这个字符串满足要求的有多少,()和[]都是一个匹配.需要注意的是这里的匹配规则. 解题思路 区间DP,开始自己没想到是区间DP,以为就是用栈进行模拟呢,可是发现就是不大对,后来想到是不是使用DP,但是开始的时候自己没有推出递推关系,后来实在想不出来看的题解,才知道是区间DP,仔细一想确实是啊. 下面就是状态转移方程: \[ \begin{cases}dp[i][j] &=& dp[i+1][j-1]+if(str…
这道题目能用区间dp来解决,是因为一个大区间的括号匹配数是可以由小区间最优化选取得到(也就是满足最优子结构) 然后构造dp 既然是区间类型的dp 一般用二维 我们定义dp[i][j] 表示i~j这个区间需要添加括号的数量 那么状态怎么转移呢? 第一种情况:对于i指向的括号 如果i+1 ~ j里面不存在与之匹配的括号 那么dp[i][j] =dp[i+1][j]+1; 第二种情况:对于i指向的括号 如果i+1~ j 里面存在与之匹配的括号下标我们记作k 那么在i+1 ~ j 中我们枚举所有的k d…
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. F…