概述
687. RepeatsProblem code: REPEATS |
A string s is called an (k,l)-repeat if s is obtained by concatenating k>=1 times some seed string t with length l>=1. For example, the string
s = abaabaabaaba
is a (4,3)-repeat with t = aba as its seed string. That is, the seed string t is 3 characters long, and the whole string s is obtained by repeating t 4 times.
Write a program for the following task: Your program is given a long string u consisting of characters ‘a’ and/or ‘b’ as input. Your program must find some (k,l)-repeat that occurs as substring within u with k as large as possible. For example, the input string
u = babbabaabaabaabab
contains the underlined (4,3)-repeat s starting at position 5. Since u contains no other contiguous substring with more than 4 repeats, your program must output the maximum k.
Input
In the first line of the input contains H- the number of test cases (H <= 20). H test cases follow. First line of each test cases is n - length of the input string (n <= 50000), The next n lines contain the input string, one character (either ‘a’ or ‘b’) per line, in order.
Output
For each test cases, you should write exactly one interger k in a line - the repeat count that is maximized.
Example
Input: 1 17 b a b b a b a a b a a b a a b a b Output: 4
后缀数组先穷举长度L,然后求长度为L 的子串最多能连续出现几次。首先连续出现 1 次是肯定可以的,所以这里只考虑至少2 次的情况。假设在原字符串中连续出 现2 次,记这个子字符串为S,那么S 肯定包括了字符r[0], r[L], r[L*2], r[L*3], ……中的某相邻的两个。所以只须看字符r[L*i]和r[L*(i+1)]往前和 往后各能匹配到多远,记这个总长度为K,那么这里连续出现了K/L+1 次。最后 看最大值是多少。如图7 所示。 穷举长度L 的时间是n,每次计算的时间是n/L。所以整个做法的时间复杂 度是O(n/1+n/2+n/3+……+n/n)=O(nlogn)。
因为是隔段枚举,所以,有可能在k%l!=0的时候,这样,会有可能,向前移几格,可能刚好能多加一个,所以,再加一个判断就可以了!
#include <iostream> #include <stdio.h> #include <math.h> #include <string.h> using namespace std; #define maxn 200500 int wa[maxn],wb[maxn],ws[maxn],wv[maxn],wsd[maxn],r[maxn],ans[maxn]; char str[maxn]; #define M 100010 int dp[M][18]; #define F(x) ((x)/3+((x)%3==1?0:tb)) #define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2) int c0(int *r,int a,int b) {return r[a]==r[b]&&r[a+1]==r[b+1]&&r[a+2]==r[b+2];} int c12(int k,int *r,int a,int b) {if(k==2) return r[a]<r[b]||r[a]==r[b]&&c12(1,r,a+1,b+1); else return r[a]<r[b]||r[a]==r[b]&&wv[a+1]<wv[b+1];} void sort(int *r,int *a,int *b,int n,int m) { int i; for(i=0;i<n;i++) wv[i]=r[a[i]]; for(i=0;i<m;i++) wsd[i]=0; for(i=0;i<n;i++) wsd[wv[i]]++; for(i=1;i<m;i++) wsd[i]+=wsd[i-1]; for(i=n-1;i>=0;i--) b[--wsd[wv[i]]]=a[i]; return; } void dc3(int *r,int *sa,int n,int m) { int i,j,*rn=r+n,*san=sa+n,ta=0,tb=(n+1)/3,tbc=0,p; r[n]=r[n+1]=0; for(i=0;i<n;i++) if(i%3!=0) wa[tbc++]=i; sort(r+2,wa,wb,tbc,m); sort(r+1,wb,wa,tbc,m); sort(r,wa,wb,tbc,m); for(p=1,rn[F(wb[0])]=0,i=1;i<tbc;i++) rn[F(wb[i])]=c0(r,wb[i-1],wb[i])?p-1:p++; if(p<tbc) dc3(rn,san,tbc,p); else for(i=0;i<tbc;i++) san[rn[i]]=i; for(i=0;i<tbc;i++) if(san[i]<tb) wb[ta++]=san[i]*3; if(n%3==1) wb[ta++]=n-1; sort(r,wb,wa,ta,m); for(i=0;i<tbc;i++) wv[wb[i]=G(san[i])]=i; for(i=0,j=0,p=0;i<ta && j<tbc;p++) sa[p]=c12(wb[j]%3,r,wa[i],wb[j])?wa[i++]:wb[j++]; for(;i<ta;p++) sa[p]=wa[i++]; for(;j<tbc;p++) sa[p]=wb[j++]; return; } 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++) wsd[i]=0; for(i=0;i<n;i++) wsd[x[i]=r[i]]++; for(i=1;i<m;i++) wsd[i]+=wsd[i-1]; for(i=n-1;i>=0;i--) sa[--wsd[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++) wsd[i]=0; for(i=0;i<n;i++) wsd[wv[i]]++; for(i=1;i<m;i++) wsd[i]+=wsd[i-1]; for(i=n-1;i>=0;i--) sa[--wsd[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; } void makermq(int n,int b[]) { int i,j; for(i=1;i<=n;i++) dp[i][0]=b[i]; for(j=1;(1<<j)<=n;j++) for(i=1;i+(1<<j)-1<=n;i++) dp[i][j]=min(dp[i][j-1],dp[i+(1<<(j-1))][j-1]); } int rmq(int s,int v) { s=rank[s],v=rank[v]; if(s>v)swap(s,v); s++; int k=(int)(log((v-s+1)*1.0)/log(2.0)); return min(dp[s][k],dp[v-(1<<k)+1][k]); } int solve(int n){ int i,k,tmp,j,maxx=1,l,t; for(l=1;l<n;l++) for(i=0;i+l<n;i=i+l){ k=rmq(i,i+l); t=k/l+1; tmp=i-(l-k%l); if(tmp>=0&&(k%l!=0)&&rmq(tmp,tmp+l)>=k) t++; if(t>maxx){ maxx=t; } } printf("%dn",maxx); } int main() { int n,n1,i,tcase; scanf("%d",&tcase); while(tcase--){ scanf("%d",&n); getchar(); char c; for(i=0;i<n;i++){ c=getchar(); getchar(); r[i]=(int)c+1; } int l=n; r[n]=0; int m=300; //da(r,ans,n+1,m); dc3(r,ans,n+1,m); calheight(r,ans,n); makermq(n,height); int maxx=solve(n); } return 0; }
最后
以上就是义气发卡为你收集整理的spoj687. Repeats687. Repeats的全部内容,希望文章能够帮你解决spoj687. Repeats687. Repeats所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复