ZOJ 3199 Longest Repeated Substring(后缀数组)
Write a program that takes a string and returns length of the longest repeated substring. A repeated substring is a sequence of characters that is immediately followed by itself.
For example, given "Mississippi", the longest repeated substring is "iss" or "ssi" (not "issi").
Given "Massachusetts", the longest repeated substring would be either "s" or "t".
Given "Maine", the longest repeated substring is "" (the empty string).
Input
The first line of the input contains a single integer T , the number of test cases.
Each of the following T lines, is exactly one string of lowercase charactors.
The length of each string is at most 50000 characters.
Output
For each test case, print the length of the Longest Repeated Substring.
Sample Input
2 aaabcabc ab
Sample Output
3 0
#include<iostream> #include<stdio.h> #include<string.h> #include<cmath> using namespace std; #define max(x,y) (x)>(y)? (x):(y) #define min(x,y) (x)<(y)? (x):(y) #define Max 100020 char s[Max]; int sa[Max], Rank[Max], height[Max]; int wa[Max], wb[Max], wd[Max]; void build_sa(int n, int m){ // 倍增算法 n为总长度,n=l+1, m为字符范围 int i,j,p,*xy, *x = wa, *y = wb; for(i = 0; i < m; i ++) wd[i] = 0; for(i = 0; i < n; i ++) wd[x[i]=s[i]] ++; for(i = 1; i < m; i ++) wd[i] += wd[i-1]; for(i = n-1; i >= 0; i --) sa[-- wd[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 < m; i ++) wd[i] = 0; for(i = 0; i < n; i ++) wd[x[y[i]]] ++; for(i = 1; i < m; i ++) wd[i] += wd[i-1]; for(i = n-1; i >= 0; i --) sa[-- wd[x[y[i]]]] = y[i]; xy=x;x=y;y=xy; p=1;x[sa[0]]=0; for(i=1;i<n;i++){ x[sa[i]] = (y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+j]==y[sa[i]+j])?p-1:p++; } } } void getHeight(int n){ // 求height数组。 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];s[i+k]==s[j+k];k++); } //sa[1~~l]为有效值 sa[i]=a则代表排在第 i 位的是第a个后缀。 a属于[0~~l-1] //Rank[0~~l-1]是有效值 Rank[i]=b则代表第 i 个后缀排在第b位 b属于[1~~l] //height[2~~l]是有效值 height[i]=c 则代表排在第 i 位的后缀和排在第i-1的后缀的最长前缀长度是c int main(){ int l,i,ans,T,temp; scanf("%d",&T); while(T--){ scanf("%s",s); l=strlen(s); build_sa(l+1,200); getHeight(l); ans=0; for(i=2;i<=l;i++){ temp=height[i]; if(abs(sa[i]-sa[i-1])<=temp) ans=max(ans,abs(sa[i]-sa[i-1])); for(int j=i+1;j<=l;j++){ if(!height[j]||temp<=ans) break; temp=min(temp,height[j]); if(abs(sa[j]-sa[i-1])<=temp) ans=max(ans,abs(sa[j]-sa[i-1])); } } printf("%d\n",ans); } return 0; } /* hei sa rank 1 0 aaabcabc 2 1 2 1 aabcabc 1 4 3 5 abc 3 -3 4 2 abcabc 0 4 5 6 bc 2 -3 6 3 bcabc 0 0 7 7 c 1 -4 8 4 cabc */