Result | TIME Limit | MEMORY Limit | Run Times | AC Times | JUDGE |
---|---|---|---|---|---|
3s | 8192K | 1256 | 350 | Standard |
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 input consists of several test cases, each test case has a single character string of up to 80 non-blank characters.
Output
An integer denoting the smallest period of the input string.
Sample Input
HoHoHo
Sample Output
2
#include <cstring>
#include <cstdio>
#include <iostream>
using namespace std;
int main ()
{
char ss[1000];
while(cin>>ss)
{
int k; //記錄最小週期
int flag=1; //記錄是否有
for(int i=1;i<=strlen(ss);i++)
{
flag=1;
char a[1000];
int j;
for(j=0;j<i;j++)
a[j]=ss[j];
a[j]='/0';
for(j=i;j<strlen(ss);j+=i)
{
for(int m=0;m<i;m++)
if(a[m]!=ss[j+m]) {flag=0; break;}
if(!flag) break;
}
if(flag)
{
k=i;
break;
}
}
cout<<k<<endl;
}
return 0;
}