(简单) POJ 2406 Power Strings,扩展KMP。

Description

Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of concatenation as multiplication, exponentiation by a non-negative integer is defined in the normal way: a^0 = "" (the empty string) and a^(n+1) = a*(a^n).

 

  水题,扩展KMP。

 

代码如下:

// ━━━━━━神兽出没━━━━━━
//      ┏┓       ┏┓
//     ┏┛┻━━━━━━━┛┻┓
//     ┃           ┃
//     ┃     ━     ┃
//     ████━████   ┃
//     ┃           ┃
//     ┃    ┻      ┃
//     ┃           ┃
//     ┗━┓       ┏━┛
//       ┃       ┃
//       ┃       ┃
//       ┃       ┗━━━┓
//       ┃           ┣┓
//       ┃           ┏┛
//       ┗┓┓┏━━━━━┳┓┏┛
//        ┃┫┫     ┃┫┫
//        ┗┻┛     ┗┻┛
//
// ━━━━━━感觉萌萌哒━━━━━━

// Author        : WhyWhy
// Created Time  : 2015年07月18日 星期六 10时54分33秒
// File Name     : 2406.cpp

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>

using namespace std;

const int MaxN=1000006;

void EKMP_pre(int m,char s[],int next1[])
{
    int p=0,a=1,L;

    next1[0]=m;
    
    while(p+1<m && s[p]==s[p+1])
        ++p;

    next1[1]=p;

    for(int k=2;k<m;++k)
    {
        L=next1[k-a];
        p=next1[a]+a-(next1[a]!=0);

        if(k+L-1<p)
            next1[k]=L;
        else
        {
            ++p;

            while(p<m && s[p]==s[p-k])
                ++p;

            next1[k]=p-k;
            a=k;
        }
    }

    next1[m]=0;
}

int N;
char s[MaxN];
int next1[MaxN];

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

    while(~scanf("%s",s))
    {
        if(s[0]=='.' && s[1]==0)
            break;

        N=strlen(s);

        EKMP_pre(N,s,next1);

        int i;

        for(i=1;i<N;++i)
            if(next1[i]+i==N && next1[i]%i==0)
                break;

        printf("%d\n",N/i);
    }
    
    return 0;
}
View Code

 

posted @ 2015-07-18 11:03  WhyWhy。  阅读(247)  评论(0编辑  收藏  举报