• 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
  • 相关阅读:
    window下安装jupyter,ipython的方法
    虚拟机中,安装VM tools的小发现
    Perl 与 Python 之间的一些异同
    perl中的grep函数介绍
    基因芯片(Affymetrix)分析1:芯片质量分析
    议员是如何投票的?
    社会网络分析:探索人人网好友推荐系统
    支持向量机(一)
    主成分分析(Principal components analysis)-最小平方误差解释
    因子分析(Factor Analysis)
  • 原文地址:https://www.cnblogs.com/oyking/p/3535257.html
Copyright © 2020-2023  润新知