算法实验一 TEST #2

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
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值