zoukankan      html  css  js  c++  java
  • kmp(所有长度的前缀与后缀)

    http://poj.org/problem?id=2752
    Seek the Name, Seek the Fame
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 27512   Accepted: 14244

    Description

    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
    

    Source

     
    题意:求字符串所有的相同前缀与后缀;
    思路:我们知道next数组是求最长前缀与后缀,如果要求所有,我们就要向前递归next数组直到为0,即可找出所有长度的前缀与后缀
     
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <iostream>
    #include <algorithm>
    #include <iostream>
    #include<cstdio>
    #include<string>
    #include<cstring>
    #include <stdio.h>
    #include <string.h>
    using namespace std;
    char a[1000009];
    int next[1000009];
    int num[1000009];
    void getnext(char *a , int len , int *next)
    {
        next[0] = -1 ;
        int k = -1 , j = 0 ;
        while(j < len)
        {
            if(k == -1 || a[k] == a[j])
            {
                k++;
                j++;
               // if(a[k] != a[j])
                    next[j] = k;//next数组不能优化
              //  else
                //    next[j] = next[k];*/
            }
            else
                k = next[k];
        }
    }
    
    
    int main()
    {
        while(~scanf("%s" , a))
        {
            memset(next , 0 , sizeof(next));
            memset(num , 0 , sizeof(num));
            int len = strlen(a);
            getnext(a , len , next);
            int l = 0 ;
            num[l++] = len ;
            while(next[len] > 0)//向前递归next数组
            {
                num[l++] = next[len];
                len = next[len];
            }
            for(int i = l - 1 ; i > 0 ; i--)
            {
                printf("%d " , num[i]);
            }
            printf("%d
    " , num[0]);
        }
    
        return 0 ;
    }
  • 相关阅读:
    Java中OutOfMemoryError(内存溢出)的情况及解决办法
    php strtotime函数服务器和本地不相同
    Object传入String类型和其他
    Java静态变量,常量,成员变量,局部变量
    Vector使用
    Java反射机制
    List和ArrayList,LinkList的区别
    phpstrtotime()对于31日求上个月有问题
    PGsql解决时差24H
    drawable 另外一种形式dimens.xml
  • 原文地址:https://www.cnblogs.com/nonames/p/11294338.html
Copyright © 2011-2022 走看看