zoukankan      html  css  js  c++  java
  • POJ 1961 2406 (KMP,最小循环节,循环周期)

    关于KMP的最短循环节、循环周期,请戳:

    http://www.cnblogs.com/chenxiwenruo/p/3546457.html (KMP模板,最小循环节)

    POJ 2406  Power Strings

    题意:
    给一个字符串,问这个字符串是否能由另一个字符串重复R次得到,求R的最大值。

    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    /*
    题意:
    给一个字符串,问这个字符串是否能由另一个字符串重复R次得到,求R的最大值。
    */
    using namespace std;
    const int maxn=1000005;
    int next[maxn];
    char str[maxn];
    void getnext(char *str,int len){
        next[0]=-1;
        int i=0,j=-1;
        while(i<len){
            if(j==-1||str[i]==str[j]){
                i++;
                j++;
                next[i]=j;
            }
            else
                j=next[j];
        }
    }
    int main()
    {
        while(scanf("%s",str)!=EOF){
            if(str[0]=='.')
                break;
            int len=strlen(str);
            getnext(str,len);
            int l=len-next[len];
            if(len%l==0)
                printf("%d
    ",len/l);
            else
                printf("1
    ");
        }
        return 0;
    }
    View Code

    POJ 1961  Period

    给你一个字符串,对于它长度为i的前缀(i=2~n),给出对应的循环周期k,即前缀可由某个字符串拼接k次得到(k>1)
    2406的加强版

    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    /*
    给你一个字符串,对于它长度为i的前缀(i=2~n),给出对应的循环周期k,即前缀可由某个字符串拼接k次得到(k>1)
    2406的加强版
    */
    using namespace std;
    const int maxn=1000005;
    int next[maxn];
    char str[maxn];
    int n;
    void getnext(char*str,int len){
        next[0]=-1;
        int i=0,j=-1;
        while(i<len){
            if(j==-1||str[i]==str[j]){
                i++;j++;
                next[i]=j;
            }
            else
                j=next[j];
        }
    }
    int main()
    {
        int cases=0;
        while(scanf("%d",&n),n){
            scanf("%s",str);
            getnext(str,n);
            printf("Test case #%d
    ",++cases);
            for(int i=2;i<=n;i++){
                int l=i-next[i];
                if(i%l==0 && i/l>1){
                    printf("%d %d
    ",i,i/l);
                }
            }
            printf("
    ");
        }
        return 0;
    }
    View Code
  • 相关阅读:
    Spring@Profile注解
    day 32 子进程的开启 及其用法
    day 31 udp 协议SOCK_DGRAM
    day 30 客户端获取cmd 命令的步骤
    day 29 socket 理论
    day 29 socket 初级版
    有关 组合 继承
    day 27 多态 接口 类方法 静态方法 hashlib 摘要算法模块
    新式类和经典类的区别
    day 28 hasattr getattr serattr delattr 和带__内置__ 类的内置方法
  • 原文地址:https://www.cnblogs.com/chenxiwenruo/p/3546675.html
Copyright © 2011-2022 走看看