UVa455 Periodic Strings(周期串)

Description

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 "abc". It also has periods 6 (two repetitions of "abcabc") and 12 (one repetition of "abcabcabcabc").

Write a program to read a character string and determine its smallest period.

Input

The first line oif the input file will contain a single integer N indicating how many test case that your program will test followed by a blank line. Each test case will contain a single character string of up to 80 non-blank characters. Two consecutive input will separated by a blank line.

Output

An integer denoting the smallest period of the input string for each input. Two consecutive output are separated by a blank line.

Sample Input

1

HoHoHo

Sample Output

2

代码如下:

#include 
#include 
using namespace std;
int main()
{
	int t;scanf("%d",&t);int len,yes;
	char a[100];
	getchar();
	while(t--)
	{
		scanf("%s",a);
		len=strlen(a);
		for(int i=1;i<=len;i++)
		{
			if(len%i==0) //假设周期为i开始试探 
			{
				yes=1;
				for(int j=i;j

思路:假设周期为i,从周期i=1开始逐渐试探。

博客已搬:洪学林博客

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Hoaresky1998/article/details/49936997
文章标签: ACM C++ 编程 uva
个人分类: ACM之路->字符串
相关热词: uva455 uva455题目
上一篇环状序列UVa1584紫苏第三章
下一篇POJ1936-All-in-All(子序列)
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