zoukankan      html  css  js  c++  java
  • 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;
    }




     
     
     
  • 相关阅读:
    [错误处理]UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 0: ordinal not in range(128)
    [已解决]使用 apt-get update 命令提示 ...中被配置了多次
    linux各种版本查看方法
    [Pandas技巧] 如何把pandas dataframe对象或series对象转换成list
    linux下终止相关操作
    [错误处理]Vim卡死,无法输入是怎么回事?是不是按了Ctrl+S
    批量修改文件名称方法
    pycharm配置 自动运行指定脚本
    pip安装超时,更换国内镜像源安装
    命令行特殊字符名字转义
  • 原文地址:https://www.cnblogs.com/Egoist-/p/7418284.html
Copyright © 2011-2022 走看看