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 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如果一个字符串可以由某个长度为k的字符串重复多次得到,则称该串以k为周期。例如,abcabcabc以3为周期(注意,它也以6和9为周期)输入一个长度不超过80的字符串,输出其最小周期。
#include<stdio.h>
#include<string.h>
char s[100];
int main()
{
    int t,ok;
    scanf("%d",&t);
    while(t--){
        scanf("%s",s);
        int n=strlen(s);
        for(int i=1;i<=n;i++){ //周期
            if(n%i==0){  //周期必须能够整除字符串长度,如ababab的周期为2,字符串长度为6,6%2=0,或者abcd的周期为4,字符串长度为4,4%4=0;
                ok=true;
                for(int j=i;j<n;j++){
                if(s[j]!=s[j%i]){ //根据周期比较字符串
                 ok=false;
                 break;
                }
            }
            if(ok){
                printf("%d\n",i);
                break;
               }
            }
        }
        if(t!=0)
            printf("\n");
    }
    return 0;
}
				
时间: 2024-10-13 16:52:13

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

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

【周期串】NYOJ-1121 周期串

[题目链接:NYOJ-1121] 例如:abcabcabc 该字符串的长度为9,那么周期串的长度len只可能为{1,3,9},否则就不可能构成周期串. 接下来,就是要在各周期间进行比较.描述不清...自己走一遍就懂了. 1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 const int MAXN = 202; 5 char s[MAXN]; 6 int main(){ 7 while((cin &g

周期串-java

import java.util.Scanner;/** * 周期串 * @author NEU-2015 * */public class Demo {    public static void main(String[] args) {        Scanner input = new Scanner(System.in);        String str = null;        boolean flag = true;          while (input.hasNe

[华为机试练习题]1.周期串问题

题目一[周期串问题] 如果一个字符串可以由某个长度为k的字符串重复多次得到,我们说该串以k为周期.例如,abcabcabcabc以3为周期(注意,它也可以6和12为周期,结果取最小周期3).字符串的长度小于等于100,由调用者保证. 接口说明 原型: int GetMinPeriod(char *inputstring); 输入参数: char * inputstring:字符串 返回值: int 字符串最小周期 代码一 /*-----------------------------------

HDU 1358 (所有前缀中的周期串) Period

题意: 给出一个字符串,在所有长度大于1的前缀中,求所有的周期至少为2的周期串,并输出一个周期的长度以及周期的次数. 分析: 有了上一题 HDU 3746 的铺垫,这道题就很容易解决了 把next求出来,然后逐个判断即可. 1 #include <cstdio> 2 #include <cstring> 3 4 const int maxn = 1000000 + 10; 5 char p[maxn]; 6 int n, next[maxn]; 7 8 void get_next(

算法竞赛入门经典 5.1.3 周期串

5.1.3  周期串 如果一个字符串可以由某个长度为k的字符串重复多次得到,我们说该串以为周期.例如,abcabcabcabc以3为周期(注意,它也以6和12为周期).输入一个长度不超过80的串,输出它的最小周期. 样例输入:HoHoHo 样例输出:2 #include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc, char *argv[]) { char s[100]; sca

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

WLS的周期串(C++)

WLS的周期串 难度级别:A: 运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 如果一个字符串可以由某个长度为k的字符串重复多次得到,我们说该串以k为周期.例如,abcabcabcabc以3为周期(注意,它也以6和12为周期).输入一个长度不超过1000的串输出它的最小周期. 输入 一个由常用字符组成的字符串 输出 输入字符串的最小周期 输入示例 abcabcabcabc 输出示例 3 #include <cstdio> #include

算法篇——最小周期串

来源:<算法竞赛入门经典>例题5.1.3 题目:如果一个字符串可以由某个长度为k的字符串重复多次得到,我们说该串以k为周期.例如,abcabcabcabc以3为周期(注意,它也以6和12为周期).输入一个长度不超过80的串,输出它的最小周期. 样例输入:HoHoHo 样例输出:2 分析:题目中说过,字符串可能会有多个周期.但因为只需求出最小的一个,可以从小到大枚举各个周期,一旦符合条件就立即输出. 源码: #include<stdio.h> #include<string.h