Power Strings
Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of concatenation as multiplication, exponentiation by a non-negative integer is defined in the normal way: a^0 = "" (the empty string) and a^(n+1) = a*(a^n).
Input
Each test case is a line of input representing s, a string of printable characters. The length of s will be at least 1 and will not exceed 1 million characters. A line containing a period follows the last test case.
Output
For each s you should print the largest n such that s = a^n for some string a.
Sample Input
abcd aaaa ababab .
Sample Output
1 4 3
Hint
This problem has huge input, use scanf instead of cin to avoid time limit exceed.
题目链接:http://poj.org/problem?id=2406
题目大意:给出一个字符串L是一个连续重复串,问最大重复次数是多少
思路:字符串L是由子串S重复R次得到的,枚举S的长度k,判断S是否满足条件。首先判断L的长度是否能被S的长度k整除,再看suffix(1)和suffix(k+1)的LCP是否等于n-k。线性预处理出所有后缀与suffi(1)的LCP
代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define ll long long
#define F(x) ((x)/3+((x)%3==1?0:tb))
#define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)
const int N=2000010;
int wa[N*3],wb[N*3],wv[N*3],wss[N*3];//所有相关数组都要开到3倍
int rak[N*3],h[N*3],a[N*3],sa[N*3];
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++) wss[i]=0;
for(i=0;i<n;i++) wss[wv[i]]++;
for(i=1;i<m;i++)wss[i]+=wss[i-1];
for(int i=n-1;i>=0;i--) b[--wss[wv[i]]]=a[i];
}
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++];
}
void da(int a[],int sa[],int rak[],int h[],int n,int m)
{
for(int i=n;i<3*n;i++) a[i]=0;
dc3(a,sa,n+1,m);
int i,j,k=0;
for(int i=0;i<=n;i++) rak[sa[i]]=i;
for(int i=0;i<n;i++)
{
if(k) k--;
int j=sa[rak[i]-1];
while(a[i+k]==a[j+k])k++;
h[rak[i]]=k;
}
}
char s[N];
int b[N];
int main()
{
while(~scanf("%s",s))
{
if(s[0]=='.')break;
int n=strlen(s);
for(int i=0;i<n;i++)a[i]=s[i];
a[n]=0;
da(a,sa,rak,h,n,127);
int x=rak[0];
b[x]=n;
for(int i=x;i>=2;i--) b[i-1]=min(b[i],h[i]);
for(int i=x+1;i<=n;i++) b[i]=min(b[i-1],h[i]);
int ans=0;
for(int i=1;i<n;i++)
{
if(n%i!=0) continue;
if(b[rak[i]]==n-i)
{
ans=n/i;
break;
}
}
if(ans==0) ans=1;
printf("%d\n",ans);
}
return 0;
}
来源:https://blog.csdn.net/chimchim04/article/details/99693282