Suffix array --- [USACO06FEC]Milk Patterns

[USACO06FEC]Milk Patterns

Topic description:

Farmer John has noticed that the quality of milk given by his cows varies from day to day.

On further investigation, he discovered that although he can't predict the quality of milk from one day to the next, there are some regular patterns in the daily milk quality.

 

To perform a rigorous study, he has invented a complex classification scheme by which each milk sample is recorded as an integer between 0 and 1,000,000 inclusive, and has recorded data from a single cow over N (1 ≤ N ≤ 20,000) days.

He wishes to find the longest pattern of samples which repeats identically at least K (2 ≤ K ≤ N) times.

This may include overlapping patterns -- 1 2 3 2 3 2 3 1 repeats 2 3 2 3 twice, for example.

 

Help Farmer John by finding the longest repeating subsequence in the sequence of samples.

It is guaranteed that at least one subsequence is repeated at least K times.

Input format:

Line 1: Two space-separated integers: N and K

Lines 2..N+1: N integers, one per line, the quality of the milk on day i appears on the ith line.

Output format:

Line 1: One integer, the length of the longest pattern which occurs at least K times

 

translate:

Farmer John finds that the quality of milk produced by his cows has been fluctuating.

After careful investigation, he found that although he could not foresee the quality of milk produced tomorrow, there was a lot of overlap in the quality of several consecutive days.

We call this a "pattern". John's milk can be assigned a number between 0 and 1,000,000 by quality.

And John records the milk quality value for N (1<=N<=20000) days.

He wants to know the length of the longest pattern that occurs at least K (2<=K<=N) times.

For example, 2 3 2 3 appears twice in 1 2 3 2 3 2 3 1. When K=2, this length is 4.

 

very classic suffix array

First find the three arrays of \(sa,rank,height\)

Divide into a length\(p\)

The suffixes are classified according to \(p\), specifically, those with common prefixes will be arranged together.

So we can also know that for a certain substring, the suffix arrays with it as a common prefix must be arranged together.

And the number of consecutive \(height\) of these suffix arrays exceeding \(len(string)\) is the number of occurrences of this substring.

Therefore, there is a substring that appears more than \(k\) times ------> The number of suffixes divided according to the \(height\) array has a group greater than \(k\)

What if the maximum length is required?

The bipartite length (\(len(string)\)) is enough to satisfy monotonicity.

 

The code is here (github is down, please wait)

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324645367&siteId=291194637