This page is still under construction.

Parts of this page are still being built. What you see may change.

Longest Common Substring

Time limit1sMemory limit256 MB

Summary
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 T=t1t2…tmT = t_1 t_2 \dots t_m is a substring of a string S=s1s2…snS = s_1 s_2 \dots s_n when there is an index 0≤i≤n−m0 \le i \le n - m with si+1si+2…si+m=Ts_{i+1} s_{i+2} \dots s_{i+m} = T. A substring is a run of consecutive characters cut out of SS.

You are given two strings AA and BB. Write a program that finds the length of the longest string that is a substring of AA and also a substring of BB, together with the lexicographically smallest common substring of that length.

Input

The first line has the string AA and the second line has the string BB. 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.

Examples3

  1. Example 1

    Input
    yeshowmuchiloveyoumydearmotherreallyicannotbelieveit
    yeaphowmuchiloveyoumydearmother
    
    Expected output
    27
    howmuchiloveyoumydearmother
    
  2. Example 2

    Input
    abxcd
    cdxab
    
    Expected output
    2
    ab
    
  3. Example 3

    Input
    abc
    xyz
    
    Expected output
    0