zoukankan      html  css  js  c++  java
  • POJ 3261 Milk Patterns(后缀数组)

    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

    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

    Line 1: One integer, the length of the longest pattern which occurs at least K times
     
    题目大意:给一串数字,问至少重复出现K次的子串最长有多长,这些子串可以重复。
    思路:构建后缀数组。二分长度L,看height数组内是否有连续K-1个大于等于L的。
     
    代码(94MS):
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 #include <iostream>
     5 using namespace std;
     6 
     7 const int MAXN = 20010;
     8 const int MAXK = 1000010;
     9 
    10 int s[MAXN], n, k;
    11 int sa[MAXN], rank[MAXN], tmp[MAXN], height[MAXN];
    12 int c[MAXK];
    13 
    14 void makesa(int m) {
    15     memset(c, 0, sizeof(c));
    16     for(int i = 0; i < n; ++i) ++c[rank[i] = s[i]];
    17     for(int i = 1; i < m; ++i) c[i] += c[i - 1];
    18     for(int i = 0; i < n; ++i) sa[--c[rank[i]]] = i;
    19     for(int k = 1; k < n; k <<= 1) {
    20         for(int i = 0; i < n; ++i) {
    21             int j = sa[i] - k;
    22             if(j < 0) j += n;
    23             tmp[c[rank[j]]++] = j;
    24         }
    25         int j = c[0] = sa[tmp[0]] = 0;
    26         for(int i = 1; i < n; ++i) {
    27             if(rank[tmp[i]] != rank[tmp[i - 1]] || rank[tmp[i] + k] != rank[tmp[i - 1] + k])
    28                 c[++j] = i;
    29             sa[tmp[i]] = j;
    30         }
    31         memcpy(rank, sa, n * sizeof(int));
    32         memcpy(sa, tmp, n * sizeof(int));
    33     }
    34 }
    35 
    36 void calheight() {
    37     for(int i = 0, k = 0; i < n; height[rank[i++]] = k) {
    38         if(k > 0) --k;
    39         int j = sa[rank[i] - 1];
    40         while(s[i + k] == s[j + k]) ++k;
    41     }
    42 }
    43 
    44 bool check(int L) {
    45     int cnt = 1;
    46     for(int i = 1; i < n; ++i) {
    47         if(height[i] >= L) {
    48             if(++cnt >= k) return true;
    49         } else {
    50             cnt = 1;
    51         }
    52     }
    53     return cnt >= k;
    54 }
    55 
    56 int solve() {
    57     int l = 1, r = n;
    58     while(l < r) {
    59         int mid = (l + r) >> 1;
    60         if(check(mid)) l = mid + 1;
    61         else r = mid;
    62     }
    63     return l - 1;
    64 }
    65 
    66 int main() {
    67     scanf("%d%d", &n, &k);
    68     for(int i = 0; i < n; ++i) scanf("%d", &s[i]), ++s[i];
    69     s[n++] = 0;
    70     makesa(MAXK);
    71     calheight();
    72     printf("%d
    ", solve());
    73 }
    View Code
  • 相关阅读:
    IOS开发-UIDynamic(物理仿真)简单使用
    IOS开发---视频录制
    利用阿里云服务器免费体验word press博客、个人网站
    Next Cloud通过修改数据库表,达到替换文件而不改变分享的链接地址的效果,以及自定义分享链接地址
    非华为笔记本如何实现多屏协同和一碰互传以及一些问题的解决方法
    如何申请XShell和XFtp的免费家庭学生版本
    PicGo配合Typora怎么配置Chevereto图床,PicGo的Chevereto图床配置
    Ubuntu无法正常引导,进不去Ubuntu,安装Ubuntu20和Window10双系统后,
    搭建自己的Chevereto免费图床—写博客更加得心应手了!
    如何搭建自己的本地服务器,Web服务器
  • 原文地址:https://www.cnblogs.com/oyking/p/3535257.html
Copyright © 2011-2022 走看看