joj1873

 1873: Power Strings

Result TIME Limit MEMORY Limit Run Times AC Times JUDGE
10s 8192K 640 254 Standard
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).

Each test case is a line of input representing s, a string of printable characters. For each s you should print the largest n such that s = a^n for some string a. 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.

Sample Input

abcd
aaaa
ababab
.

Output for Sample Input

1
4
3



这不是一道难题,只要找到字符串循环的最小单位就行了



#include<stdio.h>
#include<string.h>
char str[1000005];
int len;
bool exam(int u,int v)
{
    int add=v-u;
   // printf("%d  %d   %d\n",u,v,add);
    if(len%add!=0)
    return false;
    for(int i=u;i<v;i++)
    {
        int t=i+add;
      //  printf("%d  %d\n",u,v);
        while(t<len)
        {
            if(str[i]!=str[t])
            return false;
            t=t+add;
      //      printf("%d  %d\n",u,v);
        }
    }
    return true;
}
int  find()
{
    for(int j=1;str[j]!='\0';j++)
    {
        if(str[0]==str[j])
        {
            if(exam(0,j))
            return j;
        }
    }
    return len;
}
int main()
{
    //freopen("in.txt","r",stdin);
    while(gets(str))
    {
        if(str[0]=='.')
        break;
        len=strlen(str);
        int m=find();
        printf("%d\n",len/m);
    }
    return 0;
}

你可能感兴趣的:(joj1873)