(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--)
12     {
13         scanf("%s",s);
14         int len=strlen(s);
15         int loop,test;
16         for(loop=1;loop<=len;loop++)
17         {
18             if(len%loop==0)
19             {
20                 for(test=loop;test<=len;test++)
21                 if(s[test]!=s[test%loop]) break;
22                 if (test==len)
23                 {
24                     printf("%d\n",loop);
25                     break;
26                 }
27             }
28         }
29         if (t) printf("\n");
30     }
31     return 0;
32 }

时间: 2024-08-07 23:22:53

(UVA)455 --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 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

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 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

uva 1328 - Period (周期串的判断 kmp)

题意:给一个长为n的字符串,问字符串的前缀是不是周期串,如果是周期串,输出前缀的最后一个字母的位置和最短周期 思路:kmp字符串匹配的性质运用. 对于前i个字符,如果f[i]不等于零,说明在此字符串的前缀中,有一部分[0,f[i]]和本字符串[i-f[i],i]的这一部分是相同的.如果这i个字符组成一个周期串,那么错开的一部分[f[i],i]恰好是一个循环节.(换句话说,如果满足f[i]不等于零 && [f[i],i]是循环节这两点,就可以说明前i个字符组成一个周期串) code: #in

UVA - 455(周期串)

求一段长度为k的字符串的最小重复周期(k<=80),代码: 思路: 周期串长度n必然能被k整除,在这一条件下,对周期小于k/2的,进行验证,验证k/n-1次,取最先成功的重复周期长度或k为最终结果 小技巧: 如果题目要求"两结果之间"有换行,那么对于最后一个case,可利用以下代码高亮处的方式来处理,使代码更简洁美观巧妙. #include"iostream" #include"cctype" #include"cstring&q

E - Power Strings,求最小周期串

E - Power Strings Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2406 Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = &

uva 周期串(求模分组)

/*本题题意求出周期串的最短长度:eg : hohoho 那么此周期串的长度可以为2 (ho) 同时也可以为 6(hohoho)  最短长度当然就是2本题思路:    求出周期串, 必然存在一个周期, 设定一个循环, i代表周期的长度,从1递增,最长的长度为字符串的长度 s.size(), 通过周期 将 字符串分组分组的字符串长度必须为整数, 因此只用考虑整数长度的字符串, 如果存在字符串所有分组都与第一组的字符相同, 那么此时就满足题意要求,输出对应的的周期长度 也就是最短的周期串长度, 本题

UVa 10298 - Power Strings

题目:求一个串的最大的循环次数. 分析:dp,KMP,字符串.这里利用KMP算法. KMP的next函数是跳跃到最近的串的递归结构位置(串元素取值0 ~ len-1): 由KMP过程可知: 如果存在循环节,则S[0 ~ next[len]-1] 与 S[len-next[len] ~ len-1]相匹配: 则S[next[len] ~ len-1]就是循环节(且最小),否则next[len]为0: 因此,最大循环次数为len/(len-next[len]),最小循环节为S[next[len] ~