• Period II


    For each prefix with length P of a given string S,if

    S[i]=S[i+P] for i in [0..SIZE(S)-p-1],

    then the prefix is a “period” of S. We want to all the periodic prefixs.

    Input

    Input contains multiple cases.

    The first line contains an integer T representing the number of cases. Then following T cases.

    Each test case contains a string S (1 <= SIZE(S) <= 1000000),represents the title.S consists of lowercase ,uppercase letter.

    Output

    For each test case, first output one line containing "Case #x: y", where x is the case number (starting from 1) and y is the number of periodic prefixs.Then output the lengths of the periodic prefixs in ascending order.

    Sample Input

    4
    ooo
    acmacmacmacmacma
    fzufzufzuf
    stostootssto
    

    Sample Output

    Case #1: 3
    1 2 3
    Case #2: 6
    3 6 9 12 15 16
    Case #3: 4
    3 6 9 10
    Case #4: 2
    9 12
    题意:求所有的循环节
    思路:循环ans=ne[ans],len-ans
    #include <iostream>
    #include<string.h>
    using namespace std;
    const int maxn = 1e6+10;
    char a[maxn];
    int ne[maxn],sum[maxn];
    int main()
    {
        int t;
        int num=0;
        scanf("%d",&t);
        while(t--)
        {
            memset(ne,0,sizeof(ne));
            scanf("%s",a+1);
            int len=strlen(a+1);
            for(int i=2,j=0;i<=len;i++)
            {
                while(j&&a[i]!=a[j+1]) j=ne[j];
                if(a[i]==a[j+1]) j++;
                ne[i]=j;
            }
            int ans=len,cnt=0;
            while(ans)
            {
                ans=ne[ans];
                sum[++cnt]=len-ans;
            }
            printf("Case #%d: %d
    ",++num,cnt);
            for(int i=1;i<=cnt-1;i++)
                printf("%d ",sum[i]);
            printf("%d
    ",sum[cnt]);
        }
        return 0;
    }
  • 相关阅读:
    hdu 1108 最小公倍数
    hdu 1106 排序
    hdu 1097 A hard puzzle
    hdu 1076 An Easy Task
    hdu 1064 Financial Management
    hdu 1061 Rightmost Digit
    hdu 1050 Moving Tables
    hdu 1060 Leftmost Digit
    hdu 1049 Climbing Worm
    hdu1104
  • 原文地址:https://www.cnblogs.com/wjc2021/p/11310428.html
Copyright © 2020-2023  润新知