基础动态规划题目基础动态规划题目

打印 上一主题 下一主题

主题 556|帖子 556|积分 1668

目录
题目1: P1216 [USACO1.5] [IOI1994]数字三角形 Number Triangles
 代码示例:
题目2: Common Subsequence
代码示例
题目3 :最长上升子序列
最长不下降子序列
最长上升子序列oj答案
题目1: P1216 [USACO1.5] [IOI1994]数字三角形 Number Triangles

P1216 [USACO1.5] [IOI1994]数字三角形 Number Triangles - 洛谷 | 盘算机科学教育新生态 (luogu.com.cn)
https://www.luogu.com.cn/problem/P1216

 代码示例:

  1. // c++ 代码示例
  2. #include <algorithm>
  3. #include <iostream>
  4. using namespace std ;
  5. int n,a[1005][1005],f[1005][1005] ;
  6. int dfs(int x, int y)
  7. {
  8.     if (x == n) return a[x][y] ;
  9.     if (f[x][y] != -1) return f[x][y] ;
  10.     return f[x][y] = max(dfs(x + 1, y), dfs(x + 1, y + 1)) + a[x][y] ;
  11. }
  12. int main()
  13. {
  14.     int n ;
  15.     cin >> n ;
  16.     for (int i = 1 ; i <= n ; i++)
  17.     {
  18.         for (int j = 1 ; j <= i ; j++)
  19.         {
  20.             cin >> a[i][j] ;
  21.         }
  22.     }
  23.     for (int i = 1 ; i <= n ; i++)
  24.     {
  25.         for (int j = 1 ; j <= i ; j++)
  26.         {
  27.             f[i][j] = -1 ;
  28.         }
  29.     }
  30.     cout << dfs(1, 1) ;
  31.     return 0 ;
  32. }
复制代码
  1. // c++ 代码示例
  2. #include <algorithm>
  3. #include <iostream>
  4. using namespace std ;
  5. long long n, a[1005][1005] ;
  6. int main()
  7. {
  8.     cin >> n ;
  9.     for (int i = 1 ; i <= n ; i++)
  10.     {
  11.         for (int j = 1 ; j <= i ; j++)
  12.         {
  13.             cin >> a[i][j] ;
  14.         }
  15.     }
  16.     for (int i = n ; i >= 1 ; i--)
  17.     {
  18.         for (int j = 1 ; j <= i ; j++)
  19.         {
  20.             a[i][j] = a[i][j] + max(a[i + 1][j], a[i + 1][j + 1]);        
  21.             
  22.         }
  23.     }
  24.     cout << a[1][1] ;
  25.     return 0 ;
  26.    
  27. }
复制代码
 
  1. // c++ 代码示例
  2. #include <algorithm>
  3. #include <iostream>
  4. #include <cstdlib>
  5. #include <cstring>
  6. #include <cstdio>
  7. #include <string>
  8. using namespace std;
  9. #define rint register int
  10. inline void read(int &x)
  11. {
  12.     x = 0;
  13.     int w = 1;
  14.     char ch = getchar();
  15.     while (!isdigit(ch) && ch != '-')
  16.     {
  17.         ch = getchar();
  18.     }
  19.     if (ch == '-')
  20.     {
  21.         w = -1;
  22.         ch = getchar();
  23.     }
  24.     while (isdigit(ch))
  25.     {
  26.         x = (x << 3) + (x << 1) + (ch ^ '0');
  27.         ch = getchar();
  28.     }
  29.     x = x * w;
  30. }
  31. const int maxn = 1000 + 10;
  32. int n, a[maxn][maxn], ans;
  33. int main()
  34. {
  35.     read(n);
  36.     for (rint i = 1; i <= n; i++)
  37.     {
  38.         for (rint j = 1; j <= i; j++)
  39.         {
  40.             read(a[i][j]);
  41.             if (i == 1 && j == 1)
  42.             {
  43.                 // The top of the triangle
  44.                 continue;
  45.             }
  46.             if (j == 1)
  47.             {
  48.                 // Left boundary
  49.                 a[i][j] += a[i - 1][j];
  50.             }
  51.             else if (j == i)
  52.             {
  53.                 // Right boundary
  54.                 a[i][j] += a[i - 1][j - 1];
  55.             }
  56.             else
  57.             {
  58.                 // Middle elements
  59.                 a[i][j] += max(a[i - 1][j - 1], a[i - 1][j]);
  60.             }
  61.             ans = max(ans, a[i][j]);
  62.         }
  63.     }
  64.     cout << ans << endl;
  65.     return 0;
  66. }
复制代码

题目2: Common Subsequence

Common Subsequence - HDU 1159 - Virtual Judge (vjudge.net)
https://vjudge.net/problem/HDU-1159
代码示例

  1. // c++ 代码示例
  2. int a[MAXN], b[MAXN], f[MAXN][MAXN] ;
  3. int dp()
  4. {
  5.     for (int i = 1 ; i <= n ; i++)
  6.     {
  7.         for (int j = 1 ; j <= m ; j++)
  8.         {
  9.             if (a[i - 1] == b[j - 1])
  10.             {
  11.                 f[i][j] = f[i - 1][j - 1] + 1 ;
  12.             }
  13.             else
  14.             {
  15.                 f[i][j] = std::max(f[i - 1][j], f[i][j - 1]) ;
  16.             }
  17.         }
  18.     }
  19.     return f[n][m] ;
  20. }
