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 }
  • 相关阅读:
    EventBus总结(原)
    StarUML 破解方法2.X(转)
    扩展阿里巴巴Java开发规约插件(转)
    Java 代码规范,你应该知道的一些工具和用法(转)
    使用IntelliJ IDEA 15和Maven创建Java Web项目(转)
    Git--将已有的项目添加到github(转)
    单元测试汇总
    Intellij idea创建javaWeb:实现JSP/Servlet(转)
    设计模式学习之中介者模式(转)
    jvm加载包名和类名相同的类的规则,以及如何加载包名和类名相同的类(转)
  • 原文地址:https://www.cnblogs.com/OFSHK/p/11391322.html
Copyright © 2011-2022 走看看