POJ 3261 Milk Patterns 及其变形(后缀数组)

Milk Patterns
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 7337   Accepted: 3331
Case Time Limit: 2000MS

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 ≤ KN) 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次最长重复子串,论文上有,注意这道题的between 0 and 1,000,000 inclusive,10^6,

  1 #include <cstdio>
  2 #include <iostream>
  3 #include <cstdlib>
  4 #include <algorithm>
  5 #include <cstring>
  6 #include <string>
  7 using namespace std;
  8 
  9 const int maxn=1010000,maxl=maxn-2000,maxm=1010000;
 10 int wss[maxn],wa[maxn],wb[maxn],wv[maxn];
 11 int r[maxm],sa[maxm],rank[maxm],h[maxm];
 12 int n,m,t,maxx,minn,he,mid,kk,x;
 13 
 14 
 15 void close()
 16 {
 17 }
 18 
 19 int cmp(int *r,int x,int y,int l)
 20 {
 21     return (r[x]==r[y] && r[x+l]==r[y+l]);
 22 }
 23 
 24 void da(int n,int m,int *ws)
 25 {
 26     int i,j,*t,*x=wa,*y=wb,p;
 27     for (i=0;i<m;i++) ws[i]=0;
 28     for (i=0;i<n;i++) ws[x[i]=r[i]]++;
 29     for (i=1;i<m;i++) ws[i]+=ws[i-1];
 30     for (i=n-1;i>=0;i--) sa[--ws[x[i]]]=i;
 31     for (j=1,p=1;p<n;j*=2,m=p)
 32     {
 33         for (p=0,i=n-j;i<n;i++) y[p++]=i;
 34         for (i=0;i<n;i++) if (sa[i]>=j) y[p++]=sa[i]-j;
 35         for (i=0;i<n;i++) wv[i]=x[y[i]];
 36         for (i=0;i<m;i++) ws[i]=0;
 37         for (i=0;i<n;i++) ws[wv[i]]++;
 38         for (i=1;i<m;i++) ws[i]+=ws[i-1];
 39         for (i=n-1;i>=0;i--) sa[--ws[wv[i]]]=y[i];
 40         for (t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;i<n;i++)
 41             x[sa[i]]=cmp(y,sa[i],sa[i-1],j)?p-1:p++;
 42     }
 43 }
 44 
 45 void callheight(int n)
 46 {
 47     int i,j,k=0;
 48     for (i=1;i<=n;i++) rank[sa[i]]=i;
 49     for (i=0;i<n;h[rank[i++]]=k)
 50         for (k?k--:0,j=sa[rank[i]-1];r[i+k]==r[j+k];k++);
 51 }
 52 
 53 bool check(int k)
 54 {
 55     minn=sa[1];maxx=sa[1];
 56     int cnt=0;
 57     for (int i=2;i<=n;i++)
 58     {
 59         if (h[i]>=k)
 60         {
 61             cnt++;
 62             if (cnt+1>=kk)
 63                 return true;
 64         }
 65         else
 66         {
 67             cnt=0;
 68         }
 69     }
 70     return false;
 71 }
 72 
 73 void work()
 74 {
 75     he=0;t=n;
 76     while (he<=t)
 77     {
 78 //        printf("he:%d mid:%d t:%d\n",he,mid,t);
 79         mid=(he+t)>>1;
 80         if (check(mid))
 81             he=mid+1;
 82         else t=mid-1;
 83     }
 84     printf("%d\n",t);
 85 }
 86 
 87 void init ()
 88 {
 89     while(scanf("%d %d",&n,&kk)!=EOF)
 90      {
 91          for (int i=0;i<n;i++)
 92          {
 93              scanf("%d",&x);
 94              r[i]=x+97;
 95          }
 96          r[n]=0;
 97              da(n+1,maxl,wss);
 98              callheight(n);
 99              work();
100      }
101 }
102 
103 int main ()
104 {
105     init();
106     close();
107     return 0;
108 }


接下来的一道题
Time Limit:
10000ms
Memory Limit:
262144kB
Description

    给一个字符集很大的字符串。用数字表示字符。求出其中至少重复K(2 ≤ K ≤ N)次的最长子串(可重叠)。例如在“1 2 3 2 3 2 3 1”中“2 3 2 3”重复了二次。