复制代码
  1. // c++ 代码示例
  2. #include <cmath>
  3. #include <iostream>
  4. #include <cstdio>
  5. #include <cstdlib>
  6. #include <cstring>
  7. #include <algorithm>
  8. using namespace std;
  9. char a[1001], b[1001];
  10. int dp[1001][1001], len1, len2;
  11. void lcs(int i,int j)
  12. {
  13.     for(i=1; i<=len1; i++)
  14.     {
  15.         for(j=1; j<=len2; j++)
  16.         {
  17.             if(a[i-1] == b[j-1])
  18.                 dp[i][j] = dp[i-1][j-1] + 1;
  19.             else if(dp[i-1][j] > dp[i][j-1])
  20.                 dp[i][j] = dp[i-1][j];
  21.             else
  22.                 dp[i][j] = dp[i][j-1];
  23.         }
  24.     }
  25. }
  26. int main()
  27. {
  28.     while(~scanf(" %s",a))
  29.     {
  30.         scanf(" %s", b);
  31.         memset(dp, 0, sizeof(dp));
  32.         len1 = strlen(a);
  33.         len2 = strlen(b);
  34.         lcs(len1, len2);
  35.         printf("%d\n", dp[len1][len2]);
  36.     }
  37.     return 0;
  38. }
复制代码
题目3 :最长上升子序列

信息学奥赛一本通(C++版)在线评测体系 (ssoier.cn)
http://ybt.ssoier.cn:8088/problem_show.php?pid=1281
最长不下降子序列

  1. //c++代码示例
  2. # include <iostream>
  3. # include <cstdio>
  4. using namespace std ;
  5. int n ;
  6. int a[1001] ;
  7. int f[1001] ;
  8. int mx = -1 ;
  9. int main()
  10. {
  11.     scanf("%d", &n) ;
  12.     for (int i = 1 ; i <= n ; i++)
  13.     {
  14.         scanf("%d", &a[i]) ;
  15.         f[i] = 1 ;
  16.     }
  17.     for (int i = 2 ; i <= n ; i++)
  18.     {
  19.         for (int j = i - 1 ; j >= 1 ; j--)
  20.         {
  21.             if (a[i] >= a[j])
  22.             {
  23.                 f[i] = max(f[i], f[j] + 1) ;
  24.             }
  25.         }
  26.     }
  27.     for (int i = 1 ; i <= n ; i++)
  28.     {
  29.         mx = max(mx, f[i]) ;
  30.     }
  31.     printf("%d", mx) ;
  32.     return 0 ;
  33.    
  34. }
复制代码
  1. //c++代码示例
  2. # include <iostream>
  3. # include <cstdio>
  4. using namespace std ;
  5. int n ;
  6. int a[1001] ;
  7. int f[1001] ;
  8. int mx = -1 ;
  9. int main()
  10. {
  11.     scanf("%d", &n) ;
  12.     for (int i = 1 ; i <= n ; i++)
  13.     {
  14.         scanf("%d", &a[i]) ;
  15.         f[i] = 1 ;
  16.     }
  17.     for (int i = n - 1 ; i >= 1 ; i--)
  18.     {
  19.         for (int j = i + 1 ; j <= n ; j++)
  20.         {
  21.             if (a[i] <= a[j])
  22.             {
  23.                 f[i] = max(f[i], f[j] + 1) ;
  24.             }
  25.         }
  26.     }
  27.     for (int i = 1 ; i <= n ; i++)
  28.     {
  29.         mx = max(mx, f[i]) ;
  30.     }
  31.     printf("%d", mx) ;
  32.     return 0 ;
  33.    
  34. }
复制代码
最长上升子序列oj答案

  1. //c++代码示例
  2. # include <iostream>
  3. # include <cstdio>
  4. using namespace std ;
  5. int n ;
  6. int a[1001] ;
  7. int f[1001] ;
  8. int mx = -1 ;
  9. int main()
  10. {
  11.     scanf("%d", &n) ;
  12.     for (int i = 1 ; i <= n ; i++)
  13.     {
  14.         scanf("%d", &a[i]) ;
  15.         f[i] = 1 ;
  16.     }
  17.     for (int i = n - 1 ; i >= 1 ; i--)
  18.     {
  19.         for (int j = i + 1 ; j <= n ; j++)
  20.         {
  21.             if (a[i] < a[j])
  22.             {
  23.                 f[i] = max(f[i], f[j] + 1) ;
  24.             }
  25.         }
  26.     }
  27.     for (int i = 1 ; i <= n ; i++)
  28.     {
  29.         mx = max(mx, f[i]) ;
  30.     }
  31.     printf("%d", mx) ;
  32.     return 0 ;
  33.    
  34. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

大号在练葵花宝典

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表