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 }
    
    
  • 相关阅读:
    url 路径的拼接
    java 实现导出Excel文件
    window 使用频率最高的快捷键
    jeesite 框架的简单应用
    一个软件开发者的解决问题的心得——善于利用蛛丝马迹
    在linux上安装dotnetcore
    c#多线程同步之EventWaitHandle使用
    sharepoint 2013 安装
    模板模式的应用
    正则表达式的应用
  • 原文地址:https://www.cnblogs.com/ISGuXing/p/7282128.html
Copyright © 2011-2022 走看看