Brackets Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14226 Accepted: 7476 Description We give the following inductive definition of a "regular brackets" sequence: the empty sequence is a regular brackets sequence, if s is a regula…
题目链接: http://poj.org/problem?id=2955 题目大意:括号匹配.对称的括号匹配数量+2.问最大匹配数. 解题思路: 看起来像个区间问题. DP边界:无.区间间隔为0时,默认为memset为0即可. 对于dp[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][j]),合并两个区间的结果. #include "cstdio"…
第一道自己做出来的区间dp题,兴奋ing,虽然说这题并不难. 从后向前考虑: 状态转移方程:dp[i][j]=dp[i+1][j](i<=j<len); dp[i][j]=Max(dp[i][j],dp[i+1][k-1]+dp[k+1][j]+1),(a[i]==a[j]&&i<len,j<len,k<len); #include<stdio.h> #include<string.h> #define N 300 int dp[N][…
求一个括号的最大匹配数,这个题可以和UVa 1626比较着看. 注意题目背景一样,但是所求不一样. 回到这道题上来,设d(i, j)表示子序列Si ~ Sj的字符串中最大匹配数,如果Si 与 Sj能配对,d(i, j) = d(i+1, j-1) 然后要枚举中间点k,d(i, j) = max{ d(i, k) + d(k+1, j) } #include <iostream> #include <cstdio> #include <cstring> #include…
见题解链接https://blog.csdn.net/sdjzping/article/details/19160013 #include<bits/stdc++.h> using namespace std; #define maxn 750 #define mod 1000000007 int len,p[maxn]; ][]; char s[maxn]; void match(){ stack<int>stk; while(!stk.empty())stk.pop(); ;i…
https://www.nitacm.com/problem_show.php?pid=8314 思路:类似于https://blog.csdn.net/MIKASA3/article/details/51523563 #define IOS ios_base::sync_with_stdio(0); cin.tie(0); #include <cstdio>//sprintf islower isupper #include <cstdlib>//malloc exit strc…
Cheapest Palindrome Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7869   Accepted: 3816 Description Keeping track of all the cows can be a tricky task so Farmer John has installed a system to automate it. He has installed on each cow a…
//poj 2955 //sep9 #include <iostream> using namespace std; char s[128]; int dp[128][128]; int n; int rec(int l,int r) { if(dp[l][r]!=-1) return dp[l][r]; if(l==r) return dp[l][r]=0; if(l+1==r){ if(s[l]=='('&&s[r]==')') return dp[l][r]=2; if(…
题目链接: http://poj.org/problem?id=1651 题目大意:加分取牌.如果一张牌左右有牌则可以取出,分数为左牌*中牌*右牌.这样最后肯定还剩2张牌.求一个取牌顺序,使得加分最少. 解题思路: 矩阵链乘的变种题. 假设有10.20.30.40.50五张牌. 如果我想要最后取30,则应该先取20.40,这样就还剩10.30.50三张牌了. 不难发现取20是dp[i][k]部分,取40是dp[k][j]部分,最后剩下的就是i.k.j三张牌. DP边界:无 因为是算加分,区间间隔…
给一组小括号与中括号的序列,加入最少的字符,使该序列变为合法序列,输出该合法序列. dp[a][b]记录a-b区间内的最小值, mark[a][b]记录该区间的最小值怎样得到. #include "stdio.h" #include "string.h" int inf=99999999; char str[110]; int dp[110][110],mark[110][110]; void pri(int l,int r) { if (l>r) retur…