1. 程式人生 > >週期串Plus(字串週期水題ZCMU1986)

週期串Plus(字串週期水題ZCMU1986)

1986: 週期串plus

Description

Input

多組測試資料,每組僅一行為一個僅有大寫字母組成的字串。

Output

對於每組資料輸出該字串的最小週期。

Sample Input

HOHO

Sample Output

2

思路:從1開始暴力找週期(最大隻找到len/2,防止asdfasd這類情況輸出週期為4)

每天水水題,不想做菜雞學長...

#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<map>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#define mod 998244353;
#define Max 0x3f3f3f3f;
#define Min 0xc0c0c0c0;
#define mst(a) memset(a,0,sizeof(a))
#define f(i,a,b) for(int i=a;i<b;i++)
using namespace std;
typedef long long ll;
const int maxn=100005;
char str[maxn];
int main(){
    ios::sync_with_stdio(false);
    while(cin>>str){
        int flag=1;
        int len=strlen(str);
        int i;
        for(i=1;i<=len/2;i++){
            flag=1;
            for(int j=0;j<len;j++){
                if(str[j]!=str[j%i]){
                    flag=0;
                    break;
                }
            }
            if(flag==1){
                break;
            }
        }
        if(flag==1){
                cout<<i<<endl;
            }
            else{
                cout<<len<<endl;
            }
        }
    return 0;
}