#BS0000464. Xâu con lặp dài nhất (Longest Duplicate Substring)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Xâu con lặp dài nhất (Longest Duplicate Substring)
Longest Duplicate Substring
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
Given a string of lowercase English letters, a substring is a contiguous block of characters of . A substring is repeated if it occurs at two or more distinct starting positions. The occurrences may overlap.
Find a repeated substring of maximum length. If several substrings attain that length, any of them is accepted.
Input
The only line contains the string . Let .
Output
Print any longest repeated substring. If no nonempty repeated substring exists, print -1.
Subtasks
- Subtask 1 (20 points): ; every character of is between
aandz. - Subtask 2 (30 points): ; every character of is between
aandz. - Subtask 3 (50 points): ; every character of is between
aandz.
Examples
Input
banana
Output
ana
Explanation
ana begins at positions and (one-based); the occurrences overlap at position . The length-four substrings bana, anan, nana are distinct, so is optimal.
Input
abcd
Output
-1
Explanation
All four letters are distinct, so no nonempty substring occurs twice. Print -1.
Input
aabb
Output
a
Explanation
Both a and b occur twice. The length-two blocks aa, ab, bb are distinct. Either a or b is accepted.