1. 程式人生 > >UVA 455 Periodic Strings(字串的迴圈節)

UVA 455 Periodic Strings(字串的迴圈節)

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
}
/*
6
123123123123
3

*/