几个简单DP问题 虽然我不怎么不会。。

http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2080

最长公共子序列

状态方程

if ( ci==cj)

dp[i][j] = dp[i-1][j-1]+1;

else

dp[i][j] = max{dp[i-1][j],dp[i][j-1]};

View Code
 1 #include <iostream>

 2 #include<cstdio>

 3 #include<string.h>

 4 using namespace std;

 5 int main()

 6 {

 7     int i,j,k,n,m,dp[501][501],k1,k2;

 8     char c1[501],c2[501];

 9     while(gets(c1)!=NULL)

10     {

11         gets(c2);

12         k1 = strlen(c1);

13         k2 = strlen(c2);

14         for(i = 0 ; i < k1 ; i++)

15         dp[i][0] =0;

16         for(j = 0 ;j <= k2 ; j++)

17         dp[0][j] = 0;

18         for(i = 1 ; i <= k1 ; i++)

19         {

20             for(j =1 ; j <= k2 ; j++)

21             if(c1[i-1]==c2[j-1])

22             dp[i][j] = dp[i-1][j-1]+1;

23             else

24             {

25                 if(dp[i][j-1]<dp[i-1][j])

26                 dp[i][j] = dp[i-1][j];

27                 else

28                 dp[i][j] = dp[i][j-1];

29             }

30         }

31         printf("%d\n",dp[k1][k2]);

32     }

33     return 0;

34 }
最长上升子序列
View Code
 1 #include <iostream>

 2 #include<cstdio>

 3 #include<string.h>

 4 using namespace std;

 5 int dp[1001];

 6 int main()

 7 {

 8     int i,j,k,n,m,a[1001];

 9     scanf("%d",&n);

10     for(i = 1; i <= n ; i++)

11     {

12         scanf("%d",&a[i]);

13     }

14     dp[1] =1;

15     int max = 1;

16     for(i = 2; i <= n ;i++)

17     {

18         int tmax =1;

19         for(j = 1; j < i ; j++)

20         {

21             if(a[i]>a[j]&&tmax<dp[j]+1)

22             tmax = dp[j]+1;

23         }

24         dp[i] = tmax;

25         if(dp[i]>max)

26         max = dp[i];

27     }

28     printf("%d\n",max);

29     return 0;

30 }

http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1730

数字三角形

dp[i][j] = max{dp[i+1][j],dp[i+1][j+1]}+a[i][j];

View Code
 1 #include <iostream>

 2 #include<cstdio>

 3 #include<string.h>

 4 using namespace std;

 5 int main()

 6 {

 7     int i,j,k,n,dp[101][101],a[101][101];

 8     scanf("%d",&n);

 9     for(i = 1; i <= n ; i++)

10     for(j = 1 ; j <= i ; j++)

11     scanf("%d",&a[i][j]);

12     for(i = 1 ; i <= n ;i++)

13     dp[n][i] = a[n][i];

14     for(i = n-1; i >= 1 ; i--)

15     {

16         for(j = 1; j <= i ; j++)

17         if(dp[i+1][j]>dp[i+1][j+1])

18         dp[i][j]=a[i][j]+dp[i+1][j];

19         else

20         dp[i][j] = a[i][j]+dp[i+1][j+1];

21     }

22     printf("%d\n",dp[1][1]);

23     return 0;

24 }

http://acm.hdu.edu.cn/showproblem.php?pid=1003 最大序列和

View Code
 1 #include <iostream>

 2 #include<cstdio>

 3 #include<string.h>

 4 using namespace std;

 5 int a[100001];

 6 int main()

 7 {

 8     int i,j,k = 0,n,m,t,x,y;

 9     scanf("%d",&t);

10     while(t--)

11     {

12         k++;

13         scanf("%d",&n);

14         for(i = 1; i <= n ; i++)

15         scanf("%d",&a[i]);

16         int tmax = 0,max = -999,tx = 1,y = 1;

17         for(i = 1; i <= n ; i++)

18         {

19             tmax+=a[i];

20             if(tmax>max)

21             {

22                 max = tmax;

23                 x = tx;

24                 y = i;

25             }

26             if(tmax<0)

27             {

28                 tmax = 0;

29                 tx = i+1;

30             }

31         }

32         printf("Case %d:\n",k);

33         printf("%d %d %d\n",max,x,y);

34         if(t!=0)

35         puts("");

36     }

37     return 0;

38 }

http://www.cnblogs.com/shangyu/archive/2012/04/12/2445021.html

 

你可能感兴趣的:(dp)