您的位置:首页 > 其它

第一篇Blog:NENU ACM集训第六天:简单的线性DP

2014-07-21 16:18 260 查看
线性DP比较经典的问题包括最大子段和、最长公共子序列、最长回文子序列、最长递增子序列、最长公共递增子序列等。

最大不相交字段和(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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: