Milk Patterns
Time limit1sMemory limit128 MB
Given N integers, find the length of the longest contiguous subsequence that repeats at least K times, counting overlapping occurrences.
- Level
Hard8 of 10
- Topics
- String matching, Binary search, Sorting, Array
- Solved
- No attempts yet
Problem
Farmer John has noticed that the quality of milk his cows produce varies from day to day. After a closer investigation he found that, although he cannot predict the next day's quality, the daily milk quality follows some regular patterns.
For a rigorous study he devised a classification scheme in which every milk sample is recorded as an integer between and inclusive, and he recorded data from a single cow over days (). He wants to find the longest pattern of samples that repeats identically at least times (). Occurrences of the pattern may overlap; for example, in the sequence 1 2 3 2 3 2 3 1 the pattern 2 3 2 3 repeats twice.
Given the sequence of samples, find the length of the longest such repeating contiguous subsequence. It is guaranteed that at least one subsequence repeats at least times.
Input
- Line 1: Two space-separated integers, and .
- Lines 2 to : integers, one per line; line holds the milk quality on day .
Output
- Line 1: A single integer, the length of the longest pattern that occurs at least times.