zoukankan      html  css  js  c++  java
  • Seek the Name, Seek the Fame POJ

    The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the little cat to give names to their newly-born babies. They seek the name, and at the same time seek the fame. In order to escape from such boring job, the innovative little cat works out an easy but fantastic algorithm: 

    Step1. Connect the father's name and the mother's name, to a new string S. 
    Step2. Find a proper prefix-suffix string of S (which is not only the prefix, but also the suffix of S). 

    Example: Father='ala', Mother='la', we have S = 'ala'+'la' = 'alala'. Potential prefix-suffix strings of S are {'a', 'ala', 'alala'}. Given the string S, could you help the little cat to write a program to calculate the length of possible prefix-suffix strings of S? (He might thank you by giving your baby a name:) 

    Input

    The input contains a number of test cases. Each test case occupies a single line that contains the string S described above. 

    Restrictions: Only lowercase letters may appear in the input. 1 <= Length of S <= 400000. 

    Output

    For each test case, output a single line with integer numbers in increasing order, denoting the possible length of the new baby's name.

    Sample Input

    ababcababababcabab
    aaaaa
    

    Sample Output

    2 4 9 18
    1 2 3 4 5

    本题非常的巧妙

    我们先利用nxt[n]就是最长的那个 然后不断查找nxt[nxt[n]]

    直到为0我们就可以将所有的前后缀全部求出

    然后倒序输出即可

    #include<cstdio>
    #include<string.h>
    using namespace std;
    char s[400005];
    int nxt[400005];
    int n;
    void get_nxt()
    {
        int i=0;
        int k=-1;
        nxt[0]=-1;
        while(i<n)
        {
            if(k==-1||s[i]==s[k])
            {
                i++;
                k++;
                nxt[i]=k;
            }
            else
            {
                k=nxt[k];
            }
        }
        return;
    }
    int ans[400005];
    int main()
    {
        while(~scanf("%s",s))
        {
            n=strlen(s);
            get_nxt();
            int cnt=0;
            ans[++cnt]=n;
            int x=n;
            while(x)
            {
                x=nxt[x];
                if(x!=0)
                {
                    ans[++cnt]=x;
                }
            }
            for(int i=cnt;i>=1;i--)
            {
                printf("%d%s",ans[i],i==1?"
    ":" ");
            }
        }
        return 0;
    }
    
  • 相关阅读:
    【WPF】WPF Behavior 行为 实现鼠标移入按钮自动旋转
    cgo遇到的几个坑
    工程师什么时机最合适选择跳槽?
    在 ASP.NET Core 中自动启用 CAP 事务
    轻量级压力测试工具http_load
    纯C语言AES-128(可修改)-ECB模式加密-zero模式
    内存字符串暴力搜索定位代码
    windbg配置网络版双机调试
    安装docker compose
    linux安装基础软件包
  • 原文地址:https://www.cnblogs.com/caowenbo/p/11852212.html
Copyright © 2011-2022 走看看