第一篇Blog:NENU ACM集训第六天:简单的线性DP
2014-07-21 16:18
260 查看
线性DP比较经典的问题包括最大子段和、最长公共子序列、最长回文子序列、最长递增子序列、最长公共递增子序列等。
最大不相交字段和(poj 2479)
思路大概就是分别求[0~i-1]的最大子段和以及[i~n-1]的最大子段和,再加起来求最大的就可以了.
时间复杂度O(n).
求最大公共子序列(LCS)的长度 (poj 1458)
dp[i,j]表示X的前i个字符和Y的前j个字符的最长公共子序列长度, 后面的就好理解了,
转移方程 :
if(i==0||j==0) dp[i,j]=0;
else if(X[i]== Y[i]) dp[i,j] = dp[i-1,j-1]+1;
else dp[i,j]= max (dp[i-1,j],dp[j-1,i]);
End.
最大不相交字段和(poj 2479)
思路大概就是分别求[0~i-1]的最大子段和以及[i~n-1]的最大子段和,再加起来求最大的就可以了.
时间复杂度O(n).
#include<cstdio> #include<algorithm> #include<cstring> #include<iostream> #include<math.h> #include<cstdlib> #include<stack> #include<queue> using namespace std; int a[50001]; int Left[50001]; int Right[50001]; int main(){ int t; scanf("%d",&t); while(t--){ int n; scanf("%d",&n); for(int i = 0;i<n;i++) scanf("%d",&a[i]); Left[0] = a[0]; for(int i = 1;i<n;++i){ if(Left[i-1] < 0) Left[i] = a[i]; else Left[i] = Left[i-1] + a[i]; } for(int i = 1;i < n;i++) Left[i] = max(Left[i],Left[i-1]); Right[n-1] = a[n-1]; for(int i = n-2;i>=0;--i){ if(Right[i+1] < 0) Right[i] = a[i]; else Right[i] = Right[i+1] + a[i]; } for(int i = n-2;i>=0;i--) Right[i] = max(Right[i],Right[i+1]); int Inf = -1000000; for(int i=1;i<n;i++) Inf = max(Inf,Left[i-1] + Right[i]); printf("%d\n",Inf); } return 0 ; }
求最大公共子序列(LCS)的长度 (poj 1458)
dp[i,j]表示X的前i个字符和Y的前j个字符的最长公共子序列长度, 后面的就好理解了,
转移方程 :
if(i==0||j==0) dp[i,j]=0;
else if(X[i]== Y[i]) dp[i,j] = dp[i-1,j-1]+1;
else dp[i,j]= max (dp[i-1,j],dp[j-1,i]);
#include<cstdio> #include<algorithm> #include<cstring> #include<iostream> #include<math.h> #include<cstdlib> #include<stack> #include<queue> using namespace std; #define N 500 int dp ={0}; int main(){ int len1,len2; string str1,str2; while(cin>>str1>>str2){ len1 = str1.length(); len2 = str2.length(); for(int i = 1;i<=len1;i++){ for(int j = 1;j <= len2;j++){ if(str1[i-1] == str2[j-1]) dp[i][j] = dp[i-1][j-1]+1; else dp[i][j] = max(dp[i][j-1],dp[i-1][j]); } } printf("%d\n",dp[len1][len2]); } return 0 ; }
End.
相关文章推荐
- 我的第一篇BLOG
- 我的第一篇blog
- 教育网外BLOG的第一篇文章
- 我的第一篇Blog
- 我的第一篇Blog
- 2005年第一篇blog,祝大家新年快乐:)
- 第一篇:我的blog
- 我的BLOG的第一篇-----ORACLE的EXP工具使用。
- 一种简单方法实现页面导出(Excel)功能 选择自 xieyun1977 的 Blog
- 踩到一堆狗屎——我的第一篇Blog
- 原来这么简单就可以申请Blog了,害我还自己做了一个~
- 第一篇blog: 庆贺一把!
- 第一篇---简单介绍
- Blog第一篇
- 第一篇blog来啦 - 君子坦荡荡
- 第一篇文章,第一个blog,第一.......
- 我的第一篇BLog
- 第一篇,今天开始使用blog
- Asp.Net中DataGrid的模版列(TemplateColumn)内容控制简单方法。 选择自 TomMax 的 Blog
- [引]SQLServer和Access、Excel数据传输简单总结 选择自 xdev 的 Blog