poj 2406

来源:互联网 发布:写歌软件下载 编辑:程序博客网 时间:2024/05/17 02:58


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

abcdaaaaababab.

Sample Output

143
我的代码
# 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. }  

原创粉丝点击