LintCode Longest Common Subsequence
原题链接在这里: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.
What's the definition of Longest Common Subsequence?
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:
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) {
if(A == null || B == null){
return 0;
}
int m = A.length();
int n = B.length();
int [][] dp = new int[m+1][n+1];
for(int i = 1; i<=m; i++){
for(int j = 1; j<=n; j++){
//两个末尾char match, 数目就是dp[i-1][j-1]+1
if(A.charAt(i-1) == B.charAt(j-1)){
dp[i][j] = dp[i-1][j-1]+1;
}else{
dp[i][j] = Math.max(dp[i][j-1],dp[i-1][j]);
}
}
}
return dp[m][n];
}
}
LintCode Longest Common Subsequence的更多相关文章
- Lintcode:Longest Common Subsequence 解题报告
Longest Common Subsequence 原题链接:http://lintcode.com/zh-cn/problem/longest-common-subsequence/ Given ...
- lintcode 77.Longest Common Subsequence(最长公共子序列)、79. Longest Common Substring(最长公共子串)
Longest Common Subsequence最长公共子序列: 每个dp位置表示的是第i.j个字母的最长公共子序列 class Solution { public: int findLength ...
- 【Lintcode】077.Longest Common Subsequence
题目: Given two strings, find the longest common subsequence (LCS). Your code should return the length ...
- LintCode Longest Common Substring
原题链接在这里:http://www.lintcode.com/en/problem/longest-common-substring/# 题目: Given two strings, find th ...
- C++版 - Lintcode 77-Longest Common Subsequence最长公共子序列(LCS) - 题解
版权声明:本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C++版 - L ...
- Lintcode: Longest Common Substring 解题报告
Longest Common Substring 原题链接: http://lintcode.com/zh-cn/problem/longest-common-substring/# Given tw ...
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- [UCSD白板题] Longest Common Subsequence of Three Sequences
Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...
- LCS(Longest Common Subsequence 最长公共子序列)
最长公共子序列 英文缩写为LCS(Longest Common Subsequence).其定义是,一个序列 S ,如果分别是两个或多个已知序列的子序列,且是所有符合此条件序列中最长的,则 S 称为已 ...
随机推荐
- 在CentOS7上安装Docker
具体过程如下 到网站下载centos7: http://isoredirect.centos.org/ http://isoredirect.centos.org/centos/7/isos/x86_ ...
- PV操作
学习PV操作之前,我们首先来了解两个很基础的概念: 同步.互斥: 同步:其实说同步还不如说”协作“,就是我们的目标只有一个,我们奔着同一个目标去的,都是在大家的努力下共同完成这么一件事情.还是比较容易 ...
- SSM框架整合首只拦路虎——Eclipse新建Maven Project界面select an archetype 空白
首先给大家说,本篇博客没有技术价值,纯属个人学习总结,权当给大家添加一乐.事件如有雷同,纯属巧合,莫怪! 前一段时间一直在看<淘淘商城>这个教程,里面讲的是SSM框架的一个电商项目.这不是 ...
- html学习第一天笔记
语义化,让你的网页更好的被搜索引擎理解在这一章节我们要开始把网页中常用到的标签一 一向大家介绍,学习这一章节的时候要记住学习html标签过程中,主要注意两个方面的学习:标签的用途.标签在浏览器中的默认 ...
- Trie URAL 7192 Chip Factory (15长春J)
题目传送门 题意:从n个数中选出不同的三个数a b c,使得(a+b)^c 最大 分析:先将所有数字按位插入到字典树上,然后删除两个数字,贪心询问与剩下的数字最大异或值. /************* ...
- sublime text3点击ctrl+B无法运行Python程序?
1.打开sublime text 3 ,选择 tools-->Build System-->New Build System.... 2.将下面代码块复制进新文件中,并命名为Python. ...
- JavaScript变量和作用域
认识JavaScript中的变量 JavaScript中的变量有两种类型,一种是基本类型.一种是引用类型. 基本数据类型:Defined,Null,Boolean,Number,String.注意St ...
- 将Matlab中的矩阵输出到txt文件
将矩阵输出到txt文件中的方法,遍寻网络,始见真经!!! fid=fopen('C:Documents and Settingscleantotal.ped','wt');%写入文件路径 matrix ...
- html5和css3学习笔记
HTML5针对移动端,移动端的浏览器主要是chrome,是webkit内核; app(applicatin):应用; native app:原生的app sadsadsadad 单标签可以省略结尾标记 ...
- Wordpress 所有hoor列表
d 在插件加载的时候执行 wp_footer 加载页面底部时执行 admin_menu 加载管理员菜单时执行 wp_head 在body标签的开始添加html内容 after_setup_theme ...