LCS - Longest Common Substring

no tags 

A string is finite sequence of characters over a non-empty finite set Σ.

In this problem, Σ is the set of lowercase letters.

Substring, also called factor, is a consecutive sequence of characters occurrences at least once in a string.

Now your task is simple, for two given strings, find the length of the longest common substring of them.

Here common substring means a substring of two or more strings.


The input contains exactly two lines, each line consists of no more than 250000 lowercase letters, representing a string.


The length of the longest common substring. If such string doesn't exist, print "0" instead.




Notice: new testcases added

hide comments
yaswanth: 2019-10-13 20:58:35

Got TLE with Suffix array with storing temp results (partial ordering array). Had to change it to get AC. Too strict of a time limit.

beingbmc: 2019-09-24 12:51:06

NLogN suffix array works with cpp but TLE with Java

mac1309: 2019-07-26 13:30:09

its so bad that suffix array in O(nlog^2n) is giving tle

tuilathao: 2019-07-24 18:42:47

oh ye

tuilathao: 2019-07-24 18:41:31


mwx36mwx: 2018-05-26 10:33:53

manber-myers works for me :-)

cold_chair: 2017-12-08 11:26:37

As the example of Suffix automation,this problem is so classic!

Rajivteja: 2017-11-08 05:42:15

O(NlogN) suffix array won't work. You need to implement the O(N) suffix tree/suffix automaton for this.

darkghostnj: 2017-09-11 13:48:34

first i got TLE using suffix Automaton and dp and by changing the suffix Automaton algorithem i get AC with a good time ( it turns out that the old suffix Automaton implementation was very slow )

lu: 2017-02-28 13:56:19

Last edit: 2017-02-28 14:03:40

Added by:Bin Jin
Time limit:0.294s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64 CPP