Input
第一行:二个整数,N和K。(N<10^5,2 ≤ K ≤ N)
第2...N+1行:每行一个整数C。(0≤C≤10^9
Output
一个整数,重复了至少K次的子串的长度。
Sample Input
8 2
1
2
3
2
3
2
3
1
Sample Output
4
思路:m的值达到了10^9,会RT的,所以需要先对原数组排一道序,所以我用了map。。。。。
  1 #include <cstdio>
  2 #include <iostream>
  3 #include <cstdlib>
  4 #include <algorithm>
  5 #include <cstring>
  6 #include <string>
  7 #include <map>
  8 using namespace std;
  9 
 10 const int maxn=100100,maxm=maxn;
 11 int wss[maxn],wa[maxn],wb[maxn],wv[maxn];
 12 int r[maxm],sa[maxm],rank[maxm],h[maxm];
 13 int n,m,t,maxx,minn,he,mid,kk,x,a[maxn];
 14 
 15 map<int,int> mp;
 16 
 17 void close()
 18 {
 19 }
 20 
 21 int cmp(int *r,int x,int y,int l)
 22 {
 23     return (r[x]==r[y] && r[x+l]==r[y+l]);
 24 }
 25 
 26 void da(int n,int m,int *ws)
 27 {
 28     int i,j,*t,*x=wa,*y=wb,p;
 29     for (i=0;i<m;i++) ws[i]=0;
 30     for (i=0;i<n;i++) ws[x[i]=r[i]]++;
 31     for (i=1;i<m;i++) ws[i]+=ws[i-1];
 32     for (i=n-1;i>=0;i--) sa[--ws[x[i]]]=i;
 33     for (j=1,p=1;p<n;j*=2,m=p)
 34     {
 35         for (p=0,i=n-j;i<n;i++) y[p++]=i;
 36         for (i=0;i<n;i++) if (sa[i]>=j) y[p++]=sa[i]-j;
 37         for (i=0;i<n;i++) wv[i]=x[y[i]];
 38         for (i=0;i<m;i++) ws[i]=0;
 39         for (i=0;i<n;i++) ws[wv[i]]++;
 40         for (i=1;i<m;i++) ws[i]+=ws[i-1];
 41         for (i=n-1;i>=0;i--) sa[--ws[wv[i]]]=y[i];
 42         for (t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;i<n;i++)
 43             x[sa[i]]=cmp(y,sa[i],sa[i-1],j)?p-1:p++;
 44     }
 45 }
 46 
 47 void callheight(int n)
 48 {
 49     int i,j,k=0;
 50     for (i=1;i<=n;i++) rank[sa[i]]=i;
 51     for (i=0;i<n;h[rank[i++]]=k)
 52         for (k?k--:0,j=sa[rank[i]-1];r[i+k]==r[j+k];k++);
 53 }
 54 
 55 bool check(int k)
 56 {
 57     minn=sa[1];maxx=sa[1];
 58     int cnt=0;
 59     for (int i=2;i<=n;i++)
 60     {
 61         if (h[i]>=k)
 62         {
 63             cnt++;
 64             if (cnt+1>=kk)
 65                 return true;
 66         }
 67         else
 68         {
 69             cnt=0;
 70         }
 71     }
 72     return false;
 73 }
 74 
 75 void work()
 76 {
 77     he=0;t=n;
 78     while (he<=t)
 79     {
 80 //        printf("he:%d mid:%d t:%d\n",he,mid,t);
 81         mid=(he+t)>>1;
 82         if (check(mid))
 83             he=mid+1;
 84         else t=mid-1;
 85     }
 86     printf("%d\n",t);
 87 }
 88 
 89 void init ()
 90 {
 91     while(scanf("%d %d",&n,&kk)!=EOF)
 92      {
 93          for (int i=0;i<n;i++)
 94          {
 95              scanf("%d",&x);
 96              r[i]=x+97;
 97              a[i]=r[i];
 98          }
 99          sort(a,a+n);
100          for (int i=0;i<n;i++)
101              mp[a[i]]=i;
102          for (int i=0;i<n;i++)
103          {
104              r[i]=mp[r[i]]+3;
105          }
106          r[n]=0;
107              da(n+1,maxn,wss);
108              callheight(n);
109              work();
110      }
111 }
112 
113 int main ()
114 {
115     init();
116     close();
117     return 0;
118 }
原文地址:https://www.cnblogs.com/cssystem/p/2962076.html