Substring appearing twice
Time limit6sMemory limit128 MB
Given a string and at most K letter replacements, maximize the length of the longest substring that occurs at two different starting positions, allowing overlap.
- Level
Hard8 of 10
- Topics
- String, Binary search, Dynamic programming, String matching
- Solved
- No attempts yet
Problem
You are given a string of lowercase English letters.
Define the following value: the length of the longest substring that occurs at two different starting positions in . The two occurrences may overlap. If no such substring exists, the value is .
You may replace a letter of with any other lowercase letter, and you may do this at most times. After every replacement is made, the value above is computed on the resulting string. Find the largest value you can reach.
Replacements apply to the string itself, once. When the two occurrences overlap, the letters inside the overlap are shared by both occurrences.
Input
The first line contains the integer ().
The second line contains the string , made of lowercase English letters only ().
Output
Print one integer, the largest value reachable with at most replacements.
Note
Take and equal to abcba. Changing the third letter c to a gives ababa, where the substring aba occurs at position 0 and at position 2. The answer is 3.