UVA 455 Periodic Strings(字符串的循环节)

来源:互联网 发布:中国f1车队知乎 编辑:程序博客网 时间:2024/05/22 17:48

 

 

UVA 455 Periodic Strings

A character stringis said to have periodk if it can be formed by concatenating one or morerepetitions of anotherstring of lengthk. For example, the string”abcabcabcabc” has period 3, since it is formed by 4 repetitionsof the string ”abc”. Italso has periods 6 (tworepetitions of ”abcabc”) and 12 (one repetition of ”abcabcabcabc”).

Write a program to read a character stringand determine its smallest period.

 

Input

The first lineoif the inputfile will contain a single integer N indicating how many test casethat your program will test followed by a blank line. Each test case will containa single characterstring of up to 80 non-blank characters.Two consecutive input will separated by a blankline.

 

Output

An integerdenoting the smallest period of the inputstring for each input.Two consecutive output are separated by a blank line.

 

Sample Input

1

 

HoHoHo

 

Sample Output

2


题意:

输出字符串的最小循环节

思路:

卡的是输出格式,应该是字符串的处理问题。

AC CODE:

#include<stdio.h>#include<cstring>#include<algorithm>#define HardBoy main()#define ForMyLove return 0;using namespace std;const int MYDD = 1103;int HardBoy {int n, kc = 0;scanf("%d", &n);while(n--) {getchar();char cv[MYDD];scanf("%s", cv);int lencv = strlen(cv);for(int i = 1; i <= lencv; i++) {if(lencv % i == 0) {/*当前长度被整除才可能为循环节*/int flag = 1;for(int j = i; j < lencv; j++) {if(cv[j] != cv[j%i]) {flag = 0;/*前面若为循环节,依次判断后面的*/break;}}if(flag) {printf("%d\n", i);break;}}}if(n)printf("\n");/*输出的格式调整*/}ForMyLove}/*61231231231233*/


0 0
原创粉丝点击