• (简单) 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/i);
        }
        
        return 0;
    }
    View Code
  • 相关阅读:
    Window 下配置ChromeDriver(简单4步完成)[转]
    selenium之 chromedriver与chrome版本映射表(更新至v2.46)[转]
    学习网站
    如何理解python中的类和方法(转)
    面试题整理20191127
    mysql 慢查询
    python学习--代码组织实例
    ubuntu下安装Matlab
    SkinPP for VC
    C++中的4个类型转换关键字
  • 原文地址:https://www.cnblogs.com/whywhy/p/4656411.html
Copyright © 2020-2023  润新知