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 }
    
    
  • 相关阅读:
    windows cmd中查看某个命令所在的路径
    linux vi编辑器中,如何通过快捷键上下翻页?
    linux系统中,查看当前系统中,都在监听哪些端口
    下载mysql server安装包的时候,不登录oracle账号,实现下载
    plsql developer中,清除登录历史
    linux环境下,清空history中记录的历史命令
    Linux下搭建hadoop开发环境-超详细
    HDFS架构详解-非官档
    SSH免密码登录配置方法详解
    bin/hdfs dfs命令存在WARN util.NativeCodeLoader问题消除方法
  • 原文地址:https://www.cnblogs.com/ISGuXing/p/7282128.html
Copyright © 2011-2022 走看看