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 ;
    }
  • 相关阅读:
    2016阿里巴巴73款开源产品全向图
    在微软5年,我学到的几个小技能
    2016 年 Java 工具和技术的调查:IDEA 已超过
    PHP学习总结(9)——PHP入门篇之WAMPServer服务控制面板介绍
    Template Pattern & Strategy Pattern
    天之道,损有余而补不足。人之道,则不然,损不足以奉有余。孰能有余以奉天下?唯有道者
    [林锐13]面向对象程序设计方法概述
    [林锐8.4]函数指针
    ZT c++ 中的重载全局new,delete
    ZT 自定义operator new与operator delete的使用(1)
  • 原文地址:https://www.cnblogs.com/nonames/p/11294338.html
Copyright © 2011-2022 走看看