poj 2406


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<stdio.h># include<string.h>
char ss[1000002];
int next[1000002],len;
void getNext()
{
int i=1,j=0;
next[1]=0;
while(i<len)
{
if(j==0||ss[i]==ss[j])
{
i++;j++;
next[i]=j;
}
   else
  j=next[j];
}
}
int main()
{
while(1)
{
int i,j,seg,sum=0;
scanf("%s",ss+1);
len=strlen(ss+1);
if(len==1&&ss[1]=='.')
break;
getNext();
seg=len-next[len];
for(i=seg;i<=len-seg;i+=seg)////拖沓。。。。
if(ss[i]!=ss[i+seg])
{


sum=1;
break;
}
if(len%seg==0&&sum==0)
printf("%d\n",len/seg);
else
printf("%d\n",1);

}
return 0;

}

、、大神的代码!!!!!

  1. #include<stdio.h>  
  2. #include<string.h>  
  3. #define max 1000000  
  4. int next[max];  
  5. char str1[max];  
  6. int get_next(char *pat)  
  7. {  
  8.     int j=0,k=-1;  
  9.     int len=strlen(pat);  
  10.     next[0]=-1;  
  11.     while(j<len)  
  12.     {  
  13.         if(k==-1||pat[j]==pat[k])  
  14.             next[++j]=++k;  
  15.         else  
  16.             k=next[k];  
  17.     }  
  18.     j=len-k;//如果最后一个位置不匹配,那么就会滚到len-k的位置,也就是最小重复字串的长度。  
  19.     if(len%j==0)  
  20.         return len/j;  
  21.     else  
  22.         return 1;  
  23. }  
  24. int main()  
  25. {  
  26.     while(scanf("%s",&str1)!=EOF)  
  27.     {  
  28.         if(str1[0]=='.')  
  29.             break;  
  30.         printf("%d\n", get_next(str1));  
  31.     }  
  32.     return 0;  
  33. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值