• poj3261 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.

    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

    Sample Input

    8 2
    1
    2
    3
    2
    3
    2
    3
    1

    Sample Output

    4

    题意:

    找一个最长的子串,他在原字符串中出现的次数至少k次。允许重叠。

    思路:

    二分枚举答案。对某一段i~j,如果height[i~j]都超过了mid,说明可以有一个长度为mid的串在这些后缀中都有出现过。也就是说这个串出现了j - i次,统计这个次数,如果超过k,st = mid + 1

    感觉应该hash也能做?没写过。【嗯不行不行,仔细想了一下hash好像要n方】

      1 #include <iostream>
      2 #include <set>
      3 #include <cmath>
      4 #include <stdio.h>
      5 #include <cstring>
      6 #include <algorithm>
      7 #include <vector>
      8 #include <queue>
      9 #include <map>
     10 using namespace std;
     11 typedef long long LL;
     12 #define inf 0x7f7f7f7f
     13 
     14 const int maxn = 1e6 + 5;
     15 int n, k;
     16 char str[maxn];
     17 
     18 int sa[maxn];
     19 int t1[maxn], t2[maxn], c[maxn];
     20 LL rnk[maxn], height[maxn];
     21 int cnt[maxn];
     22 
     23 void build_sa(int s[], int n, int m)
     24 {
     25     int i, j, p, *x = t1, *y = t2;
     26     for(i = 0; i < m; i++)c[i] = 0;
     27     for(i = 0; i < n; i++)c[x[i] = s[i]]++;
     28     for(i = 1; i < m; i++)c[i] += c[i - 1];
     29     for(i = n - 1; i >= 0; i--)sa[--c[x[i]]] = i;
     30     for(j = 1; j <= n; j <<= 1){
     31         p = 0;
     32         for(i = n - j; i < n; i++)y[p++] = i;
     33         for(i = 0; i < n; i++)if(sa[i] >= j)y[p++] = sa[i] - j;
     34         for(i = 0; i < m; i++)c[i] = 0;
     35         for(i = 0; i < n; i++)c[x[y[i]]]++;
     36         for(i = 1; i < m; i++)c[i] += c[i - 1];
     37         for(i = n - 1; i >= 0; i--)sa[--c[x[y[i]]]] = y[i];
     38         swap(x, y);
     39         p = 1;
     40         x[sa[0]] = 0;
     41         for(i = 1; i < n; i++)
     42             x[sa[i]] = y[sa[i - 1]] == y[sa[i]] && y[sa[i - 1] + j] == y[sa[i] + j] ? p - 1:p++;
     43         if(p >= n)break;
     44         m = p;
     45     }
     46 }
     47 
     48 void get_height(int s[], int n)
     49 {
     50     int i, j, k = 0;
     51     //cout<<"SA:"<<endl;
     52     for(i = 0; i <= n; i++){
     53         //cout<<sa[i]<<endl;
     54         rnk[sa[i]] = i;
     55     }
     56     for(i = 0; i < n; i++){
     57         if(k) k--;
     58         j = sa[rnk[i] - 1];
     59         while(s[i + k] == s[j + k])k++;
     60         height[rnk[i]] = k;
     61     }
     62 }
     63 
     64 bool check(int t)
     65 {
     66     int num = 1;
     67     for(int i = 2; i <= n; i++){
     68         if(height[i] >= t){
     69             num++;
     70             if(num >= k)return true;
     71         }
     72         else num = 1;
     73     }
     74     return false;
     75 }
     76 
     77 int s[maxn];
     78 int main()
     79 {
     80     while(scanf("%d%d", &n, &k) != EOF){
     81         //scanf("%s", str);
     82         int m = -inf;
     83         for(int i = 0; i < n; i++){
     84             scanf("%d", &s[i]);
     85             m = max(m, s[i]);
     86             cnt[i] = 0;
     87         }
     88         s[n] = cnt[n] = 0;
     89         build_sa(s, n + 1, m + 1);
     90         //cout<<1<<endl;
     91         get_height(s, n);
     92         //cout<<2<<endl;
     93 
     94         int st = 0, ed = n, ans;
     95         while(st <= ed){
     96             int mid = (st + ed) / 2;
     97             if(check(mid)){
     98                 st = mid + 1;
     99                 ans = mid;
    100             }
    101             else{
    102                 ed = mid - 1;
    103             }
    104         }
    105 
    106         printf("%d
    ", ans);
    107 
    108     }
    109     return 0;
    110 }
  • 相关阅读:
    WPF/MVVM 快速开始指南(译)
    Rose2003的安装和破解
    自定义vs代码段
    silverlight中Grid.ColumnDefinitions属性设置错误
    vs绑定和取消绑定项目和解决方案
    firefox浏览器中silverlight无法输入问题
    silverlight中当前上下文中不存在名称“InitializeComponent“错误
    inotify使用代码
    build android toochain on mac (gcc 4.4.3 compile)
    istat 一个不错的widget
  • 原文地址:https://www.cnblogs.com/wyboooo/p/9865584.html
Copyright © 2020-2023  润新知