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

*/


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值