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 }
  • 相关阅读:
    Linux Docker容器磁盘出现日志/var/lib/docker/overlay2占用100%
    (转)关于文字放大缩小
    Jquery 点击按钮切换div(可循环轮播)
    C#调用C++的dll两种方法(托管与非托管)
    Spring——项目优雅停机
    Maven——(mavenantrunplugin、mavendependencyplugin)插件的使用
    微信开放平台第三方平台开发的一些坑解决以备忘 编程
    QRCodeEncoder 生成二维码提示 数组越界 的解决方法 编程
    jenkins+docker+nginx+vue前端项目实现自动部署
    nginx+vue项目请求后端接口报错:405 not allowed
  • 原文地址:https://www.cnblogs.com/OFSHK/p/11391322.html
Copyright © 2011-2022 走看看