C++-POJ2955-Brackets[DP]】的更多相关文章

题目链接:https://vjudge.net/problem/POJ-2955 Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9630   Accepted: 5131 Description We give the following inductive definition of a “regular brackets” sequence: the empty sequence is a regu…
题目链接:http://poj.org/problem?id=2955 题意:给定字符串 求括号匹配最多时的子串长度. 区间dp,状态转移方程: dp[i][j]=max ( dp[i][j] , 2+dp[i+1][k-1]+dp[k+1][j] ); 代码: #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; #def…
取血怒.first blood,第一区间DP,这样第一次没有以某种方式在不知不觉中下降~~~ 题目尽管是鸟语.但还是非常赤裸裸的告诉我们要求最大的括号匹配数.DP走起~ dp[i][j]表示区间[i,j]的最大匹配数.那么最重要的状态转移方程就是: dp[i][j]=max(dp[i][k]+dp[k+1][j]) 对啦,要先初始化边界啊.两步走~: memset(dp,0,sizeof dp); if str[i]==str[i+1]   则:dp[i][i+1]=2       请看----…
给一个括号序列,求有几个括号是匹配的. dp[i][j]表示序列[i,j]的匹配数 dp[i][j]=dp[i+1][j-1]+2(括号i和括号j匹配) dp[i][j]=max(dp[i][k]+dp[k+1][j])(i<=k<j) #include<cstdio> #include<cstring> #include<algorithm> using namespace std; ]; ][]; int main(){ ]!='e'){ int n=s…
很好的区间DP题. 需要注意第一种情况不管是否匹配,都要枚举k来更新答案,比如: "()()()":dp[0][5]=dp[1][4]+2=4,枚举k,k=1时,dp[0][1]+dp[2][5]=6,最后取最大值6. 第一层d相当于"长度"的含义,第二层枚举i,j就可以用i+d表示,通过这种方式枚举区间左右端点. 1 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring>…
题目链接:http://poj.org/problem?id=2955 这题要求求出一段括号序列的最大括号匹配数量 规则如下: 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 sequences, th…
Famil Door and Brackets Time Limit: 20 Sec  Memory Limit: 512 MB Description Input Output Sample Input 4 1 ( Sample Output 4 HINT Solution 显然,我们考虑运用DP.先求出 f[i][j] 表示 长度为 i 的括号序列,“)” 比 “(” 多 j 个的方案(时刻保证 j >= 0). 然后我们考虑怎样获得答案.先预处理出L,R表示将读入的括号序列消去若干对之后剩…
C. Famil Door and Brackets 题目连接: http://www.codeforces.com/contest/629/problem/C Description As Famil Door's birthday is coming, some of his friends (like Gabi) decided to buy a present for him. His friends are going to buy a string consisted of roun…
DP: 边界条件:dp[0][j] = 1 递推公式:dp[i][j] = sum{dp[i-k][j] * dp[k-1][j-1] | 0<k≤i} i对括号深度不超过j的,能够唯一表示为(X)Y形式,当中X和Y能够为空,设X有k-1对括号,则相应的方案数为dp[i-k][j] * dp[k-1][j-1] Little Brackets Time Limit: 2 Seconds      Memory Limit: 65536 KB Consider all regular bracke…
Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8716   Accepted: 4660 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…