Power Strings

Power Strings

TimeLimit: 1 Second   MemoryLimit: 32 Megabyte

Totalsubmit: 1791   Accepted: 528  

Description

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.

Output

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
.

Sample Output

1
4
3

思路:由于题目要求的是最大值,因此从n开始向下查找,第一次出现的满足条件的那个数就是最大的,查找就可以结束,如果查找到1是仍未找到合适的值,则为1,就是说不是任何字符串的次方如abcd

#include<stdio.h>
#include<string.h>
#define N 1000001
int str_judge(int n,int i,char * pstr)
{
int j,k,p = 0;
if(n%i==0)
{

for(j = 0;j<n-i;j++)
{
if(*(pstr+j)==*(pstr+j+i))
p = 1;
else
return 0;
}
return p;
}
else
return 0;
}

int main()
{
int counter,k,len_str;
char str[N]={'0'};
while(strcmp(str,".")!=0)
{
counter = 0;
scanf("%s",str);
getchar();
len_str = strlen(str);
for(k = 1;k<len_str;k++)
{
if(str_judge(len_str,k,str)==1)
{
printf("%d ",len_str/k);
counter++;
break;
}
}
if(counter==0&&strcmp(str,".")!=0)
printf("%d ",1);
}
}

原文地址:https://www.cnblogs.com/anhuizhiye/p/3316100.html