zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    自己的文件管理软件,想怎样分类管理就怎样分类
    给博客园发展支点招
    看看自己存在哪些的个人知识管理问题和烦恼呢?
    2008年个人知识管理软件测评[草稿]
    獲取某天是星期幾
    創建鏈接服務器
    在.NET中调用Oracle9i存储过程经验总结
    C#转换成VB.net或vb.net转换成C#
    話說我們家姚明
    解决winxp搜索不到文件的问题!
  • 原文地址:https://www.cnblogs.com/cyb123456/p/5769250.html
Copyright © 2011-2022 走看看