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. css004 用样式继承节省时间

       css004 用样式继承节省时间 继承:inherit 继承可以简化样式表 继承是有局限的,有些样式没法继承,如:border,width,height

  2. Linux查看CPU和内存使用情况

    在系统维护的过程中,随时可能有需要查看 CPU 使用率,并根据相应信息分析系统状况的需要.在 CentOS 中,可以通过 top 命令来查看 CPU 使用状况.运行 top 命令后,CPU 使用状态会 ...

  3. nginx+php-fpm的socket配置小结

    关于socket的介绍本文不再赘述,生产环境中常用socket方式,本文简述其配置方式. #cd /app/local/php#切换到php安装目录下 #mkdir run #chmod 777 ./ ...

  4. ubuntu下vim输入中文和中文显示

    安装和配置VIM,参考   http://jingyan.baidu.com/album/046a7b3efd165bf9c27fa915.html?picindex=4 在home/你的用户名 这个 ...

  5. Java——其他容器

    除了JFrame表示之外,还有其他几种常见的窗体:JPanel.JSplitPane.JTabbedPane.JScrollPane.JDesktopPane.JInternalFrame等. imp ...

  6. JS数组类型检测

    在强类型语言,数组类型检测是非常容易的事情(typeof就可以解决),而在弱语言JS数据类型就很容易混淆了. JS中常见的数据类型有:number.string.boolean.undefined.f ...

  7. js返回顶部

    1. function scrollTop(){ $(},); } 2. $("#side-bar .gotop").click(function(){ $(},); //返回顶部 ...

  8. Database Initialization Strategies in Code-First:

    You already created a database after running your Code-First application the first time, but what ab ...

  9. yum管理

    一.yum发展与作用     在linux系统维护中管理员经常遇到软件包的依赖问题,有时无法解决,比如你在安装库文件时常出现报错问题,说依赖其它软件包.由于这个问题一直困绕linux的广大爱好者,开源 ...

  10. js中自定义事件,使用了jQuery

    $(function(){ $('#btn').bind("myClick", function(){ //自定义myClick事件 $('#test').append(" ...