题意:给一个字符串S, 求出所有前缀pre,使得这个前缀也正好是S的后缀。 输出所有前缀的结束位置。

就是求前缀和后缀相同的那个子串的长度  然后从小到大输出,主要利用next数组求解。

例如 “ababcababababcabab”, 以下这些前缀也同时是S的后缀

ab  :  位置2

abab  : 位置4

ababcabab : 位置9

ababcababababcabab : 位置 18

分析与总结:

这题,关键在于对KMP的失配函数的理解。只要真正理解了,那么做出来完全不成问题。

next[i]的意义就是:前面长度为i的字串的【前缀和后缀的最大匹配长度】

下面是后来在网上找的一个图片,很形象.

e.g.

i 0 1

2

3 4 5 6 7 8

9

10 11 12 13 14 15 16 17

18

dp a b

a

b c a b a b

a

b a b c a b a b

/0

next -1 0

0

1 2 0 1 2 3

4

3 4 3 4 5 6 7 8

9

Out: 2  4  9  18
      由于子串既是S的前缀又是后缀,所以除去字符串本身长度18外,次长的为在字符串结束符‘\0’处求得的next数组值9,

此时S的前缀为 ababcabab(012345678),S的后缀为 ababcabab(9-17)。接着找第三长的既是S的前缀又是S后缀的子串。

如果找next[17]处的值8,则不满足是S后缀的要求,因为17本身的字符是被排除在外的,10-16亦是同理。

而对于9之前的子串有next[18]知既是S的前缀又是S的后缀。而next[9]的值4指明了位置在9前的子串的前后缀长度为4,

而该子串包含在S的前缀中,所以该子串的后缀也包含在S的前缀中,而S的前缀又是S的后缀,所以该子串的后缀也是S的后缀。

依次往前直到满足条件的子串长度为0为止。

题目:

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

AC代码

#include<stdio.h>
#include<string.h>
using namespace std;
const int M=4e5+10;
char dp[M];
int s[M],e[M];
void f(int x)
{memset(s,0,sizeof(s));int i=0;int j=-1;s[0]=-1;while(i<x){if(j==-1||dp[i]==dp[j])s[++i]=++j;else j=s[j];}
}
int main()
{while(~scanf("%s",dp)){memset(e,0,sizeof(e));int x=strlen(dp);f(x);int b=0,k,a=0,i,flag=0;i=x;while(i!=-1){e[a++]=i;i=s[i];if(i==0)break;}for(i=a-1; i>=1; i--)printf("%d ",e[i]);printf("%d\n",e[0]);}return 0;
}

Seek the Name, Seek the Fame POJ - 2752 (理解KMP函数的失配)既是S的前缀又是S的后缀的子串相关推荐

  1. Seek the Name, Seek the Fame - POJ 2752(next运用)

    题目大意:小猫是非常有名气的,所以很多父母都来找它给孩子取名字,因为找的人比较多,小猫为了摆脱这个无聊的工作,于是它发明了一种取名字的办法,它把孩子父母的名字合在一起,然后从这个名字里面找一个前缀,并 ...

  2. Seek the Name, Seek the Fame POJ - 2752(KMP应用)

    Stringland有一只小猫非常有名,许多夫妇跋涉到Stringland请小猫为他们的新出生的婴儿起名字,他们寻求这个名字,同时寻求名声.为了摆脱这种无聊的工作,创新的小猫制定了一个简单而奇妙的算法 ...

  3. [poj2752]Seek the Name, Seek the Fame_KMP

    Seek the Name, Seek the Fame poj-2752 题目大意:给出一个字符串p,求所有既是p的前缀又是p的后缀的所有字符串长度,由小到大输出. 注释:$1\le strlen( ...

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

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

  5. 【POJ - 2752】Seek the Name, Seek the Fame (KMP,公共前缀后缀长度及个数)

    题干: The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked t ...

  6. 【hash】Seek the Name, Seek the Fame

    [哈希和哈希表]Seek the Name, Seek the Fame 题目描述 The little cat is so famous, that many couples tramp over ...

  7. KMP Seek the Name,Seek the Fame

    Seek the Name,Seek the Fame 时间限制: 1 Sec 内存限制: 128 MB 题目描述 给定一个字符串s,从小到大输出s中既是前缀又是后缀的子串的长度. 字符串长度不超过四 ...

  8. Seek the Name, Seek the Fame(KMP-next数组详解)

    Seek the Name, Seek the Fame 目录 Seek the Name, Seek the Fame 解题过程 举个例子 AC代码 The little cat is so fam ...

  9. poj-2752 Seek the Name, Seek the Fame **

    /* * KMP **/ #include <cstdio>#include <cstring>using namespace std; const int maxL = 40 ...

最新文章

  1. Activity管理(一):activity运行机制
  2. 忘记目标 潜心做事([日] 端河光二)
  3. 方法的重写-扩展父类方法,super对象调用父类方法
  4. PHP解压与配置的图片,PHP完善压缩处理类(支持主流的图像类型(jpg、png、gif)...
  5. postman websocket_postman的“替代者”postwoman的使用体验—从入门到放弃
  6. 第九周-每周例行报告
  7. ubuntu 17.10 如何设置合盖不关机
  8. 利用word2vec创建中文主题词典——以网络暴力关键词为例
  9. C++语言中关于switch的用法
  10. linux备份压缩tgz,linux关于解压和压缩命令 zip rar tar.gz tgz
  11. 基于STM32F429IGT6的NAND FLASH读写测试(CUBEMX)
  12. 新产品、新特性、新生态丨一文回顾openGauss峰会云和恩墨分论坛150分钟的精彩...
  13. 微信公众号上传文件附件教程
  14. wps怎么把字缩到最小_wps怎么把最左侧的字体变小
  15. 【归纳】S3C2440A之ARM学习的所有的问题:
  16. 齐博x1 相关栏目名称的调用
  17. 新年第一帖——元旦这天骑车迷路了
  18. nib must contain exactly one top level object which must be a UITableViewCell instance
  19. 解决虚拟光驱导致物理光驱丢失问题
  20. GNSS/INS松组合算法原理简介

热门文章

  1. Android Studio之package org.junit does not exist解决办法
  2. java之socket的OOBInline和UrgentData和发送心跳包研究
  3. 栈和队列之设计一个有getMin(得到最小值)功能的栈
  4. php json -gt;访问,【转】Php+ajax+jsonp解决ajax跨域问题
  5. 实现线段切割法_漫画:如何实现抢红包算法?
  6. 计算机导论上机模拟,计算机导论模拟考试题6份完整版.doc
  7. python创_Python创建Windows 服务
  8. 这不是特效也不是魔术!
  9. 这样子称象你试过没有?
  10. 17道因为太难而被禁用的Google面试题