• UVa455


    455 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

    题意:

           给出一个字符串,找出它的最大循环节字符串长度。

    输入:

           情况数T,然后是一个空行,之后每一种情况给一个字符串,相邻两情况间有空行。

    输出:

           每种情况输出它的最大循环节字符串长度。

    分析:

           简单模拟,编写一个函数检验输入的字符串是否是某个长度的字符串的重复。然后列举输入字符串长度的所有因子,检查字符串是否是为某个因子长度的字符串的重复即可。

     1 #include <cstdio>
     2 #include <iostream>
     3 #include <cstring>
     4 #include <cmath>
     5 using namespace std;
     6 const int MAX_LEN = 80;
     7 char str[90];
     8 bool IS_THIS_STRING_LOOPING_IN_THIS_NUMBER(char str[],int loop_num,int len){
     9     for(int i = 0 ; i < loop_num ; i++)
    10         for(int j = i + loop_num ; j < len ; j += loop_num)
    11             if(str[i] != str[j]) return false;
    12     return true;
    13 }
    14 int main(){
    15     int T; cin >> T;
    16     while(T--){
    17         int ans;
    18         getchar();
    19         scanf("%s",str);
    20         int len = strlen(str);
    21         for(int i = 1 ; i <= len ; i++){
    22             if(len % i == 0){
    23                 if(IS_THIS_STRING_LOOPING_IN_THIS_NUMBER(str,i,len)){
    24                     ans = i;
    25                     break;
    26                 }
    27             }
    28         }
    29         printf("%d
    ",ans);
    30         if(T != 0) cout << endl;
    31     }
    32     return 0;
    33 }
    View Code
  • 相关阅读:
    高斯过程回归
    第一行代码读书笔记3+错误分析
    多项式各种操作
    [BZOJ3625] [Codeforces Round #250]小朋友和二叉树
    [BZOJ2055] 80人环游世世界
    [BZOJ3698] XWW的难题
    [BZOJ3456] 城市规划
    分治FFT
    [BZOJ5306] [HAOI2018]染色
    [BZOJ3380] [USACO2004 Open]Cave Cows 1 洞穴里的牛之一
  • 原文地址:https://www.cnblogs.com/cyb123456/p/5769250.html
Copyright © 2020-2023  润新知