题目:

Given two strings, find the longest common subsequence (LCS).

Your code should return the length of LCS.

Clarification

Example

For "ABCD" and "EDCA", the LCS is "A" (or "D""C"), return 1.

For "ABCD" and "EACB", the LCS is "AC", return 2.

题解:

Solution 1 ()

  1. class Solution {
  2. public:
  3. /**
  4. * @param A, B: Two strings.
  5. * @return: The length of longest common subsequence of A and B.
  6. */
  7. int longestCommonSubsequence(string A, string B) {
  8. int n1 = A.size(), n2 = B.size();
  9. vector<vector<int>> dp(n1 + , vector<int> (n2 + , ));
  10. for (int i = ; i <= n1; ++i) {
  11. for (int j = ; j <= n2; ++j) {
  12. if (A[i - ] == B[j - ]) {
  13. dp[i][j] = dp[i - ][j - ] + ;
  14. } else {
  15. dp[i][j] = max(dp[i - ][j], dp[i][j - ]);
  16. }
  17. }
  18. }
  19. return dp[n1][n2];
  20. }
  21. };

【Lintcode】077.Longest Common Subsequence的更多相关文章

  1. 【leetcode】1143. Longest Common Subsequence

    题目如下: Given two strings text1 and text2, return the length of their longest common subsequence. A su ...

  2. 【Lintcode】076.Longest Increasing Subsequence

    题目: Given a sequence of integers, find the longest increasing subsequence (LIS). You code should ret ...

  3. 【SP1812】LCS2 - Longest Common Substring II

    [SP1812]LCS2 - Longest Common Substring II 题面 洛谷 题解 你首先得会做这题. 然后就其实就很简单了, 你在每一个状态\(i\)打一个标记\(f[i]\)表 ...

  4. 【SP1811】LCS - Longest Common Substring

    [SP1811]LCS - Longest Common Substring 题面 洛谷 题解 建好后缀自动机后从初始状态沿着现在的边匹配, 如果失配则跳它的后缀链接,因为你跳后缀链接到达的\(End ...

  5. 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)

    [LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...

  6. 【leetcode】300.Longest Increasing Subsequence

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  7. 【SPOJ】1812. Longest Common Substring II(后缀自动机)

    http://www.spoj.com/problems/LCS2/ 发现了我原来对sam的理解的一个坑233 本题容易看出就是将所有匹配长度记录在状态上然后取min后再对所有状态取max. 但是不要 ...

  8. 【LeetCode】14. Longest Common Prefix 最长前缀子串

    题目: Write a function to find the longest common prefix string amongst an array of strings. 思路:求最长前缀子 ...

  9. 【LeetCode】14 - Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. Solution: cla ...

随机推荐

  1. 深度 | Facebook的图像识别很强大,一次开源了三款机器视觉工具(附论文)

    http://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&mid=2650718597&idx=1&sn=56aa4e5deff9962 ...

  2. Microsoft-office 常见问题

    1.工作表写入保护,忘记密码,解决办法: 流程如下: 1打开文件2工具---宏----录制新宏---输入名字如:aa3停止录制(这样得到一个空宏)4工具---宏----宏,选aa,点编辑按钮5删除窗口 ...

  3. linux history 命令 禁用history

    保存在.bash_history文件中,默认1000条,你也可以更改这个 值 !!:上一个指令 !number 运行第几个指令 查看命令历史的时间戳,那么可以执行: # export HISTTIME ...

  4. iOS --生产JSON格式,创建JSON文件,创建文件夹,指定储存

    //生成json文件 - (void)onjson { //    如果数组或者字典中存储了  NSString, NSNumber, NSArray, NSDictionary, or NSNull ...

  5. __str__ 和 __unicode__ 的区别和用法

    转自:https://www.cnblogs.com/painberg/p/8514860.html 简而言之,就是__str__和__unicode__都是为了再管理站点中加载这个表时想显示什么属性 ...

  6. ES通过API调整设置

    1.查询es的设置信息 2.查询单个索引的设置 3.设置复制集为0

  7. MongoDB的CRUD操作(java Util )

    1.保存插入操作: public static synchronized String insert(DBObject record) { DBCollection col = MongoDB.get ...

  8. (1)Web 应用是一个状态机,视图与状态是一一对应的。 (2)所有的状态,保存在一个对象里面。

    Redux 入门教程(一):基本用法 - 阮一峰的网络日志 http://www.ruanyifeng.com/blog/2016/09/redux_tutorial_part_one_basic_u ...

  9. 【题解】P3162CQOI2012组装

    [题解][CQOI2012]组装 考虑化为代数的形式,序列\(\left[a_i \right]\)表示选取的\(i\)种类仓库的坐标. \(ans=\Sigma(a_i-x)^2,(*)\),展开: ...

  10. POJ 2442 Sequence【堆】

    题目链接:http://poj.org/problem?id=2442 题目大意:给出一个m*n的矩阵,从每一行中取出一个数相加.能得到n^m个不同的结果.要求输出当中前n项. 建立一个以n元数组为底 ...