C - Power Strings KMP

C - Power Strings

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <deque>
#include <bitset>
#define ll              long long
#define pii             pair<int, int>
#define rep(i,a,b)      for(ll  i=a;i<=b;i++)
#define dec(i,a,b)      for(ll  i=a;i>=b;i--)
#define forn(i, n)      for(ll i = 0; i < int(n); i++)
using namespace std;
int dir[4][2] = { { 1,0 },{ 0,1 } ,{ 0,-1 },{ -1,0 } };
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
const double pi = 3.14159265358979323846;
const double eps = 1e-6;
const int mod = 1e7 + 7;

inline ll read()
{
    ll x = 0; bool f = true; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
    while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
    return f ? x : -x;
}
inline ll gcd(ll m, ll n)
{
    return n == 0 ? m : gcd(n, m % n);
}
void exgcd(ll A, ll B, ll& x, ll& y)
{
    if (B) exgcd(B, A % B, y, x), y -= A / B * x; else x = 1, y = 0;
}

bool prime(int x) {
    if (x < 2) return false;
    for (int i = 2; i * i <= x; ++i) {
        if (x % i == 0) return false;
    }
    return true;
}
inline int qpow(int x, ll n) {
    int r = 1;
    while (n > 0) {
        if (n & 1) r = 1ll * r * x % mod;
        n >>= 1; x = 1ll * x * x % mod;
    }
    return r;
}

const int N = 1e6+5;
char s[N];
int pext[N];
int len;
void getnxt(char *s,int *next)
{
    int k=-1;
    int j=0;
    pext[j]=-1;
    while(j<len)
    {
        if(k==-1 || s[j]==s[k])
        {
            k++;
            j++;
            pext[j]=k;
        }
        else
        {
            k=pext[k];
        }
    }
}

int main()
{
    while(scanf("%s",s))
    {
        if(strcmp(s,".")==0)
            break;
        int sum=0;
        len=strlen(s);
        getnxt(s,pext);
        int z;
        z=len-pext[len];
        if(len%z==0)
        {
            for(int i=1;i<=len;i++)
            {
                if(i%z==0)
                    sum++;
            }
            printf("%d\n",sum);
        }
        else
        {
            printf("1\n");
        }
    }
    return 0;
}

 

posted @ 2020-07-21 17:39  DeaL57  阅读(131)  评论(0编辑  收藏  举报