Seek the Name, Seek the Fame POJ - 2752 (理解KMP函数的失配)既是S的前缀又是S的后缀的子串
发布日期:2021-06-29 04:04:30 浏览次数:2 分类:技术文章

本文共 2329 字,大约阅读时间需要 7 分钟。

题意:给一个字符串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

ababcababababcababaaaaa

Sample Output

2 4 9 181 2 3 4 5

AC代码

#include
#include
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
=1; i--) printf("%d ",e[i]); printf("%d\n",e[0]); } return 0;}

 

转载地址:https://blog.csdn.net/zeng_jun_yv/article/details/100022483 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Too Many Segments (hard version) CodeForces - 1249D2(贪心+容器vector+set)
下一篇:一个简单的轮播图代码

发表评论

最新留言

做的很好,不错不错
[***.243.131.199]2024年04月08日 15时24分48秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

Atitit 编程语言语言规范总结 目录 1. 语言规范 3 2. Types 3 2.1.1. Primitive types 3 2.1.2. Compound types 4 3. State 2019-04-29
Atitit QL查询语言总结 目录 1. QL = Query Language, 是查询语言的简称 1 2. 具体实现 1 2.1. Apcl 流程控制语言 1 2.2. 脚本流程控制 2 2. 2019-04-29
Atitit 开发效率大法 v0 t025.docx Atitit 提升开发效率几大策略 目录 1. 提升效率三原则 3 1.1. 更少的代码量简化 3 1.2. 优化配置减少等待 3 1.3. 2019-04-29
Atitit mybatis的扩展使用sql udf,js java等语言 目录 1.1. 默认,mybatis使用xml,sql等语言来书写业务流程 1 2. 使用ognl调用java函数 1 3 2019-04-29
Atitit if else 选择决策流程ast对比 sql java 表达式类型 binaryExpression hase left and rit expr 目录 1.1. Sql 1 2019-04-29
Atitit 数据库存储引擎 目录 1.1. BLACKHOLE 黑洞引擎 1 1.2. Myisam innodb 1 1.3. Archive 档案类 1 1.4. Fed 连接引擎 2 1. 2019-04-29
Atitit sql注入的防范 目录 1.1. 检查数据类型 1 2. 有限操作DML 1 2.1. 限制执行函数黑名单机制 2 2.2. 限制执行系统sp 2 2.3. 限制数据查询语句类型,只能 2019-04-29
Atitit 自然语言与人工语言的语法构建ast的异同点 目录 1. 语言节点gaishu。。 2 1.1. 节点、函数数量大约200个 2 1.2. 关键词节点 是 有 的 3 1.3. 标识符 2019-04-29
Atitit 效率提升法细则 v3 t028.docx Atitit 提升效率细则 目录 1. 目标 2 1.1. 配置化增加扩展性 尽可能消除编译 方便增加 调整业务逻辑 2 1.2. 统一接口 2019-04-29
Atitit 工程师程序员技术级别对应表与主要特征 P1--p6 说明 类别 职称 对应技术标志 P5 高级工程师 工程师类 一般四五年 P6 资深开发 工程师类 78年经历 P7 P7 2019-04-29
paip.activex控件在WEB中使用流程与工具 2019-04-29
paip.软件及网站项目开发效率低下的思索与改进 2019-04-29
Atitit 可移植性之道attilax著 2019-04-29
paip.截屏功能流程说明 2019-04-29
Atiitt uke兼wag集团2017年度成果报告总结 attilax著 1. 组织机构进一步完善 8大首席部门 1 2. 事业部进一步完善,以及一百多个事业部了 1 3. 企业文化进一步完善 1 2019-04-29
Atititi ui之道 attilax著 v3 s11.docx 1. 概览 2 1.1. 软件设计可分为两个部分:编码设计与UI设计 2 2. 用户界面设计的三大原则是:置界面于用户的控制之下; 2019-04-29
Atitit 集团与个人的完整入口列表 attilax的完整入口 1. 集团与个人的完整入口列表 1 2. 流量入口概念 2 3. 流量入口的历史与发展 2 1.集团与个人的完整入口列表 2019-04-29
Atitit 网络编程之道 2019-04-29
Atiitt attilax掌握的前后技术放在简历里面.docx 2019-04-29
Atiitt 文档处理之道 attilax著 2019-04-29