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.

求两个字符串的最长公共子序列,用动态规划来解决。

初始化二维数组 f[A.length() + 1][B.length() + 1],    f[ i ][ j ]表示的是A的前i个字符配上前j个字符的最长公共子序列长度。

为什么不是f[A.length() ] [B.length() ]呢,因为代表的前i个或者前j个字符,是包括了0的情况。

初始值:f[ i ][ 0 ] = 0, f [ 0] [ j ] = 0, 因为和空字符串的公共子序列肯定是长度为0;

如果A的第i-1个字符等于B的第j - 1个字符,说明最长公共子序列长度加1

f[ i ][ j ]等于f[ i - 1] [ j - 1] + 1

如果A的第i-1个字符不等于B的第j - 1个字符

f[ i ][ j ]等于 max(f[ i - 1] [ j ] , f [ i ] [ j -  1] )

注意:

因为定义f [ i ] [ j ]的i 、 j并不是下标 而是subsequence的长度

所以长度为i的字符串最后一个字符 所在的下标是i-1

public class Solution {
/**
* @param A, B: Two strings.
* @return: The length of longest common subsequence of A and B.
*/
public int longestCommonSubsequence(String A, String B) {
int m = A.length();
int n = B.length();
int[][] f = new int[m + 1][n + 1]; for (int i = 0; i < m; i++) {
f[i][0] = 0;
}
for (int j = 0; j < n; j++) {
f[0][j] = 0;
}
for (int i = 1; i < m + 1; i++) {
for (int j = 1; j < n + 1; j++) {
if (A.charAt(i - 1) == B.charAt(j - 1)) {
f[i][j] = f[i - 1][j - 1] + 1;
} else {
f[i][j] = Math.max(f[i - 1][j], f[i][j - 1]);
}
}
}
return f[m][n];
}
}

由于二维数组默认的值是0, 所以不给f[ i ][ 0 ]  和 f[ 0 ][ i ]赋初始值也是可以的,简写为:

public class Solution {
/**
* @param A, B: Two strings.
* @return: The length of longest common subsequence of A and B.
*/
public int longestCommonSubsequence(String A, String B) {
int n = A.length();
int m = B.length();
int f[][] = new int[n + 1][m + 1];
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
f[i][j] = Math.max(f[i - 1][j], f[i][j - 1]);
if(A.charAt(i - 1) == B.charAt(j - 1))
f[i][j] = f[i - 1][j - 1] + 1;
}
}
return f[n][m];
}
}

Longest Common Subsequence的更多相关文章

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

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

  2. LintCode Longest Common Subsequence

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

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

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

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

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

  5. Longest Common Subsequence & Substring & prefix

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

  6. Dynamic Programming | Set 4 (Longest Common Subsequence)

    首先来看什么是最长公共子序列:给定两个序列,找到两个序列中均存在的最长公共子序列的长度.子序列需要以相关的顺序呈现,但不必连续.例如,"abc", "abg", ...

  7. Lintcode:Longest Common Subsequence 解题报告

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

  8. UVA 10405 Longest Common Subsequence (dp + LCS)

    Problem C: Longest Common Subsequence Sequence 1: Sequence 2: Given two sequences of characters, pri ...

  9. [HackerRank] The Longest Common Subsequence

    This is the classic LCS problem. Since it requires you to print one longest common subsequence, just ...

随机推荐

  1. Java国际化程序

    根据不同的国家配置不同的资源文件(资源文件有时也称为属性文件,后缀为.properties),所有的资源文件以键值对的形式出现. Locale类 ResourceBundle类 //========= ...

  2. (转)HBase 的原理和设计

    转自:HBase的原理和设计 HBase架构:

  3. thikphp创建共享数据config.php

    要求:前台,后台:只需要配置一个config.php 其他文件共享 默认配置是 Index/Conf/config.php Admin/Conf/config.php 代码: return array ...

  4. DIV CSS 网页兼容全搞定 (IE6 IE7 IE8 IE9 火狐 谷歌)

    CSS兼容常用技巧 请尽量用xhtml格式写代码,而且DOCTYPE影响 CSS 处理,作为W3C标准,一定要加DOCTYPE声明. 1.div的垂直居中问题 vertical-align:middl ...

  5. SICP— 第一章 构造过程抽象

    SICP  Structure And Interpretation Of Computer Programs 中文第2版 分两部分  S 和 I 第一章 构造过程抽象 1,程序设计的基本元素 2,过 ...

  6. App Extension Today

     App Extensions 是iOS8新开放的扩展机制,之后不断增加功能.App Extension Programming Guide: Today   不喜欢废话,直接上干货!   一:重要概 ...

  7. text-indent:2em详解

    text-indent:2em; 解释一下:text的意思是文本,indent在计算机英语中意思是缩进,至于后面的2em意思就是2个相对单位: em又是什么单位? em这个单位的意思就是文字的高度,1 ...

  8. Task异常处理

    http://www.cnblogs.com/xray2005/archive/2011/08/24/2151459.html

  9. mysql中DATETIME,DATE和TIMESTAMP的区别整理

    简而言之.看格式,DATE 是 年月日YYYY-MM-DD,DATETIME 是 年月日时分秒YYYY-MM-DD HH:MM:SS,TIMESTAMP是 年月日时分秒YYYY-MM-DD HH:MM ...

  10. 为IIS Express添加MIME映射

    VS2013自带IIS Express,无法发布JSON文件,需添加MIME映射. 没有图形界面,只能命令行. 进入C:\Program Files(x86)\IIS Express文件夹,输入:ap ...