Description
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.
题意大概就是给你一个字符串,然后让你找它的循环节。输出最小循环节的次数。字符串的长度是e6。
这个题目可以用KMP的next数组的性质搞,或者直接就是后缀数组套上去。
1.先讲KMP的next的做法:
令next[i]=
x
。字符串的长度为len。则表示从这个串从
这是一个很好的性质。那么如果说循环节是x的话,那么对于所有的
k∗x<len,(k∈N)
必须有
kx−next[kx]=x
。
而我们要求循环节最多的,那么就意味着
x
越小越好。一旦遇到合法的,就直接输出就好了。
代码如下:
最坏的复杂度是O(
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXN 1000000
int nxt[MAXN+10];
int s[MAXN+10];
char t[MAXN+10];
void getNext(char *t,int len)
{
nxt[0]=-1;
int now=-1;
for(int i=1; i<=len; i++) {
while(now>=0&&t[i]!=t[now+1])
now=nxt[now];
nxt[i]=now+1;
now++;
}
}
int solve(int m)
{
for(int i=1; i<=m; i++) {
if(m%i==0) {
bool ok=true;
nxt[i]=0;
for(int j=i; j<=m&&ok; j+=i)
if(j-nxt[j]!=i)
ok=false;
if(ok)
return i;
}
}
return m;
}
int main()
{
int T;
scanf("%d",&T);
while(1) {
scanf("%s",t+1);
if(t[1]=='.')
break;
int m=strlen(t+1);
getNext(t,m);
int ans=solve(m);
printf("%d\n",m/ans);
}
return 0;
}
后缀数组的做法:
我们可以得到如果循环节为
x
,则必须要有
在此感谢模板的主人kuangbin。这个题目
nlogn
的过不了,只能用DC3。而DC3好长。所以还是写KMP那个方法就好了吧。
后缀数组的代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN = 1000010;
#define F(x) ((x)/3+((x)%3==1?0:tb))
#define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)
int wa[MAXN*3],wb[MAXN*3],wv[MAXN*3],wss[MAXN*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(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;
int *rn = r + n;
int *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 str[],int sa[],int rank[],int height[],int n,int m=256)
{
for(int i = n; i < n*3; i++) str[i] = 0;
dc3(str, sa, n+1, m);
int i,j,k = 0;
for(i = 0; i <= n; i++)rank[sa[i]] = i;
for(i = 0; i < n; i++) {
if(k) k--;
j = sa[rank[i]-1];
while(str[i+k] == str[j+k]) k++;
height[rank[i]] = k;
}
}
char s[MAXN*3];
int sa[MAXN*3];
int Rank[MAXN*3];
int height[MAXN*3];
int ss[MAXN*3];
void solve(int len)
{
for(int i=1;i<=len;i++)
{
if(len%i)
continue;
if(Rank[i]+1!=Rank[0])
continue;
if(height[Rank[0]]!=len-i)
continue;
printf("%d\n",len/i);
return ;
}
printf("1\n");
return ;
}
int main (void)
{
while(scanf("%s",s)!=EOF) {
if(s[0]=='.')
break;
int len=strlen(s);
for(int i=0;i<len;i++)
ss[i]=s[i]-'a'+1;
ss[len]=0;
da(ss,sa,Rank,height,len+1);
solve(len);
}
return 0;
}