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.

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

hide comments
luyuncheng: 2013-03-15 09:07:25

why I use DC3 TLE who use DC3 AC?could you please talk to me? luyuncheng@sina.com

Julio [UFSCar]: 2013-03-03 03:49:24

My DC3 got TLE. Optimizing a little more...

aristofanis: 2013-02-06 19:02:09

I don't understand, why is O(N+M) giving me TLE?

manish sharma: 2012-11-29 23:48:18

dynamic programming will not work it costs around O(n*m) ...........try suffix tree that will work...complexity O(n+m).

http://en.wikipedia.org/wiki/Longest_common_substring_problem

use the above link

ymGXX: 2011-07-17 04:32:18

DC3 can AC....
nlogn TLE....

[Rampage] Blue.Mary: 2011-07-17 04:32:18

Stranger, stranger! It's just a constant-optimization problem, and some more...

যোবায়ের: 2011-07-17 04:32:18

it gave me "time limit exceeded #0"
what does it mean by #0 ?

Tony Beta Lambda: 2011-07-17 04:32:18

Are we expected to implement Suffix Array or Suffix Tree?


Jin Bin: Suffix Automaton was expected.

Last edit: 2010-11-09 05:51:31
Seshadri R: 2011-07-17 04:32:18

Do the strings contain any whitespace? I mean, would using scanf or cin produce any unpleasant surprises?

[Trichromatic] XilinX: 2011-07-17 04:32:18

The time limit is too strict. Constant optimization needed.


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