Longest Common Subsequence & Substring & prefix
Given two strings, find the longest common subsequence (LCS).
Your code should return the length of LCS.
For "ABCD" and "EDCA", the LCS is "A" (or "D", "C"), return 1.
For "ABCD" and "EACB", the LCS is "AC", return 2.
分析:
典型的DP。
public class Solution {
/**
* @param A, B: Two strings.
* @return: The length of longest common subsequence of A and B.
*/
public int longestCommonSubsequence(String str1, String str2) {
if (str1 == null || str2 == null) return ;
int[][] opt = new int[str2.length() + ][str1.length() + ];
for (int j = ; j <= str1.length(); j++) {
for (int i = ; i <= str2.length(); i++) {
if (i == || j == ) {
opt[i][j] = ;
} else if (str2.charAt(i-) == str1.charAt(j-)) {
opt[i][j] = opt[i-][j-] + ;
} else {
opt[i][j] = Math.max(opt[i-][j], opt[i][j-]);
}
}
}
return opt[str2.length()][str1.length()];
}
}
Longest Common Substring
Given two strings, find the longest common substring.
Return the length of it.
Notice
The characters in substring should occur continuously in original string. This is different with subsequence.
Given A = "ABCD", B = "CBCE", return 2.
分析:
从头比到尾呗。
public class Solution {
/**
* @param A, B: Two string.
* @return: the length of the longest common substring.
*/
public int longestCommonSubstring(String A, String B) {
if (A == null || B == null || A.length() == || B.length() == ) return ;
int max = ;
for (int i = ; i < B.length(); i++) {
for (int j = ; j < A.length(); j++) {
int incr = ;
while (i + incr < B.length() && j + incr < A.length() && (B.charAt(i + incr) == A.charAt(j + incr))) {
incr++;
max = Math.max(max, incr);
}
}
}
return max;
}
}
Longest Common Prefix
Given k strings, find the longest common prefix (LCP).
For strings "ABCD", "ABEF" and "ACEF", the LCP is "A"
For strings "ABCDEFG", "ABCEFG" and "ABCEFA", the LCP is "ABC"
分析:
取出第一个string和剩余的string相比即可。
public class Solution {
/**
* @param strs: A list of strings
* @return: The longest common prefix
*/
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == ) return "";
if (strs.length == ) return strs[];
StringBuilder sb = new StringBuilder();
for (int j = ; j < strs[].length(); j++) {
for (int i = ; i < strs.length; i++) {
if (j >= strs[i].length() || strs[i].charAt(j) != strs[].charAt(j)) {
return sb.toString();
}
}
sb.append(strs[].charAt(j));
}
return sb.toString();
}
}
Longest Common Subsequence & Substring & prefix的更多相关文章
- lintcode 77.Longest Common Subsequence(最长公共子序列)、79. Longest Common Substring(最长公共子串)
Longest Common Subsequence最长公共子序列: 每个dp位置表示的是第i.j个字母的最长公共子序列 class Solution { public: int findLength ...
- Longest common subsequence(LCS)
问题 说明该问题在生物学中的实际意义 Biological applications often need to compare the DNA of two (or more) different ...
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- Lintcode:Longest Common Subsequence 解题报告
Longest Common Subsequence 原题链接:http://lintcode.com/zh-cn/problem/longest-common-subsequence/ Given ...
- [Algorithms] Longest Common Subsequence
The Longest Common Subsequence (LCS) problem is as follows: Given two sequences s and t, find the le ...
- LintCode Longest Common Subsequence
原题链接在这里:http://www.lintcode.com/en/problem/longest-common-subsequence/ 题目: Given two strings, find t ...
- [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 称为已 ...
- Longest Common Subsequence
Given two strings, find the longest common subsequence (LCS). Your code should return the length of ...
随机推荐
- 转-JS之Window对象
一.说明:他是JS中最大的对象,它描述的是一个浏览器窗口,一般要引用他的属性和方法时,不需要用“Window.XXX”这种形式,而是直接使用“XXX”.一个框架页面也是一个窗口. 二.Window窗口 ...
- ajax使用post提交中文
Ajax使用POST提交中文乱码问题 前段时间写JSP,使用AJAX以POST方式提交数据,如果是中文字符提交就会乱码,后来写ASP时用到AJAX以POST方式提交数据,中文一样是乱码.搜索一下相关资 ...
- GPUImage学习
1.GLProgram--加载vertex和fragment的shader. 好处是完全将shader模块的加载过程独立出来. 学习:每个函数处理一件事,且函数的粒度刚好 在glLinkProgram ...
- 【poj2546】 Circular Area
http://poj.org/problem?id=2546 (题目链接) 题意 求两圆的面积交 Solution 一道水题Wa死我了,肯定是昨晚搞太晚的缘故= =. 两圆的位置关系有5种,而这里要求 ...
- SQLServer用sql语句怎么返回一个月所有的天数
可用如下sql语句: select convert(varchar(10),dateadd(DAY,t2.number,t1.day),120) day from (select '2015-07'+ ...
- hdu 1575 矩阵快速幂模板
#include "iostream" #include "vector" #include "cstring" using namespa ...
- bzoj1670 Usaco2006 Building the Moat护城河的挖掘 [凸包模板题]
Description 为了防止口渴的食蚁兽进入他的农场,Farmer John决定在他的农场周围挖一条护城河.农场里一共有N(8<=N<=5,000)股泉水,并且,护城河总是笔直地连接在 ...
- 迷宫问题(bfs)
import java.util.LinkedList; import java.util.Queue; import java.util.Stack; public class BFS { priv ...
- POJ1860Currency Exchange(Bellman + 正权回路)
Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 23938 Accepted: 867 ...
- Nagios安装部署和介绍(一)
一.软件版本下载 Nagios版本下载地址: http://prdownloads.sourceforge.net/sourceforge/nagios/ http://sourceforge.net ...