Lintcode: Longest Common Substring 解题报告
Longest Common Substring
原题链接: http://lintcode.com/zh-cn/problem/longest-common-substring/#
Given two strings, find the longest common substring.
Return the length of it.
The characters in substring should occur continiously in original string. This is different with subsequnce.
标签 Expand
SOLUTION 1:
public class Solution {
/**
* @param A, B: Two string.
* @return: the length of the longest common substring.
*/
public int longestCommonSubstring(String A, String B) {
// write your code here
if (A == null || B == null) {
return 0;
}
int lenA = A.length();
int lenB = B.length();
// bug 1: use error init.
int[][] D = new int[lenA + 1][lenB + 1];
int max = 0;
// BUG 2: should use <= instead of <
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] = 0;
}
}
max = Math.max(max, D[i][j]);
}
}
return max;
}
}
GITHUB:
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/lintcode/dp/LongestCommonSubstring.java
Lintcode: Longest Common Substring 解题报告的更多相关文章
- Lintcode:Longest Common Subsequence 解题报告
Longest Common Subsequence 原题链接:http://lintcode.com/zh-cn/problem/longest-common-subsequence/ Given ...
- LintCode Longest Common Substring
原题链接在这里:http://www.lintcode.com/en/problem/longest-common-substring/# 题目: Given two strings, find th ...
- LeetCode: Longest Common Prefix 解题报告
Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...
- Leetcode:【DP】Longest Palindromic Substring 解题报告
Longest Palindromic Substring -- HARD 级别 Question SolutionGiven a string S, find the longest palindr ...
- 【LeetCode】Longest Palindromic Substring 解题报告
DP.KMP什么的都太高大上了.自己想了个朴素的遍历方法. [题目] Given a string S, find the longest palindromic substring in S. Yo ...
- lintcode 77.Longest Common Subsequence(最长公共子序列)、79. Longest Common Substring(最长公共子串)
Longest Common Subsequence最长公共子序列: 每个dp位置表示的是第i.j个字母的最长公共子序列 class Solution { public: int findLength ...
- SPOJ LCS2 - Longest Common Substring II
LCS2 - Longest Common Substring II A string is finite sequence of characters over a non-empty finite ...
- Longest Common Substring
Given two strings, find the longest common substring. Return the length of it. Example Given A = &qu ...
- 【SPOJ】1812. Longest Common Substring II(后缀自动机)
http://www.spoj.com/problems/LCS2/ 发现了我原来对sam的理解的一个坑233 本题容易看出就是将所有匹配长度记录在状态上然后取min后再对所有状态取max. 但是不要 ...
随机推荐
- iOS 批量打包
如果你曾经试过做多 target 的项目,到了测试人员要测试包的时候,你就会明白什么叫“生不如死”.虽然 Xcode 打包很方便,但是当你机械重复打 N 次包的时候,就会觉得这纯粹是浪费时间的工作.所 ...
- 【虚拟化系列】VMware vSphere 5.1 网络管理
网络是VMware vSphere 5.1的基础,所有虚拟机都需要网络来进行通信.如果将所有的虚拟机都看成是物理机,则在网络拓扑上,需要网卡和交换机等不同的网络连接设备和方式.而在虚拟化中,这些 ...
- oracle收购Mysql后,Mysql的安装配置方法
自从Oracle收购MySQL后,略微发生了一些小小的变化,原来mysql安装完成后默认是没有密码的,但是新版的mysql安装完成后oracle提供了一个free password放着/root/.m ...
- SaltStack 入门到精通第三篇:Salt-Minion配置文件详解
SaltStack 入门到精通第三篇:Salt-Minion配置文件详解 作者:ArlenJ 发布日期:2014-06-09 17:52:16 ##### 主要配置设置 ##### 配置 默认值 ...
- 【Android】Android解析短信操作
目录结构: contents structure [-] 获取短信 发送短信 1.获取短信 在AndroidManifest.xml中,添加权限: <uses-permission androi ...
- 【C语言】字符串与整型数值之间的转换
一.将字符串转化为对应的数值 /*============================================================================= # # F ...
- 【iOS开发-36】Bundle Identifier的中文字符变成-的问题
在创建新项目时,Bundle Identifier=Organization Identifier+Product Name.可是它们对中文的识别统一变成短横线 - . 所以在创建多个项目的时候,须要 ...
- IntelliJ IDEA推荐插件
JRebel for IntelliJ 一款热部署插件,只要不是修改了项目的配置文件,用它都可以实现热部署.收费的,破解比较麻烦.不过功能确实很强大.算是开发必备神器了.热部署快捷键是control+ ...
- java字符串拼接的几种方式
1. plus方式 当左右两个量其中有一个为String类型时,用plus方式可将两个量转成字符串并拼接. String a="";int b=0xb;String c=a+b;2 ...
- office2010激活 错误代码0X8007000D,KMS激活0x8007000D错误解决办法,亲测成功激活
只针对VL版本 注意:此方法只对VL版本的,或者是MSDN版本通过替换文件转为VL版本的Office2010有效.零售版本的就别指望KMS激活了. 怎么看自己是不是VL版本的,只要看帮助里面激活的地方 ...