您的位置:首页 > 其它

joj1873

2011-09-16 22:28 274 查看




1873:
Power Strings

ResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE

10s8192K640254Standard
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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: