Problem
给一个长度 N 的字符串 S ,求一个最长子串 S1 的长度, S1 是由某一个子串 S2 重复 k 次 (k>=1) 而得到的,比如“abaabaabaaba”是由“aba”重复4次得到。
Limits
TimeLimit(ms):1985
MemoryLimit(MB):NoLimit
N∈[1,5×104]
字符集∈[a,b]
Look up Original Problem From here
Solution
模版题。枚举 S2 的长度 L ,每次访问 0,L,2L,… 的相邻两个位置,求出往前往后最大匹配值,即为答案。
Complexity
TimeComplexity:O(N/1+N/2+…+N/N)=O(N×lnN)
MemoryComplexity:O(N)
My Code
#include<cstdio>
#include<iostream>
#include<sstream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<cctype>
#include<ctime>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
typedef long double ld;
#define peter cout<<"i am peter"<<endl
#define input freopen("data.txt","r",stdin)
#define randin srand((unsigned int)time(NULL))
#define INT (0x3f3f3f3f)*2
#define LL (0x3f3f3f3f3f3f3f3f)*2
#define gsize(a) (int)a.size()
#define len(a) (int)strlen(a)
#define slen(s) (int)s.length()
#define pb(a) push_back(a)
#define clr(a) memset(a,0,sizeof(a))
#define clr_minus1(a) memset(a,-1,sizeof(a))
#define clr_INT(a) memset(a,INT,sizeof(a))
#define clr_true(a) memset(a,true,sizeof(a))
#define clr_false(a) memset(a,false,sizeof(a))
#define clr_queue(q) while(!q.empty()) q.pop()
#define clr_stack(s) while(!s.empty()) s.pop()
#define rep(i, a, b) for (int i = a; i < b; i++)
#define dep(i, a, b) for (int i = a; i > b; i--)
#define repin(i, a, b) for (int i = a; i <= b; i++)
#define depin(i, a, b) for (int i = a; i >= b; i--)
#define pi 3.1415926535898
#define eps 1e-6
#define MOD 1000000007
#define MAXN 150100
#define N
#define M 20
int wa[MAXN],wb[MAXN],wv[MAXN],wss[MAXN],sa[MAXN],r[MAXN];
inline int cmp(int *r,int a,int b,int l){
return r[a]==r[b] && r[a+l]==r[b+l];
}
inline void build_sa(int *r,int *sa,int n,int m){
int i,j,p,*x=wa,*y=wb,*t;
rep(i,0,m) wss[i]=0;
rep(i,0,n) wss[x[i]=r[i]]+=1;
rep(i,1,m) wss[i]+=wss[i-1];
depin(i,n-1,0) sa[--wss[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;
rep(i,0,n) if(sa[i]>=j) y[p++]=sa[i]-j;
rep(i,0,n) wv[i]=x[y[i]];
rep(i,0,m) wss[i]=0;
rep(i,0,n) wss[wv[i]]+=1;
rep(i,1,m) wss[i]+=wss[i-1];
depin(i,n-1,0) sa[--wss[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++;
}
}
int ranking[MAXN],height[MAXN];
inline void build_height(int *r,int *sa,int n){
int i,j,k=0;
repin(i,1,n) ranking[sa[i]]=i;
for(i=0;i<n;height[ranking[i++]]=k){
for(k?k--:0,j=sa[ranking[i]-1];r[i+k]==r[j+k];k++);
}
}
int dp[MAXN][M];
inline void RMQ_Init(int n){
repin(i,2,n){
dp[i][0]=height[i];
}
for(int j=1;1<<j<=n;j+=1){
for(int i=2;i+j-1<=n;i++){
dp[i][j]=min(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
}
}
}
int logx[MAXN];
inline int RMQ_Query(int l,int r){
int a=ranking[l],b=ranking[r];
if(a>b) swap(a,b);
a+=1;
int k=logx[b-a+1];
return min(dp[a][k],dp[b-(1<<k)+1][k]);
}
int n,lens,midl,ansk;
char s[MAXN];
int main(){
for(int i=0;1<<i<MAXN;i++){
logx[1<<i]=i;
}
rep(i,1,MAXN){
if(!logx[i]) logx[i]=logx[i-1];
}
int T;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
lens=0;
rep(i,0,n){
scanf(" %c",&s[lens++]);
}
midl=lens;
s[lens++]='#';
depin(i,midl-1,0){
s[lens++]=s[i];
}
s[lens]='\0';
repin(i,0,lens){
r[i]=static_cast<int>(s[i]);
}
build_sa(r,sa,lens+1,'b'+1);
build_height(r,sa,lens);
RMQ_Init(lens);
ansk=1;
repin(len,1,midl){
for(int i=len;i<midl;i+=len){
int p1=i-len,p2=i;
if(s[p1]!=s[p2]) continue;
int p4=lens-1-p2,p3=lens-1-p1;
int len1=RMQ_Query(p1,p2),len2=RMQ_Query(p3,p4);
int maxilen=len1+len2-1;
int k=maxilen/len+1;
ansk=max(ansk,k);
}
}
printf("%d\n",ansk);
}
}