Under construction

Still being built. Things may change

Chameleon Substring

Time limit2sMemory limit512 MB

Summary
Given a string S, find the longest substring that is both a prefix and a suffix of S and also appears somewhere strictly inside S.
Level

Medium7 of 10

Topics
String matching, String, Prefix sum
Solved
No attempts yet

Problem

A substring TT of a string SS is a chameleon substring when all three conditions hold.

  • TT is a prefix of SS.
  • TT is a suffix of SS.
  • TT also occurs in SS at a place that is neither of those two. That is, at least one occurrence of TT neither starts at the first character of SS nor ends at the last character of SS.

The three occurrences may overlap.

For example, if S=S = fixprefixsuffix, then fix is a chameleon substring. It is a prefix of SS, it is a suffix of SS, and it also occurs starting at the 7th character.

Given SS, find the longest chameleon substring.

Input

The first line contains the string SS. It consists of lowercase English letters only, and its length is between 11 and 10610^6.

Output

Print the longest chameleon substring on the first line. A chameleon substring is a prefix of SS, so its length fixes the string itself and the answer is unique. If no chameleon substring exists, print -1.

Examples7

  1. Example 1

    Input
    fixprefixsuffix
    
    Expected output
    fix
    
  2. Example 2

    Input
    abcdabc
    
    Expected output
    -1
    
  3. Example 3

    Input
    qwertyqwertyqwerty
    
    Expected output
    qwerty
    
  4. Example 4

    Input
    papapapap
    
    Expected output
    papap
    
  5. Example 5

    Input
    a
    
    Expected output
    -1
    
  6. Example 6

    Input
    aa
    
    Expected output
    -1
    
  7. Example 7

    Input
    aaa
    
    Expected output
    a