Longest Common Subsequence

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

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

Your code should return the length of LCS.

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

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

说明
What's the definition of Longest Common Subsequence?

* The longest common subsequence (LCS) problem is to find the longest subsequence common to all sequences in a set of sequences (often just two). (Note that a subsequence is different from a substring, for the terms of the former need not be consecutive terms of the original sequence.) It is a classic computer science problem, the basis of file comparison programs such as diff, and has applications in bioinformatics.

* https://en.wikipedia.org/wiki/Longest_common_subsequence_problem

标签 Expand

SOLUTION 1:

DP.

1. D[i][j] 定义为s1, s2的前i,j个字符串的最长common subsequence.

2. D[i][j] 当char i == char j, D[i - 1][j - 1] + 1

当char i != char j, D[i ][j - 1], D[i - 1][j] 里取一个大的(因为最后一个不相同,所以有可能s1的最后一个字符会出现在s2的前部分里,反之亦然。

 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) {
// write your code here
if (A == null || B == null) {
return 0;
} int lenA = A.length();
int lenB = B.length();
int[][] D = new int[lenA + 1][lenB + 1]; for (int i = 0; i <= lenA; i++) {
for (int j = 0; j <= lenB; j++) {
if (i == 0 || j == 0) {
D[i][j] = 0;
} else {
if (A.charAt(i - 1) == B.charAt(j - 1)) {
D[i][j] = D[i - 1][j - 1] + 1;
} else {
D[i][j] = Math.max(D[i - 1][j], D[i][j - 1]);
}
}
}
} return D[lenA][lenB];
}
}

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/lintcode/dp/longestCommonSubsequence.java

Lintcode:Longest Common Subsequence 解题报告的更多相关文章

  1. Lintcode: Longest Common Substring 解题报告

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

  2. 【LeetCode】873. Length of Longest Fibonacci Subsequence 解题报告(Python)

    [LeetCode]873. Length of Longest Fibonacci Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: ...

  3. 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)

    [LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...

  4. LintCode Longest Common Subsequence

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

  5. 【LeetCode】594. Longest Harmonious Subsequence 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计次数 日期 题目地址:https://leetc ...

  6. 【LeetCode】300. Longest Increasing Subsequence 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. LeetCode: Longest Common Prefix 解题报告

    Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...

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

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

  9. 【Lintcode】077.Longest Common Subsequence

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

随机推荐

  1. ipsec协议(转)

    from:http://lulu1101.blog.51cto.com/4455468/816875 ipsec协议 2012-03-25 23:40:28 标签:休闲 ipsec协议 职场 IPSe ...

  2. k8s源码分析之kubelet

    一.概述 二.Kubelet对象创建过程:(pkg/kubelet/kubelet.go ) NewMainKubelet 正如名字所示,主要的工作就是创建 Kubelet 这个对象,它包含了 kub ...

  3. 【Android】Android屏幕状态操作

    目录结构: contents structure [+] 如何监听屏幕锁屏 如何实现手机屏幕在锁屏后继续运行 最近笔者在做一个功能,就是实现Android程序在锁屏后可以继续运行,笔者在网上查了一些资 ...

  4. linux文件系统 - 初始化(一)

    术语表: struct task:进程 struct mnt_namespace:命名空间 struct mount:挂载点 struct vfsmount:挂载项 struct file:文件 st ...

  5. SLA等级那些9的实际意义

    1. 重要的系统起码要设计达到99.9%的可靠性吧. 俗称3个9,这是什么意思呢? (1-99.9%)*365*24=8.76小时,表示该软件系统在连续运行1年时间里最多可能的业务中断时间是8.76小 ...

  6. WinForm DataGridView新增加行

      1.不显示最下面的新行 通常 DataGridView 的最下面一行是用户新追加的行(行头显示 * ).如果不想让用户新追加行即不想显示该新行,可以将 DataGridView 对象的 Allow ...

  7. 增量式pid和位置式PID参数整定过程对比

    //增量式PID float IncPIDCalc(PID_Typedef* PIDx,float SetValue,float MeaValue)//err»ý·Ö·ÖÀë³£Êý { PIDx-& ...

  8. [转]What are mode and status columns under gp_segment_configuration table

    February 16, 2017 10:39 Goal In this article we will try to understand and answer to the below two q ...

  9. C#基础第五天-作业答案-用DataTable制作名片集

    .DataTable 实现 DataTable PersonCard = new DataTable(); //创建一个DataTable DataTable PersonCardCopy = new ...

  10. Jmeter录制HTTPS

    Jmeter有录制功能,录制HTTPs需要增加一个证书配置,录制步骤如下: 1.打开jmeter,添加线程组.线程组右键,逻辑控制器>录制控制器 工作台 右键 非测试元件 >HTTP代理服 ...