Longest Common Substring
Time limit1sMemory limit256 MB
Find the length of the longest substring shared by two lowercase strings and print the lexicographically smallest one of that length.
- Level
Medium7 of 10
- Topics
- String matching, Binary search, Hash map, Sorting
- Solved
- No attempts yet
Problem
A string is a substring of a string when there is an index with . A substring is a run of consecutive characters cut out of .
You are given two strings and . Write a program that finds the length of the longest string that is a substring of and also a substring of , together with the lexicographically smallest common substring of that length.
Input
The first line has the string and the second line has the string . Both strings consist of lowercase letters only, and the sum of the two lengths is at most 200,000.
Output
Print the length of the longest common substring on the first line.
If that length is greater than 0, print on the second line the lexicographically smallest common substring of that length. If the two strings have no common substring, print only 0 and print no second line.