原题链接在这里:http://www.lintcode.com/en/problem/longest-common-subsequence/

题目:

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.

题解:

DP. 参考http://www.geeksforgeeks.org/dynamic-programming-set-4-longest-common-subsequence/

dp[i][j]表示长度为i的str1 和 长度为j的str2 LCS长度.

若是str1.charAt(i-1)  == str2.charAt(j-1) 尾字符相同, dp[i][j] = dp[i-1][j-1]+1.

若是不同dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]).

举例: 1. "AGGTAB" and "GXTXAYB". Last characters match for the strings. So length of LCS can be written as:
L("AGGTAB", "GXTXAYB") = 1 + L("AGGTAB", "GXTXAYB")

2. "ABCDGH" and "AEDFHR". Last characters do not match for the strings. So length of LCS can be written as:
L("ABCDGH","AEDFHR") = MAX ( L("ABCDG", "AEDFHR"), L("ABCDGH", "AEDFH")).

Time Complexity: O(m*n). Space: O(m*n).

AC Java:

  1. public class Solution {
  2. /**
  3. * @param A, B: Two strings.
  4. * @return: The length of longest common subsequence of A and B.
  5. */
  6. public int longestCommonSubsequence(String A, String B) {
  7. if(A == null || B == null){
  8. return 0;
  9. }
  10. int m = A.length();
  11. int n = B.length();
  12. int [][] dp = new int[m+1][n+1];
  13. for(int i = 1; i<=m; i++){
  14. for(int j = 1; j<=n; j++){
  15. //两个末尾char match, 数目就是dp[i-1][j-1]+1
  16. if(A.charAt(i-1) == B.charAt(j-1)){
  17. dp[i][j] = dp[i-1][j-1]+1;
  18. }else{
  19. dp[i][j] = Math.max(dp[i][j-1],dp[i-1][j]);
  20. }
  21. }
  22. }
  23. return dp[m][n];
  24. }
  25. }

LintCode Longest Common Subsequence的更多相关文章

  1. Lintcode:Longest Common Subsequence 解题报告

    Longest Common Subsequence 原题链接:http://lintcode.com/zh-cn/problem/longest-common-subsequence/ Given ...

  2. lintcode 77.Longest Common Subsequence(最长公共子序列)、79. Longest Common Substring(最长公共子串)

    Longest Common Subsequence最长公共子序列: 每个dp位置表示的是第i.j个字母的最长公共子序列 class Solution { public: int findLength ...

  3. 【Lintcode】077.Longest Common Subsequence

    题目: Given two strings, find the longest common subsequence (LCS). Your code should return the length ...

  4. LintCode Longest Common Substring

    原题链接在这里:http://www.lintcode.com/en/problem/longest-common-substring/# 题目: Given two strings, find th ...

  5. C++版 - Lintcode 77-Longest Common Subsequence最长公共子序列(LCS) - 题解

    版权声明:本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C++版 - L ...

  6. Lintcode: Longest Common Substring 解题报告

    Longest Common Substring 原题链接: http://lintcode.com/zh-cn/problem/longest-common-substring/# Given tw ...

  7. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  8. [UCSD白板题] Longest Common Subsequence of Three Sequences

    Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...

  9. LCS(Longest Common Subsequence 最长公共子序列)

    最长公共子序列 英文缩写为LCS(Longest Common Subsequence).其定义是,一个序列 S ,如果分别是两个或多个已知序列的子序列,且是所有符合此条件序列中最长的,则 S 称为已 ...

随机推荐

  1. 【Java EE 学习 23】【log4j的使用】【ant的使用】【内省】

    一.ant下载地址:http://ant.apache.org/bindownload.cgi  二.log4j下载地址:http://logging.apache.org/log4j/2.x/dow ...

  2. [leetcode] 13. Roman to Integer

    如果某一个字母代表的数字大于上一个字母代表的数字小,那么加上这个数字,否则,减去两倍的前一个数字,然后加上这一位数字. public class Solution { private static c ...

  3. Python lambda函数

    python允许定义单行的小函数,定义lambda函数的形式如下: lambda 参数:表达式lambda函数默认返回表达式的值,可接收任意个参数,包括可选参数,但是表达式只有一个.

  4. iOS--异步下载

    #import "ViewController.h"#import "UIImageView+WebCache.h"@interface ViewControl ...

  5. oracle的sqlnet.ora,tnsnames.ora,listener.ora三个配置文件

    总结: 1 .三个配置文件都是放在$ORACLE_HOME\network\admin目录下. 2 .sqlnet.ora确定解析方式 3 .listener.ora上设SID_NAME,通常用于JD ...

  6. Employment Planning[HDU1158]

    Employment Planning Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...

  7. 【JAVA】JDK-SimpleDataFormat 线程不安全!

    [问题] publicclassProveNotSafe { staticSimpleDateFormat df = newSimpleDateFormat("dd-MMM-yyyy&quo ...

  8. 用于异步的BackgroundWorker

    XAML代码: <Window x:Class="backgroundtest.MainWindow" xmlns="http://schemas.microsof ...

  9. 【Alpha】Daily Scrum Meeting第八次

    一.本次Daily Scrum Meeting主要内容 抓紧冲刺(接下去两天都在下午增加一个小会议) 剩余任务的概况 二.项目进展 学号尾数 今日已完成任务 接下去要做 502 无 将数据库的数据转换 ...

  10. java.math.RoundingMode 几个参数详解

    java.math.RoundingMode里面有几个参数搞得我有点晕,现以个人理解对其一一进行总结: 为了能更好理解,我们可以画一个XY轴 RoundingMode.CEILING:取右边最近的整数 ...