Longest Common Substring
Given two strings, find the longest common substring.
Return the length of it.
Example
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) {
int maxlen = 0;
int m = A.length();
int n = B.length();
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
int len = 0;
while (i + len < m && j + len < n && A.charAt(i + len) == B.charAt(j + len)) {
len++;
maxlen = Math.max(maxlen, len);
}
}
}
return maxlen;
}
}
Longest Common Substring的更多相关文章
- SPOJ LCS2 - Longest Common Substring II
LCS2 - Longest Common Substring II A string is finite sequence of characters over a non-empty finite ...
- LintCode Longest Common Substring
原题链接在这里:http://www.lintcode.com/en/problem/longest-common-substring/# 题目: Given two strings, find th ...
- 【SPOJ】1812. Longest Common Substring II(后缀自动机)
http://www.spoj.com/problems/LCS2/ 发现了我原来对sam的理解的一个坑233 本题容易看出就是将所有匹配长度记录在状态上然后取min后再对所有状态取max. 但是不要 ...
- hdu 1403 Longest Common Substring(最长公共子字符串)(后缀数组)
http://acm.hdu.edu.cn/showproblem.php?pid=1403 Longest Common Substring Time Limit: 8000/4000 MS (Ja ...
- 后缀自动机(SAM):SPOJ Longest Common Substring II
Longest Common Substring II Time Limit: 2000ms Memory Limit: 262144KB A string is finite sequence of ...
- 后缀自动机(SAM) :SPOJ LCS - Longest Common Substring
LCS - Longest Common Substring no tags A string is finite sequence of characters over a non-empty f ...
- 后缀数组:HDU1043 Longest Common Substring
Longest Common Substring Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- Longest Common Substring(最长公共子序列)
Longest Common Substring Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...
- 【SPOJ】Longest Common Substring II (后缀自动机)
[SPOJ]Longest Common Substring II (后缀自动机) 题面 Vjudge 题意:求若干个串的最长公共子串 题解 对于某一个串构建\(SAM\) 每个串依次进行匹配 同时记 ...
随机推荐
- mysql导出到ms sql
导出为ms access数据库,然后在ms sql server管理器中导入就可以了,用csv.sql文件的方式都没成功
- Oracle11g的exp导出空表提示EXP-00011: 不存在
刚lg问我11g无法导出空表,实验了下,果真如此. 原因:11g默认创建一个表时不分配segment,只有在插入数据时才会产生(当然也可以强制分配),以节省磁盘空间. 对于已经存在的空表解决办法: 就 ...
- WSAStartup
WSAStartup,是Windows Sockets Asynchronous的启动命令.Windows下的网络编程接口软件 Winsock1 或 Winsock2 里面的一个命令. 外文名 WSA ...
- Java递归算法——三角数字
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.i ...
- GMU 简单使用一
<!doctype html> <html> <head> <title>iOS7风格的进度条</title> <meta chars ...
- Multiple actions were found that match the request Web API
在WebAPI工程入口不对外公开的接口不能使用public. [HttpPost] public string PostRequest([FromBody] Model model) { /// } ...
- phpstorm webstorm安装主题 sublime样 还有都可以用的注册码
注册码 webstorm phpstorm 基本所有版本通吃 webstrom9.0.3 通过 phpstorm 8.0.1 User Name: EMBRACE License Key: ==== ...
- Css常用收集
/*-------------------------------------- 圆角*/ -webkit-border-radius: 4px; -moz-border-radius: 4px; ...
- Yii2 如何更好的在页面注入JavaScript
先添加一个widgets <?php /** * User: yiqing * Date: 14-9-15 * Time: 下午12:09 */ namespace common\widgets ...
- PHP中的NULL类型
特殊的NULL值表示一个变量没有值,NULL类型唯一的值就是NULL.我们需要注意的是NULL不表示空格,也不表示零,也不是空字符串,而是一个变量的值为空.NULL不区分大小写,在下列情况下一个变量被 ...