UVa455 Periodic Strings (string cycle)

Description
If a string can be a string of length k is repeated many times, the cycle called the string to be k. For example, the string "abcabcabcabc" In cycle 3 (of course, he also 6,12 cycle, etc.).

Now you write a program, as seeking a minimum period no longer than 80 strings. Input a first input integer n, representatives of n sets of data.

Each set of data per line, is not more than a length of the string 80.
There is a blank line between two adjacent input. Output data output each integer k in a row, represents the minimum period of the string.
There should be a blank line between two adjacent output.
Sample Input
2 

abcabcabc 

HoHoHo
Sample Output
3

2


解答:
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
char s[100],a;
int l,n;
int solve()
{
    bool b=1;
    memset(s,0,sizeof(s));
    scanf("%s",&s);
    l=0;
       while(s[l]!=0) l++;
       for(int i=1;i<=l;i++)//长度
       {
           if(l%i==0)
            {
                b=1;
                for(int j=0;j<l;j+=i)
                {
                    int k;
                    for(k=0;k<i;k++)
                    {
                        if(s[j+k]!=s[k])
                        {
                            b=0;
                            break;
                        }
                        
                    }
                    if(b==0) break;
                }
                if(b==1) return i;
            }
               
    }
    return 0;
}
int main()
{
   scanf("%d",&n);
   while(n--)
   {
           int ans;
        ans=solve();
           if(n!=0) printf("%d\n\n",ans);
           else printf("%d\n",ans);
   }
}

Guess you like

Origin www.cnblogs.com/satans/p/11109052.html