zoukankan      html  css  js  c++  java
  • POJ2752Seek the Namekmp的变形

    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

    题意:求出前缀后缀一样的对应字符的位置,按递增顺序输出
    prefix-suffix:前后缀的意思。。。

     1 //ababcababababcabab
     2 #include<stdio.h>
     3 #include<iostream>
     4 #include<set>
     5 #include<string.h>
     6 using namespace std;
     7 const int N=400020;
     8 
     9 int s[N];
    10 char t[N];
    11 int nextt[N];
    12 
    13 void getnext(int len)//求的是模式串的next数组
    14 {
    15     int i=0,j=-1;
    16     nextt[0]=-1;
    17     while(i<len)
    18     {
    19         if(j<0||t[i]==t[j])
    20             nextt[++i]=++j;
    21         else
    22             j=nextt[j];
    23     }
    24 }
    25 
    26 int main()
    27 {
    28     while(~scanf("%s",t))
    29     {
    30         int len=strlen(t);
    31         getnext(len);
    32 //        for(int i=0; i<len; i++)
    33 //            cout<<nextt[i]<<"**"<<endl;
    34         int p=0;
    35         for(int i=len; i!=0; )
    36         {
    37             s[p++]=nextt[i];
    38             i=nextt[i];
    39         }
    40         p--;
    41         for(int i=p-1; i>=0; i--)
    42             printf("%d ",s[i]);
    43         printf("%d\n",len);
    44         memset(s,'\0',sizeof(s));
    45         memset(t,0,sizeof(t));
    46         memset(nextt,'\0',sizeof(nextt));
    47     }
    48     return 0;
    49 }
  • 相关阅读:
    最长公共子序列问题LCS
    [LuoguP2900] [USACO08MAR]土地征用(Land Acquisition)
    [LuoguP3195] [HNOI2008]玩具装箱TOY
    $Yeasion$的码风修改历程
    点分治模板
    Poj2919 Crane
    Poj2010 Moo University
    Kuhn-Munkres算法
    Uva10791 Minimum Sum LCM
    P1018 乘积最大(高精度加/乘)
  • 原文地址:https://www.cnblogs.com/OFSHK/p/11391322.html
Copyright © 2011-2022 走看看