3.4 Periodic Strings UVA455

来源:互联网 发布:不用端口号访问服务器 编辑:程序博客网 时间:2024/06/16 21:24

这题我记得是记录每个和首字母相同的字母的位置,从首字母一一的和以后的字母匹配,大概就是比如abcabcabcabc就位置1和4比较,然后2和5,然后3和6

有个bug就是abcababcabab这样的也能扫描到最后,所以最后卡了个倍数

A character string is said to have period k if it can be formed by concatenating one or more repetitions of another string of length k. For example, the string ”abcabcabcabc” has period 3, since it is formed by 4 repetitions of the string ”abc”. It also has periods 6 (two repetitions of ”abcabc”) and 12 (one repetition of ”abcabcabcabc”). Write a program to read a character string and determine its smallest period.

Input

The first line oif the input file will contain a single integer N indicating how many test case that your program will test followed by a blank line. Each test case will contain a single character string of up to 80 non-blank characters. Two consecutive input will separated by a blank line.

Output

An integer denoting the smallest period of the input string for each input. Two consecutive output are separated by a blank line.

Sample Input

1
HoHoHo

Sample Output

2

#include <stdio.h>#include <string.h>int main(){    int N;        char str[85] = {'\000'};        char *i = NULL, *k = NULL, *m = NULL, *sum[85];        int ans = 0, j = 0, len;    scanf("%d",&N);    while(N--)    {        memset(str,'\000', sizeof(str));        ans = j = len = 0;        int aa = 85;        while(aa--) sum[aa] = NULL;        scanf("%s",str);        len = strlen(str);        for(i = str; *i != '\000'; i++)        {            if( *(i+1) == str[0] ) sum[ans++] = (i + 1);        }        if(ans==0)        {            printf("%d",len);printf(N?"\n\n":"\n");            continue;        }        for(m = k = sum[j],i = str; *k != '\000'; )        {            if(*i != *k&& m==sum[ans-1])            {                {printf("%d",len);printf(N?"\n\n":"\n");}                break;            }            if(*i != *k)            {                m = k = sum[++j];                i = str;            }            else            {                i++;                k++;            }        }        if(*k=='\000')        {                if(len%(m-str) != 0) {printf("%d",len);printf(N?"\n\n":"\n");}                else   {printf("%d",m-str);printf(N?"\n\n":"\n");}        }    }    return 0;}

年轻就是好,还用指针写,现在看到这题就觉得直接倍数暴力过多省事。。。老了老了

原创粉丝点击