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 }
    
    
  • 相关阅读:
    jsp第二次作业
    jsp第一个作业
    Android应用程序开发
    第十次作业
    int和Integer比较详解
    com.sun.tools.jar下载失败
    JDK1.7和JDK1.8自由切换
    解析excel
    django 2.0配置url捕获参数
    python使用pymysql链接mysql数据库实现增删改查
  • 原文地址:https://www.cnblogs.com/ISGuXing/p/7282128.html
Copyright © 2011-2022 走看看