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 }
    
    
  • 相关阅读:
    VirtualBox下Linux扩展磁盘空间
    idea 2019.3 svn 忽略文件/目录
    CentOS7添加/删除用户和用户组
    查看LINUX进程内存占用情况及启动时间
    Linux定时自动备份oracle数据库
    oracle11g数据库导入导出方法教程以及导出表不全解决
    ubuntu12下安装unixODBC(mysql)
    oracle登录管理员创建数据库和表空间
    centos7 64运行32位程序
    Windows下搭建Redis集群
  • 原文地址:https://www.cnblogs.com/ISGuXing/p/7282128.html
Copyright © 2011-2022 走看看