poj 2406
题意:给出一个串,问你这个最多是多少个相同的字串重复连接而成的。如:ababab则最多有3个ab连接而成。
代码:
#include<iostream>
#include<fstream>
using namespace std;
char c[1000005];
int next[1000004];
int len;
void get(){
int i,j,k;
j=0;k=-1;next[0]=-1;
while(j<len)
if(k==-1||c[j]==c[k])
next[++j]=++k;
else
k=next[k];
}
void read(){
// ifstream cin("in.txt");
int i,j,k;
while(scanf("%s",c)!=EOF)
{
if(c[0]=='.') return;
len=strlen(c);
get();
if(len%(len-next[len])==0)
cout<<len/(len-next[len])<<endl;
else
cout<<1<<endl;
}
}
int main(){
read();
return 0;
}
浙公网安备 33010602011771号