• [USACO06FEC]Milk Patterns --- 后缀数组


    [USACO06FEC]Milk Patterns

    题目描述:

    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.

    输入格式:

    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.

    输出格式:

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

    翻译:

    农夫John发现他的奶牛产奶的质量一直在变动。

    经过细致的调查,他发现:虽然他不能预见明天产奶的质量,但连续的若干天的质量有很多重叠。

    我们称之为一个“模式”。 John的牛奶按质量可以被赋予一个0到1000000之间的数。

    并且John记录了N(1<=N<=20000)天的牛奶质量值。

    他想知道最长的出现了至少K(2<=K<=N)次的模式的长度。

    比如1 2 3 2 3 2 3 1 中 2 3 2 3出现了两次。当K=2时,这个长度为4。

     

    非常经典的后缀数组

    先求出(sa,rank,height)三个数组

    二分一个长度(p)

    根据(p)来给后缀分类,具体而言,有公共前缀的一定会排到一起。

    所以我们也可以知道对于某个子串,以它为公共前缀的后缀数组一定排到一起。

    并且这些后缀数组连续的(height)超过(len(string))的数量就是这个子串的出现次数。

    因此,存在一个子串出现超过(k)次 ------> 根据(height)数组分出来的后缀数量有一组大于(k)

    要求最长长度怎么办?

    二分长度((len(string)))即可,满足单调性。

     

    代码在此

  • 相关阅读:
    构造无限级树并深度遍历查找指定节点
    如何用python自动编写《赤壁赋》文档
    《易经》总结
    IDEA看代码必备插件Call Graph 介绍及使用方法
    chemfig化学式转换为pdf
    从wav到Ogg Opus 以及使用java解码OPUS
    使用jhipster 加速java web开发
    从Spring框架看设计模式如何灵活使用
    提升NginxTLS/SSL HTTPS 性能的7条优化建议
    高效团队的gitlab flow最佳实践
  • 原文地址:https://www.cnblogs.com/reverymoon/p/8903541.html
Copyright © 2020-2023  润新知