UVa455 Periodic Strings

#include <stdio.h>
#include <string.h>

int main()
{
    int T, k, len;
    char str[81], *p, *q, *end;
    scanf("%d", &T);
    while (T--)
    {
        scanf("%s", str);
        len = strlen(str);
        end = str+len;
        for (k = 1; k < len; ++k)
        {
            p = str;
            q = str + k;
            while (p < end && q < end)
            {
                if (*p != *q)
                    break;
                ++p, ++q;
            }
            if (q == end)
            {
                q = str;
                while (p < end && q < end && *p++ == *q++);
                if (p == end)
                    break;
            }
        }

printf("%d\n", k);
        if (T) putchar(‘\n‘);
    }

return 0;
}

时间: 2024-10-20 23:58:11

UVa455 Periodic Strings的相关文章

UVa 455 Periodic Strings (周期串)

Periodic Strings Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description  Periodic Strings  A character string is said to have period k if it can be formed by concatenating one or more repetitions of anothe

UVa OJ 455 Periodic Strings

 Periodic Strings  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 o

周期串(Periodic Strings,UVa455)

解题思路: 对一个字符串求其最小周期长度,那么,最小周期长度必定是字符串长度的约数,即最小周期长度必定能被字符串长度整除 其次,对于最小周期字符串,每位都能对应其后周期字串的每一位, 即 ABC  ABCABC (345678)->%其字串长度3 012  3%3 4%3 5%3  6%3 7%3  8%3   0      1     2        0      1       2 if(a[j]!=a[j%3])说明不对应,不是周期,进行下一位扫描. AC Code: #include<

D - Periodic Strings(UVA-455)

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 "

(UVA)455 --Periodic Strings(周期串)

题目链接:http://vjudge.net/problem/UVA-455 可以从1开始枚举周期,对后面的字符逐个测试. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 6 int main() 7 { 8 int t; 9 char s[100]; 10 scanf("%d",&t); 11 while(t--)

UVa 455 - Periodic Strings 解题报告

1.题目大意 求一个长度不超过80的字符串的最小周期. 2.思路 非常简单,基本就是根据周期的定义做出来的,几乎不需要过脑. 3.应该注意的地方 (1) 最后输出的方式要注意,不然很容易就PE了.不过个人认为,其实这题Sample Output给的不好 (2) 注意输出的要求是最小周期 4.代码 #include"stdio.h" #include"string.h" #define maxn 80 int main() { int T,m,i,j,flag; ch

Periodic Strings(周期串)

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 "

UVa 455 Periodic Strings

题意:给出一个字符串,找出它的最小的周期,枚举从1到len的周期,看是否满足. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 7 char s[105]; 8 9 int main() 10 { 11 int ncase,t,i,k,j,len; 12 scanf("%d"

周期字符串[UVA-455]

Periodic Strings  UVA - 455 https://vjudge.net/problem/UVA-455 书上第三章的习题3-4.题目要求判断一个给定的串的最小周期,题目保证了串的长度不大于80,因此使用朴素的暴力穷举法就可以解决.根据题意,第一个周期必然从第一个字符开始,因此只要用一个指针,从前往后扫,那么这个指针将整个串分割为两个字串,只要验证第一个字串是不是满足周期性即可.验证周期性可以依次扫描第二个字串,判断对应字符是否相等即可.我这里的对应使用了求余的方法.C++实