REPEATS - 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: 4since a (4, 3)-repeat is found starting at the 5th character of the input string.
往後各能匹配到多遠,記這個總長度爲K,那麼這裏連續出現了K/L+1次。最後看最大值是多少。
於是,我們不妨先算一下,從r[i*j]開始,除匹配了k/i個重複子串,還剩餘了幾個字符,剩餘的自然是k%i個字符。如果說r[i*j]的前面還有i-k%i個字符完成匹配的話,這樣就相當於利用多餘的字符還可以再匹配出一個重複子串,於是我們只要檢查一下從r[i*j-(i-k%i)]和r[i*(j+1)-(i-k%i)]開始是否有i-k%i個字符能夠完成匹配即可,也就是說去檢查這兩個後綴的最長公共前綴是否比i-k%i大即可。
當然如果公共前綴不比i-k%i小,自然就不比i小,因爲後面的字符都是已經匹配上的,所以爲了方便編寫,程序裏面就直接去看是否會比i小就可以了。
/*Sherlock and Watson and Adler*/
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<list>
#include<bitset>
#include<cmath>
#include<complex>
#include<string>
#include<algorithm>
#include<iostream>
#define eps 1e-9
#define LL long long
#define PI acos(-1.0)
#define bitnum(a) __builtin_popcount(a)
using namespace std;
const int N = 5005;
const int M = 100005;
const int inf = 1000000007;
const int mod = 1000000007;
const int MAXN = 50005;
//rnk從0開始
//sa從1開始,因爲最後一個字符(最小的)排在第0位
//height從1開始,因爲表示的是sa[i - 1]和sa[i]
//倍增算法 O(nlogn)
int wa[MAXN], wb[MAXN], wv[MAXN], ws_[MAXN];
//Suffix函數的參數m代表字符串中字符的取值範圍,是基數排序的一個參數,如果原序列都是字母可以直接取128,如果原序列本身都是整數的話,則m可以取比最大的整數大1的值
//待排序的字符串放在r數組中,從r[0]到r[n-1],長度爲n
//爲了方便比較大小,可以在字符串後面添加一個字符,這個字符沒有在前面的字符中出現過,而且比前面的字符都要小
//同上,爲了函數操作的方便,約定除r[n-1]外所有的r[i]都大於0,r[n-1]=0
//函數結束後,結果放在sa數組中,從sa[0]到sa[n-1]
void Suffix(int *r, int *sa, int n, int m)
{
int i, j, k, *x = wa, *y = wb, *t;
//對長度爲1的字符串排序
//一般來說,在字符串的題目中,r的最大值不會很大,所以這裏使用了基數排序
//如果r的最大值很大,那麼把這段代碼改成快速排序
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];//統計不大於字符i的字符個數
for(i = n - 1; i >= 0; --i) sa[--ws_[x[i]]] = i;//計算字符排名
//基數排序
//x數組保存的值相當於是rank值
for(j = 1, k = 1; k < n; j *= 2, m = k)
{
//j是當前字符串的長度,數組y保存的是對第二關鍵字排序的結果
//第二關鍵字排序
for(k = 0, i = n - j; i < n; ++i) y[k++] = i;//第二關鍵字爲0的排在前面
for(i = 0; i < n; ++i) if(sa[i] >= j) y[k++] = sa[i] - j;//長度爲j的子串sa[i]應該是長度爲2 * j的子串sa[i] - j的後綴(第二關鍵字),對所有的長度爲2 * j的子串根據第二關鍵字來排序
for(i = 0; i < n; ++i) wv[i] = x[y[i]];//提取第一關鍵字
//按第一關鍵字排序 (原理同對長度爲1的字符串排序)
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];//按第一關鍵字,計算出了長度爲2 * j的子串排名情況
//此時數組x是長度爲j的子串的排名情況,數組y仍是根據第二關鍵字排序後的結果
//計算長度爲2 * j的子串的排名情況,保存到數組x
t = x;
x = y;
y = t;
for(x[sa[0]] = 0, i = k = 1; i < n; ++i)
x[sa[i]] = (y[sa[i - 1]] == y[sa[i]] && y[sa[i - 1] + j] == y[sa[i] + j]) ? k - 1 : k++;
//若長度爲2 * j的子串sa[i]與sa[i - 1]完全相同,則他們有相同的排名
}
}
int Rank[MAXN], height[MAXN], sa[MAXN], r[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++);
}
int n,minnum[MAXN][16];
void RMQ() //預處理 O(nlogn)
{
int i,j;
int m=(int)(log(n*1.0)/log(2.0));
for(i=1;i<=n;i++)
minnum[i][0]=height[i];
for(j=1;j<=m;j++)
for(i=1;i+(1<<j)-1<=n;i++)
minnum[i][j]=min(minnum[i][j-1],minnum[i+(1<<(j-1))][j-1]);
}
int Ask_MIN(int a,int b) //O(1)
{
int k=int(log(b-a+1.0)/log(2.0));
return min(minnum[a][k],minnum[b-(1<<k)+1][k]);
}
int calprefix(int a,int b)
{
a=Rank[a],b=Rank[b];
if(a>b)
swap(a,b);
return Ask_MIN(a+1,b);
}
char s[5];
int main()
{
int t,i,j,k,ans,Max;
scanf("%d",&t);
while(t--)
{
Max=1;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%s",s);
r[i]=s[0]-'a'+1;
}
r[i]=0;
Suffix(r,sa,n+1,3);
calheight(r,sa,n);
RMQ();
//枚舉重複長度
for(i=1;i<=n;i++)
{
//找到每一段
for(j=0;j+i<n;j+=i)
{
//先求出前綴
ans=calprefix(j,j+i);
k=j-(i-ans%i);
ans=ans/i+1;
if(k>=0&&calprefix(k,k+i)>=i)
ans++;
//printf("L=%d,R=%d\n",i,ans);
Max=max(Max,ans);
}
}
printf("%d\n",Max);
}
return 0;
}