LCS - Longest Common Substring

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.

Input

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

Output

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

Example

Input:
alsdfkjfjkdsal
fdjskalajfkdsla

Output:
3

Added by:Bin Jin
Date:2007-09-24
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64 CPP

hide comments
2018-05-26 10:33:53
manber-myers works for me :-)
2017-12-08 11:26:37
As the example of Suffix automation,this problem is so classic!
2017-11-08 05:42:15 Rajivteja
O(NlogN) suffix array won't work. You need to implement the O(N) suffix tree/suffix automaton for this.
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 )
2016-12-13 15:18:14
suffix tree with optimization...pheww...
2016-12-02 22:08:54
My first implementation of suffix Automaton...
2016-11-03 19:34:26 Abdo
Weak test cases.


Last edit: 2016-11-03 20:28:09
2016-08-26 12:07:51
After reading comments, i thought it won't get AC using O(NlogN) suffix array. But whoa! AC using O(NlogN) + KasaiLCP in 0.14 sec :D

Last edit: 2016-08-26 12:09:08
2016-07-26 13:51:47
O(N) suffix array is very efficient!
Actually, I ever solved this problem in AOJ, so I copied my solution from AOJ :-)

Last edit: 2016-07-26 13:53:22
2016-02-17 05:53:32 minhthai
O(NlogN) suffix array will not work :(
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.