zoukankan      html  css  js  c++  java
  • POJ-2752

    Seek the Name, Seek the Fame
    Time Limit: 2000MS Memory Limit: 65536K
    Total Submissions: 20196 Accepted: 10514

    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
    
    
    
    
     1 #include<iostream>
     2 #include<stdio.h>
     3 #include<string>
     4 #include<string.h>
     5 using namespace std;
     6 int nxt[500000];
     7 int f[500000];
     8 string p;
     9 int change()
    10 {
    11     int plen=p.length();
    12     int j=0;
    13     int k=-1;
    14     nxt[0]=-1;
    15     while(j<plen)
    16     {
    17         if(p[j]==p[k]||k==-1)
    18         {
    19             nxt[++j]=++k;
    20         }
    21         else
    22         {
    23             k=nxt[k];
    24         }
    25     }
    26 }
    27 int main()
    28 {
    29     while(cin>>p)
    30     {
    31         change();   //求出next数组
    32         int plen=p.length();
    33         int i=plen;
    34         int t=0;
    35         while(i)
    36         {
    37             f[t++]=i;
    38             i=nxt[i];
    39         }
    40         for(int j=t-1;j>=0;j--)
    41             cout<<f[j]<<' ';
    42         cout<<endl;
    43     }
    44     return 0;
    45 }
    
    
  • 相关阅读:
    JUnit-4.11使用报java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing错误
    生成验证码图片
    九九乘法表
    Java注解之Retention、Documented、Target、Inherited介绍
    【ztree】zTree节点增删改&ztree对树节点进行检索
    一次性搞清楚equals和hashCode
    HashMap实现原理分析
    DDL、DML和DCL的区别与理解
    MySQL的@与@@区别
    springboot整合mybatis(SSM开发环境搭建)&Springboot项目热部署
  • 原文地址:https://www.cnblogs.com/ISGuXing/p/7282128.html
Copyright © 2011-2022 走看看