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的更多相关文章

  1. SPOJ LCS2 - Longest Common Substring II

    LCS2 - Longest Common Substring II A string is finite sequence of characters over a non-empty finite ...

  2. LintCode Longest Common Substring

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

  3. 【SPOJ】1812. Longest Common Substring II(后缀自动机)

    http://www.spoj.com/problems/LCS2/ 发现了我原来对sam的理解的一个坑233 本题容易看出就是将所有匹配长度记录在状态上然后取min后再对所有状态取max. 但是不要 ...

  4. hdu 1403 Longest Common Substring(最长公共子字符串)(后缀数组)

    http://acm.hdu.edu.cn/showproblem.php?pid=1403 Longest Common Substring Time Limit: 8000/4000 MS (Ja ...

  5. 后缀自动机(SAM):SPOJ Longest Common Substring II

    Longest Common Substring II Time Limit: 2000ms Memory Limit: 262144KB A string is finite sequence of ...

  6. 后缀自动机(SAM) :SPOJ LCS - Longest Common Substring

    LCS - Longest Common Substring no tags  A string is finite sequence of characters over a non-empty f ...

  7. 后缀数组:HDU1043 Longest Common Substring

    Longest Common Substring Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  8. Longest Common Substring(最长公共子序列)

    Longest Common Substring Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...

  9. 【SPOJ】Longest Common Substring II (后缀自动机)

    [SPOJ]Longest Common Substring II (后缀自动机) 题面 Vjudge 题意:求若干个串的最长公共子串 题解 对于某一个串构建\(SAM\) 每个串依次进行匹配 同时记 ...

随机推荐

  1. EmgnCv进行轮廓寻找和计算物体凸包

    http://blog.csdn.net/qq_22033759/article/details/48029493 一.轮廓寻找 用的是FindContours函数,在CvInvoke中 不过需要用到 ...

  2. Linux 的cp命令详解

    功能: 复制文件或目录说明: cp指令用于复制文件或目录,如同时指定两个以上的文件或目录,且最后的目的地是一个已经存在的目录,则它会把前面指定的所有文件或目录复制到此目录中.若同时指定多个文件或目录, ...

  3. DB服务器中的参数优化

    1.swappiness 禁止系统使用swap空间,配置/etc/sysctl.conf中的vm.swappiness=0 2.Scheduler调度 Scheduler调度,指的是磁盘的IO调度算法 ...

  4. Starling Tutorial

    http://www.hsharma.com/tutorials/starting-with-starling-ep-1-intro-setup/

  5. js中event的target和currentTarget的区别

    js中的event对象包含很多有用的信息 target:触发事件的元素. currentTarget:事件绑定的元素. 两者在没有冒泡的情况下,是一样的值,但在用了事件委托的情况下,就不一样了,例如: ...

  6. ecshop 后台-》广告

    1.后台广告宽度限制不能超过1024,高度大于1,admin/ad_position.php 第236行 || $ad_width < ) { make_json_error($_LANG['w ...

  7. 要引用这几个才有GetOwinContext与GetAutofacLifetimeScope

    using Owin; using Autofac; using Autofac.Integration.Owin; using System.Web; var owin = this.Request ...

  8. Python之路【目录】 2

    http://www.cnblogs.com/wupeiqi/articles/4938499.html

  9. 单选框的回显c:if

    <input type="radio" name="sex" value="boy" <c:if test="${te ...

  10. LINUX下搭建VPN

    一.准备 需要 dkms-2.0.17.5-1.noarch.rpm.ppp-2.4.5-33.0.rhel6.x86_64.rpm.pptpd-1.4.0-1.el6.x86_64.rpm,并依次安 ...