博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj2752seek the name, seek the fame【kmp】
阅读量:5059 次
发布时间:2019-06-12

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

 

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

 

 

题意:求有哪些子串既是前缀又是后缀,输出他们的长度

思路:字符串本身肯定是一个答案。要去找下一个比他短的子串了,而next[n-1]表示的是以最后一个字符为结尾的串前后缀相同的最长的长度。

由于后缀的最后那些字符是确定了的,所以下一个答案显然是next[n-1]。

继续向前回溯。更短的子串显然是name[k...n-1],k大于n-1-next[n-1]

而name[0,next[n-1]-1] == name[n-1-next[n-1]...n-1] 那么我们相当于就是在name[0,next[n-1]-1] 继续找他的最长的前后缀,直到next为-1.也就是next数组的作用了

需要注意的一点是,要判断name[next[j]] == name[n - 1], 因为字符的第一个元素需要特判,他的next永远是0

而当name[0]恰好等于name[n -1]时,他是可以的。但是当他不相等的时候,是不行的。

代码:

 

#include
#include
#include
#include
#include
#include
#include
#define inf 0x3f3f3f3fusing namespace std;char name[400005];int nnext[400005], ans[400005];void getnext(){ int i = 0, j = -1; nnext[0] = -1; int len = strlen(name); while(i < len){ if(j == -1 || name[i] == name[j]){ i++; j++; nnext[i] = j; } else{ j = nnext[j]; } }}int main(){ while(cin>> name){ getnext(); int len = strlen(name); int cnt = 0, j = len - 1; ans[cnt++] = len; while(nnext[j] != -1){ j = nnext[j]; if(name[j] == name[len - 1]){ ans[cnt++] = j + 1; } } cout<
= 0; i--){ cout<<" "<

 

转载于:https://www.cnblogs.com/wyboooo/p/9643432.html

你可能感兴趣的文章
Problem - 1118B - Codeforces(Tanya and Candies)
查看>>
jdk1.8 api 下载
查看>>
svn 图标不显示
查看>>
getElement的几中属性介绍
查看>>
iOS 使用Quartz 2D画虚线 【转】
查看>>
平面最接近点对
查看>>
HTML列表,表格与媒体元素
查看>>
PHP、Java、Python、C、C++ 这几种编程语言都各有什么特点或优点?
查看>>
感谢青春
查看>>
Jquery Uploadify4.2 falsh 实现上传
查看>>
雨林木风 GHOST_XP SP3 快速装机版YN12.08
查看>>
linux基础-命令
查看>>
java对象的深浅克隆
查看>>
Hadoop流程---从tpch到hive
查看>>
数据结构3——浅谈zkw线段树
查看>>
Introduction to my galaxy engine 2: Depth of field
查看>>
V2019 Super DSP3 Odometer Correction Vehicle List
查看>>
Python 3.X 练习集100题 05
查看>>
今时不同往日:VS2010十大绝技让VS6叹服
查看>>
设计器 和后台代码的转换 快捷键
查看>>