TEST # 2
Description: Given two strings X and Y, we define that X*Y to be their concatenation. For instance, if X = “abc” and Y=”def” then X*Y = “abcdef” and X*X = “abcabc”. In the same way as multiplication, we could define exponentiation as: X^0=”” (the empty string) and X^(n+1)=X*(X^n).
Input: The input is consisted of one or more test cases, each of which is a line representing s, a string of printable characters. The length of s will be at least 1 and will not exceed 100 characters. A line containing a period(.) follows the last test case.
Output: For each s you should print the largest n such s = a^n for some string a.
Sample Input:
abcd
aaaa
ababab
. Sample Output:
1
4
3
/*******************************************
*项目:算法实验一 TEST 2
*姓名:卿*
*学号:SA14226***
*******************************************/
#include "stdio.h"
#include "string.h"
#define false 0
#define true 1
/*
函数名:compare
输入:字符数组buf[],比较轮次n
功能;针对特定的划分对字符数组buf中的字符串进行比较,并判断是否符合要求
*/
int compare(char buf[],int n){
int i,j,len,temp2;
int count;
double temp;
len = strlen(buf);
count = 0;
if(len%n) //判断是否满足整数次划分
return false;
else //在满足整数次划分的前提下进行比较
{
for(i = 1;i < len/n;i++){ //做划分并逐个字符比较并计数
for(j = 0;j < n;j++){
if(buf[j] == buf[i*n+j])
count++;
}
}
if (count == (len-n)) //总计数等于数组元素个数时表示划分成功
{
return true;
}
else
return false;
}
}
int main(){
int i,j,len,n;
char buf[100];
while(1){
scanf("%s",buf); //读取字符串
if(!strcmp(buf,".")){ //退出条件判断
exit();
}
len = strlen(buf);
if(len == 1) //字符串长度为1
printf("1\n");
for(i = 1;i <= len/2;i++){ //划分单位大于等于1开始划分
//划分数目大于等于2时最大划分单位为len/2
if(compare(buf,i)){ //依次判断各种划分可能性
printf("%d\n", len/i);
break;
}
if((i+1) > len/2) //大于等于2的划分数目均不成功
printf("1\n"); //无法划分,输出划分数量为1
}
}
}