Language:Default Power Strings
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. Sample Input abcd aaaa ababab . Sample Output 1 4 3 |
//#include<bits/stdc++.h>
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
int n;
int nextt[1000005];
char a[1000005];
void get_next(){
int i=0,j=-1;
nextt[0]=-1;
while(i<=n){
if(j==-1||a[i]==a[j]){
i++;
j++;
nextt[i]=j;
}
else
j=nextt[j];
}
}
int main(){
int m,i,j,k,ans,len;
k=1;
while(~scanf("%s",a)&&a[0]!='.'){
n=strlen(a);
get_next();
len=n-nextt[n]; //循环节长度
if(n%len==0)
printf("%d\n",n/len);
else
printf("1\n");
}
return 0;
}