Hamzawy
Time limit1sMemory limit256 MB
Find the longest string that is a non-overlapping prefix, suffix, and middle substring of each given string.
- Level
Medium6 of 10
- Topics
- String matching
- Solved
- No attempts yet
Problem
Hamzawy was asked the following question in a programming interview and could not answer it, so solve it for him.
You are given a string of lowercase English letters. Find a string that is a prefix of , is a suffix of , and also appears once more as a substring of . The three occurrences must not overlap. Print the longest string that satisfies these conditions.
A prefix of a string is a string obtained by deleting zero or more characters from the end of . A suffix is a string obtained by deleting zero or more characters from the start of . A substring is a string obtained by deleting zero or more characters from the start of and zero or more characters from the end of .
Input
The first line contains the number of test cases . ()
Each of the next lines holds one test case: a non-empty string of lowercase English letters 'a' to 'z' whose length is at most . The sum of the lengths of all strings in one input is at most .
Output
For each test case print one line containing Case n:, then a single space, then the longest string that satisfies the conditions. Here is the test case number, starting from . If no such string exists, print -1 in place of the string.