zoukankan      html  css  js  c++  java
  • (简单) 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
  • 相关阅读:
    正则,ant antd from验证input框只能输入数字
    React 实现简易轮播图
    Moment.js ,JavaScript 日期处理类库
    JavaScript中准确的判断数据类型--四种方法
    介绍:一款可以描绘圆圈进度条的jQuery插件(可用作统计图)
    给网页增加水印的方法,react
    IntelliJ IDEA创建web项目及异常问题解决
    CSS 代码是什么?(转)
    JSP入门:介绍什么是JSP和Servlet(转)
    INTELLIJ IDEA集成CHECKSTYLE(转)
  • 原文地址:https://www.cnblogs.com/whywhy/p/4656411.html
Copyright © 2011-2022 走看看