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
  • 相关阅读:
    eclipse注释乱码的一个小问题
    《java 编程思想》 读书笔记 (一)
    《java 编程思想》 读书笔记 (一)
    055_SSM——sprinMVC的返回值
    054_jQuary——html()与text()方法的区别?
    053_Servlet——resp.getWrite()与resp.getPrint()的区别?
    052_html——html转环成jsp出现乱码的问题?
    051_html——checkbox的默认值与工作属性
    050_SSM——SpringMVCV中的各个器都起了什么作用?
    047_SSM——为什么返回String使用StringHttpMessageConverter解析时会造成乱码?
  • 原文地址:https://www.cnblogs.com/oyking/p/3535257.html
Copyright © 2011-2022 走看看