HackerRank - common-child【DP】

题意

给出两串长度相等的字符串,找出他们的最长公共子序列e

思路

字符串版的LCS

AC代码

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <cmath>
  5. #include <deque>
  6. #include <vector>
  7. #include <queue>
  8. #include <string>
  9. #include <cstring>
  10. #include <map>
  11. #include <stack>
  12. #include <set>
  13. #include <cstdlib>
  14. #include <ctype.h>
  15. #include <numeric>
  16. #include <sstream>
  17. using namespace std;
  18. typedef long long LL;
  19. const double PI = 3.14159265358979323846264338327;
  20. const double E = 2.718281828459;
  21. const double eps = 1e-6;
  22. const int MAXN = 0x3f3f3f3f;
  23. const int MINN = 0xc0c0c0c0;
  24. const int maxn = 1e3 * 5 + 5;
  25. const int MOD = 1e9 + 7;
  26. int dp[maxn][maxn];
  27. int main()
  28. {
  29. string a, b;
  30. cin >> a >> b;
  31. int i, j;
  32. int len = a.size();
  33. memset(dp, 0, sizeof(dp));
  34. for (i = 0; i < len; i++)
  35. {
  36. if (i)
  37. {
  38. dp[0][i] = dp[0][i - 1];
  39. dp[i][0] = dp[i - 1][0];
  40. }
  41. if (a[0] == b[i])
  42. dp[0][i] = 1;
  43. if (b[0] == a[i])
  44. dp[i][0] = 1;
  45. }
  46. for (i = 1; i < len; i++)
  47. {
  48. for (j = 1; j < len; j++)
  49. {
  50. if (a[i] == b[j])
  51. dp[i][j] = dp[i - 1][j - 1] + 1;
  52. else
  53. dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]);
  54. }
  55. }
  56. cout << dp[len - 1][len - 1] << endl;
  57. }

HackerRank - common-child【DP】的更多相关文章

  1. HDOJ 1159 Common Subsequence【DP】

    HDOJ 1159 Common Subsequence[DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...

  2. HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】

    HDOJ 1423 Greatest Common Increasing Subsequence [DP][最长公共上升子序列] Time Limit: 2000/1000 MS (Java/Othe ...

  3. Kattis - honey【DP】

    Kattis - honey[DP] 题意 有一只蜜蜂,在它的蜂房当中,蜂房是正六边形的,然后它要出去,但是它只能走N步,第N步的时候要回到起点,给出N, 求方案总数 思路 用DP 因为N == 14 ...

  4. HDOJ 1501 Zipper 【DP】【DFS+剪枝】

    HDOJ 1501 Zipper [DP][DFS+剪枝] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...

  5. HDOJ 1257 最少拦截系统 【DP】

    HDOJ 1257 最少拦截系统 [DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...

  6. HDOJ_1087_Super Jumping! Jumping! Jumping! 【DP】

    HDOJ_1087_Super Jumping! Jumping! Jumping! [DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...

  7. POJ_2533 Longest Ordered Subsequence【DP】【最长上升子序列】

    POJ_2533 Longest Ordered Subsequence[DP][最长递增子序列] Longest Ordered Subsequence Time Limit: 2000MS Mem ...

  8. LeetCode:零钱兑换【322】【DP】

    LeetCode:零钱兑换[322][DP] 题目描述 给定不同面额的硬币 coins 和一个总金额 amount.编写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合能组成 ...

  9. LeetCode:完全平方数【279】【DP】

    LeetCode:完全平方数[279][DP] 题目描述 给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, ...)使得它们的和等于 n.你需要让组成和的完全平方数的个数最少. 示 ...

随机推荐

  1. EasyUI获取DataGrid中某一列的所有值

    function count() { var rows = $('#dg'').datagrid('getRows')//获取当前页的数据行 var total = 0; for (var i = 0 ...

  2. 完成blog后台一枚

    技术实现:纯jfinal+AmazeUI

  3. java简易excel导入导出工具(封装POI)

    Octopus 如何导入excel 如何导出excel github项目地址 Octopus Octopus 是一个简单的java excel导入导出工具. 如何导入excel 下面是一个excel文 ...

  4. 【BZOJ】3299: [USACO2011 Open]Corn Maze玉米迷宫(bfs)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3299 映射一下传送门即可.. #include <cstdio> #include &l ...

  5. python Virtual Environments

    Install $ pip install virtualenv Basic usage 在一个项目中创建一个虚拟环境 $ cd my_project_folder $ virtualenv venv ...

  6. 重写ListView解决ListView内部ViewPaper滑动事件冲突问题

    非常easy 重写ListView 其它类似问题解决ScrollView嵌套ViewPager出现的滑动冲突问题 http://blog.csdn.net/zhangyiacm/article/det ...

  7. SPAF模板

    #include <iostream> #include <cstring> #include <queue> #include <cstdio> #d ...

  8. 【BZOJ4709】[Jsoi2011]柠檬 斜率优化+单调栈

    [BZOJ4709][Jsoi2011]柠檬 Description Flute 很喜欢柠檬.它准备了一串用树枝串起来的贝壳,打算用一种魔法把贝壳变成柠檬.贝壳一共有 N (1 ≤ N ≤ 100,0 ...

  9. iOS 修改textholder的颜色

    UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(, , , )]; textField.placeholde ...

  10. flex组合流动布局实例---利用css的order属性改变盒子排列顺序

    flex弹性盒子 <div class="container"> <div class="box yellow"></div> ...