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. 但是不要 ...
随机推荐
- linux 版本中 i386/i686/x86-64/pcc 等的区别
在查看dpdk官方文档的时候,发现有 这样(kernel - devel.x86_64; kernel - devel.ppc64:glibc.i686)这样的安装包信息,收集了点资料来分析这三者的关 ...
- linux下常用文件传输命令(转)
因为工作原因,需要经常在不同的服务器见进行文件传输,特别是大文件的传输,因此对linux下不同服务器间数据传输命令和工具进行了研究和总结.主要是rcp,scp,rsync,ftp,sftp,lftp, ...
- The method getServletContext() is undefined for the type HttpServletRequest
request.getServletContext().getRealPath("/") 已经加入了 sun runtime library但是还是提示错误 是因为 写法过时了改成 ...
- A. Candy Bags
A. Candy Bags http://codeforces.com/problemset/problem/334/A time limit per test 1 second memory l ...
- golang下划线(underscore) 总结
一.概述 "_" 可以简单理解为赋值但以后不再使用,在golang中使用的比较多,使用的场合也很多,稍作总结: 二.场景 1.import import _ "net/h ...
- 【Spring】spring的7个模块
Spring 是一个开源框架,是为了解决企业应用程序开发复杂性而创建的.框架的主要优势之一就是其分层架构,分层架构允许您选择使用哪一个组件,同时为 J2EE 应用程序开发提供集成的框架. Spring ...
- Linux下面安装RabbitMQ Cluster
安装rabbitmq cluster: 设置 Erlang Cookie安装完RabbitMQ之后,在第一台机器上面启动RabbitMQ,然后在停止.复制node1上的/var/lib/rabbitm ...
- 根据友盟统计错误分析线上的崩溃-b
登陆友盟官网找到友盟统计,找到你iOS平台下你所属的APP(图1) 图1 点击进去会出现当日错误列表,选择你发生错误的日期(图2) 图2 我们可以看到,这一天中出现了两个错误,每个错误出现在不同的时间 ...
- ==和equals的简单比较
前言:==和equals这个两个东西,经常放在一块做比较,下面我也给出一个简单的例子,对他俩进行一个简单的比较,先看例子,然后在看结论.(实验环境:win7+jdk7) 1:==和equals简单比较 ...
- Kafka生产环境中的错误
最近在处理日志收集任务时,发现前端服务器用flume进行收集,逐步把所有的服务器都增加上.增加的差不多时.Kafka报了如下类似错误: 709 [main] WARN kafka.producer.a ...