题目来源:http://poj.org/problem?id=3261
题目分类:后缀数组
此题心得: 熟悉后缀数组的使用+单调队列处理
时间:2011-7-21
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 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 Source |
[Submit] [Go Back] [Status] [Discuss]
All Rights Reserved 2003-2011 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator
题意与分析
题意:题意模型很简单,就是给你一个串,求其中最少重复k次的子串(可以重叠),而且要求其长度最长的一个。
分析:很明显求重复字串题要用到后缀树组,先用后缀数组求出其height数组。然后我们可以理解,height数组的每个值都是两个字串的最长公共前缀,则我们维护k-1个值中的最小值就是这k个串的最长公共前缀,然后用单调队列维护height数组中连续(k-1)个的最小值,求出最大的一个。
此题也可以二分枚举子串长度。。。
源代码
· #include<stdio.h>
· #include<string.h>
·
· #define maxn 1000001
· int wa[maxn],wb[maxn],wv[maxn],ws[maxn];
· int cmp(int *r,int a,int b,int l)
· {
· return r[a]==r[b]&&r[a+l]==r[b+l];
· }
· void da(int *r,int *sa,int n,int m)
· {
· int i,j,p,*x=wa,*y=wb,*t;
· for(i=0;i<m;i++) ws[i]=0;
· for(i=0;i<n;i++) ws[x[i]=r[i]]++;
· for(i=1;i<m;i++) ws[i]+=ws[i-1];
· for(i=n-1;i>=0;i--) sa[--ws[x[i]]]=i;
· for(j=1,p=1;p<n;j*=2,m=p)
· {
· for(p=0,i=n-j;i<n;i++) y[p++]=i;
· for(i=0;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j;
· for(i=0;i<n;i++) wv[i]=x[y[i]];
· for(i=0;i<m;i++) ws[i]=0;
· for(i=0;i<n;i++) ws[wv[i]]++;
· for(i=1;i<m;i++) ws[i]+=ws[i-1];
· for(i=n-1;i>=0;i--) sa[--ws[wv[i]]]=y[i];
· for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;i<n;i++)
· x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
· }
· return;
· }
· int rank[maxn],height[maxn];
· void calheight(int *r,int *sa,int n)
· {
· int i,j,k=0;
· for(i=1;i<=n;i++) rank[sa[i]]=i;
· for(i=0;i<n;height[rank[i++]]=k)
· for(k?k--:0,j=sa[rank[i]-1];r[i+k]==r[j+k];k++);
· return;
· }
· int RMQ[maxn];
· int mm[maxn];
· int best[20][maxn];
· void initRMQ(int n)
· {
· int i,j,a,b;
· for(mm[0]=-1,i=1;i<=n;i++)
· mm[i]=((i&(i-1))==0)?mm[i-1]+1:mm[i-1];
· for(i=1;i<=n;i++) best[0][i]=i;
· for(i=1;i<=mm[n];i++)
· for(j=1;j<=n+1-(1<<i);j++)
· {
· a=best[i-1][j];
· b=best[i-1][j+(1<<(i-1))];
· if(RMQ[a]<RMQ[b]) best[i][j]=a;
· else best[i][j]=b;
· }
· return;
· }
· int askRMQ(int a,int b)
· {
· int t;
· t=mm[b-a+1];b-=(1<<t)-1;
· a=best[t][a];b=best[t][b];
· return RMQ[a]<RMQ[b]?a:b;
· }
· int lcp(int a,int b) //最长公共前缀
· {
· int t;
· a=rank[a];b=rank[b];
· if(a>b) {t=a;a=b;b=t;}
· return(height[askRMQ(a+1,b)]);
· }
·
· //求一个串任意两后缀最长公共前缀示例。。。RMQ实现。。
· int len1, len2, n, k, mx;
· char s[maxn];
· int sa[maxn], a[maxn];
· int q[maxn];
· int main()
· {
· int i, j;
· int head, tail, ans;
· while(scanf("%d%d", &n, &k)!=EOF)
· {
· for(i=0; i<n; i++)
· {
· scanf("%d", &a[i]);
· a[i]++;
· }
· a[n] = 0;
· da(a, sa, n+1, 200);
· calheight(a, sa, n);
· /*
· for(i=1; i<=n; i++)
· {
· printf("%d ", height[i]);
· }
· printf("\n");
· */
· head = tail = 0;
· k--;
· ans = 0;
· for(i=1; i<=n; i++)
· {
· while(head<tail && height[i]<height[q[tail-1]])
· tail--;
· q[tail++] = i;
· while(i-q[head]+1>k)
· head++;
· if(i>=k-1 && height[q[head]]>ans)
· ans = height[q[head]];
· }
· printf("%d\n", ans);
· }
· return 0;
· }