Lintcode:Longest Common Subsequence 解题报告
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];
}
}
Lintcode:Longest Common Subsequence 解题报告的更多相关文章
- Lintcode: Longest Common Substring 解题报告
Longest Common Substring 原题链接: http://lintcode.com/zh-cn/problem/longest-common-substring/# Given tw ...
- 【LeetCode】873. Length of Longest Fibonacci Subsequence 解题报告(Python)
[LeetCode]873. Length of Longest Fibonacci Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: ...
- 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)
[LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...
- LintCode Longest Common Subsequence
原题链接在这里:http://www.lintcode.com/en/problem/longest-common-subsequence/ 题目: Given two strings, find t ...
- 【LeetCode】594. Longest Harmonious Subsequence 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计次数 日期 题目地址:https://leetc ...
- 【LeetCode】300. Longest Increasing Subsequence 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- LeetCode: Longest Common Prefix 解题报告
Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...
- 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 ...
随机推荐
- Ant压缩与解压缩
package com.test.utils; import java.io.File; import java.io.FileOutputStream; import java.io.InputSt ...
- Postgresql: UUID的使用
默认安装的 Postgresql 是不带 UUID 函数的,为了生成一个 UUID,我们必须装载它到数据库中. CREATE EXTENSION "uuid-ossp"; 然后就可 ...
- AndroidStudio OpenCv的配置,不用安装opencv manager
按照以下操作步骤配置并测试了,没问题. 下载OpenCV sdk for Android,解压(我的解压地址是F:\OpenCV-android-sdk) 1)新建项目项目,取名为Opencvtest ...
- 打开Word时出现“The setup controller has encountered a problem during install. Please ...”什么意思
解决办法:找到C:\Program Files\Common Files\Microsoft Shared\OFFICE12\Office Setup Controller,将这个文件夹删除或改名,就 ...
- 【转】我的第一次和最后一次 Hackathon 经历
我的第一次和最后一次 Hackathon 经历 在旧金山地区经常有一些叫做“Hackathon”的活动,吸引挺多人参加.我一直听说这个名字,可是一直不知道它到底是什么.我从来对竞赛式的活动不感兴趣,我 ...
- 绿色版mysql注册卸载服务
如果直接用绿色版的mysql,则下载后解压,只需对目录下的my.ini文件的basedir(mysql的基本目录)和datadir(mysql数据目录)指定就可以,如下所示. #Path to ins ...
- Selenium2自动化测试实战序言
记得很久之前接触自动化的时候看了一本关于某早期自动化测试工具的书,书名已经记不得了,内容却一直印象深刻.因为那本书根本就是把官方文档有选择性的翻译一遍,对于实际应用来说其作用几乎是零.因此从那时候起我 ...
- python制作exe可执行文件的方法---使用pyinstaller
python制作exe可执行文件的方法---使用pyinstaller python生成windows下exe格式的可执行程序有三种可选方案: py2exe是大家所熟知的,今天要介绍pyinsta ...
- Java 垃圾回收思维导图
文 by / 林本托 Tips 做一个终身学习的人. Java 的垃圾回收,不像 C和 C++语言,内存的分配和释放都是靠程序员来控制的.而 Java 的内存回收,程序员是不能也是无法干预,具体什么时 ...
- HTML自定义对象与属性探究(谷歌,火狐,IE9浏览器没问题)
1.自定义标签 <zqz>asdas</zqz> <style> zqz{ color:red; } </style> 页面变色 2.自定义标签的hov ...