• POJ 2406 Power Strings KMP循环节+升级版 POJ 1961 Period


    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).Input

    Each test case is a line of input representing s, a string of printable characters. The length of s will be at least 1 and will not exceed 1 million characters. A line containing a period follows the last test case.

    Output

    For each s you should print the largest n such that s = a^n for some string a.

    Sample Input

    abcd
    aaaa
    ababab
    .
    

    Sample Output

    1
    4
    3
    

    Hint

    This problem has huge input, use scanf instead of cin to avoid time limit exceed.

    remark : 字符串的循环节 :if(len%(len-next[len])) ans=len/(len-next[len]) 
    #include<iostream>  
    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    typedef long long ll;
    int n,lb,la,crt;
    char a[1000005];
    int nextt[1000005];
    void kmp()
    {
        int k = -1, j = 0;
        nextt[0] = -1;
        while (j < la)
        {
            if (k == -1 || a[j] == a[k])
            {
                j++; k++;
                nextt[j] = k;
            }
            else k = nextt[k];
        }
    }
    void solve()
    {
        if(la% (la - nextt[la]) == 0)
            printf("%d
    ", la / (la - nextt[la]));
        else puts("1");
    }
    int main()
    {
        while (scanf("%s", a)!=EOF)
        {
            if (a[0] == '.') break;
            la = strlen(a);
            kmp();
            solve();
        }
        return 0;
    }

    For each prefix of a given string S with N characters (each character has an ASCII code between 97 and 126, inclusive), we want to know whether the prefix is a periodic string. That is, for each i (2 <= i <= N) we want to know the largest K > 1 (if there is one) such that the prefix of S with length i can be written as A K ,that is A concatenated K times, for some string A. Of course, we also want to know the period K.Input

    The input consists of several test cases. Each test case consists of two lines. The first one contains N (2 <= N <= 1 000 000) – the size of the string S.The second line contains the string S. The input file ends with a line, having the
    number zero on it.

    Output

    For each test case, output "Test case #" and the consecutive test case number on a single line; then, for each prefix with length i that has a period K > 1, output the prefix size i and the period K separated by a single space; the prefix sizes must be in increasing order. Print a blank line after each test case.

    Sample Input

    3
    aaa
    12
    aabaabaabaab
    0

    Sample Output

    Test case #1
    2 2
    3 3
    
    Test case #2
    2 2
    6 2
    9 3
    12 4

    emmmmmmm……没什么好说的,跟上边代码一样一样的……
    #include<iostream>  
    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    typedef long long ll;
    int n,la,crt=0;
    char a[1000005];
    int nextt[1000005];
    void kmp()
    {
        int k = -1, j = 0;
        nextt[0] = -1;
        while (j < n)
        {
            if (k == -1 || a[j] == a[k])
            {
                j++; k++;
                nextt[j] = k;
            }
            else k = nextt[k];
        }
    }
    void solve()
    {
        printf("Test case #%d
    ", crt);
        for(int i=1;i<=n;i++)
        if(i%(i - nextt[i]) == 0&& i!=(i - nextt[i]))
            printf("%d %d
    ",i, i / (i - nextt[i]));
        puts("");
    }
    int main()
    {
        while (~scanf("%d", &n)&&n)
        {
            crt++;
            scanf("%s", a);
            kmp();
            solve();
        }
        return 0;
    }




     
     
     
  • 相关阅读:
    对康托展开的一些心得...
    HDUOJ-----Computer Transformation
    hduoj1073--Online Judge
    HDUOJ 2672---god is a girl 《斐波那契数》
    南阳OJ----Binary String Matching
    HDUOJ--汉诺塔II
    <五>企业级开源仓库nexus3实战应用–使用nexus3配置npm私有仓库
    <六>企业级开源仓库nexus3实战应用–使用nexus3配置yum私有仓库
    <三>企业级开源仓库nexus3实战应用–使用nexus3配置maven私有仓库
    filebeat安装配置
  • 原文地址:https://www.cnblogs.com/Egoist-/p/7418284.html
Copyright © 2020-2023  润新知