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 }
  • 相关阅读:
    软件工程第八周
    e课表项目第二次冲刺周期第十天
    蓝桥杯java历年真题及答案整理41~56
    蓝桥杯java历年真题及答案整理21~40
    蓝桥杯java历年真题及答案整理1~20.md
    异序二分查找 二分查找方程根 二分查找重复元素最后一个
    软件说明
    遗传算法--句子匹配
    Matlab学习笔记
    使用TenforFlow 搭建BP神经网络拟合二次函数
  • 原文地址:https://www.cnblogs.com/OFSHK/p/11391322.html
Copyright © 2011-2022 走看看