zoukankan      html  css  js  c++  java
  • UVa 455 Periodic Strings

    题意:给出一个字符串,找出它的最小的周期,枚举从1到len的周期,看是否满足。

     1 #include<iostream>  
     2 #include<cstdio>  
     3 #include<cstring>  
     4 #include<algorithm>  
     5 using namespace std;
     6 
     7 char s[105];
     8 
     9 int main()
    10 {
    11     int ncase,t,i,k,j,len;
    12     scanf("%d",&ncase);
    13     while(ncase--)
    14     {
    15         scanf("%s",s);len=strlen(s);
    16         
    17         for(t=1;t<len;t++)//枚举周期从1到len1来找最小的循环节 
    18         {
    19             if(len%t) continue;//如果不能整除的话就不是周期 
    20             int flag=1;
    21             for(j=0,k=0;j<len;k=k%t)
    22             if(s[j++]!=s[k++]) flag=0;
    23             
    24             if(flag) break;
    25         }
    26         
    27         printf("%d
    ",t);
    28         if(ncase!=0)    printf("
    ");
    29     }
    30 }
    View Code
  • 相关阅读:
    POJ 2154
    POJ 1286
    Polycarp's problems
    Greedy Change
    Goods transportation
    Ugly Problem
    Happy Matt Friends
    Dense Subsequence
    Ray Tracing
    Batch Sort
  • 原文地址:https://www.cnblogs.com/wuyuewoniu/p/4307200.html
Copyright © 2011-2022 走看看