Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u

Submit Status

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

______________________________________________________________________________________________________________________________________________________

题目大意:给定字符串S,求出S的所有公共前后缀的长度(包含自身)

KMP求出next[],S的公共前后缀中前缀的公共前后缀也是S的公共前后缀。用此循环即可。
_______________________________________________________________________________________________________________________________________________________

 1 #include<cstdio>
2 #include<cstring>
3 #include<algorithm>
4
5 using namespace std;
6 const int maxl=400010;
7 char s[maxl];
8 int next[maxl],l;
9 int ans[maxl],js=0,ll;
10 void getnext()
11 {
12 l=strlen(s);
13 next[0]=-1;
14 for(int j,i=1;i<l;i++)
15 {
16 j=next[i-1];
17 while(s[i]!=s[j+1] && j>=0)j=next[j];
18 next[i]=s[i]==s[j+1]?j+1:-1;
19 }
20 }
21 int main()
22 {
23 while(~scanf("%s",s))
24 {
25 getnext();
26 js=0;ll=l-1;
27 while(next[ll]!=-1)
28 {
29 ans[js++]=next[ll];
30 ll=next[ll];
31 }
32 while(js>0)printf("%d ",ans[--js]+1);
33 printf("%d\n",l);
34 }
35 return 0;
36 }

poj 2752Seek the Name, Seek the Fame的更多相关文章

  1. POJ 2752Seek the Name, Seek the Fame(next数组妙用 + 既是前缀也是后缀)

    题目链接 题意:求一个字符串中 前缀 和 后缀 相同的长度 分析: 对于一个字符串他自己的长度肯定是可以的.然后如果满足 前缀 和 后缀相等,那个前缀 最后一个字符 一定 和 该字符串最后一个字符相等 ...

  2. POJ 2752 Seek the Name, Seek the Fame [kmp]

    Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17898   Ac ...

  3. POJ 2752 Seek the Name, Seek the Fame

    传送门 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14761   Accepted: 7407 Description ...

  4. poj 2752 Seek the Name, Seek the Fame(KMP需转换下思想)

    Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10204   Ac ...

  5. (KMP)Seek the Name, Seek the Fame -- poj --2752

    http://poj.org/problem?id=2752 Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536 ...

  6. KMP + 求相等前后缀--- POJ Seek the Name, Seek the Fame

    Seek the Name, Seek the Fame Problem's Link: http://poj.org/problem?id=2752 Mean: 给你一个字符串,求这个字符串中有多少 ...

  7. 【POJ 2752 Seek the Name, Seek the Fame】

    Time Limit: 2000MSMemory Limit: 65536K Description The little cat is so famous, that many couples tr ...

  8. Seek the Name, Seek the Fame POJ - 2752

    Seek the Name, Seek the Fame POJ - 2752 http://972169909-qq-com.iteye.com/blog/1071548 (kmp的next的简单应 ...

  9. KMP POJ 2752 Seek the Name, Seek the Fame

    题目传送门 /* 题意:求出一个串的前缀与后缀相同的字串的长度 KMP:nex[]就有这样的性质,倒过来输出就行了 */ /************************************** ...

随机推荐

  1. 这4种ThreadLocal你都知道吗?

    什么是ThreadLocal ThreadLocal类顾名思义可以理解为线程本地变量.也就是说如果定义了一个ThreadLocal, 每个线程往这个ThreadLocal中读写是线程隔离,互相之间不会 ...

  2. Object[] cannot be converted to String[]

    原因: 你应该是想把List数组转 String数组吧! 然后想当然的调用list.toArray()方法. 结果 该方法返回的是Object[]数组,导致类型不匹配! 解决办法: 还在乖乖的用循环吧 ...

  3. 访问需要HTTP Basic Authentication认证的资源的c#的实现

    string username="username"; string password="password"; //注意这里的格式哦,为 "usern ...

  4. SQL数据库创建,创建表,增删改查

    创建数据库:create datebase数据库名 删除数据库:drop datebase 数据库名称 创建表格式: create table 表名(字段名1,字段类型1,字段名2,字段类型2) 查询 ...

  5. .NET 云原生架构师训练营(模块二 基础巩固 MongoDB 更新和删除)--学习笔记

    2.5.4 MongoDB -- 更新和删除 整体更新 更新字段 字段操作 数组操作 删除 https://docs.mongodb.com/manual/reference/operator/upd ...

  6. 原来大数据 Hadoop 是这样存储数据的

    HDFS概述 产生背景 随着数据量越来越大,在一个操作系统中存不下所有的数据.需要将这些数据分配到更多的操作系统中,带来的问题是多操作系统不方便管理和维护.需要一种系统来管理多台机器上的文件,这就是分 ...

  7. mysql 创建[序列],功能类似于oracle的序列

    参考自菜鸟教程 https://www.runoob.com/mysql/mysql-using-sequences.html 使用函数创建自增序列管理表(批量使用自增表,设置初始值,自增幅度) 第一 ...

  8. 洛谷P4848 崂山白花蛇草水 权值线段树+KDtree

    题目描述 神犇 \(Aleph\) 在 \(SDOI\ Round2\) 前立了一个 \(flag\):如果进了省队,就现场直播喝崂山白花蛇草水.凭借着神犇 \(Aleph\) 的实力,他轻松地进了山 ...

  9. java使用正则的例子

    package com.accord.util; import java.util.ArrayList; import java.util.List; import java.util.regex.M ...

  10. 使用Lists.partition切分性能优化

    项目实战 影拓邦电影同步中,使用Lists.partition按500条长度进行切分,来实现es的同步. 切分的List为 使用介绍及示例 将list集合按指定长度进行切分,返回新的List<L ...