poj 2752 KMP的next数组应用(prefix-suffix string)
来源:互联网 发布:扇贝背单词软件 编辑:程序博客网 时间:2024/05/02 04:57
题意:给定一个串,求其所有的同为前缀和后缀的子串的长度。
思路:用KMP预处理的next数组就可以解决,时间为O(n)。可以这样想,符合题意的最长的串必为原串,次长度为next[len-1],这是由next数组的定义决定的。再次长度就是对str[0,next[len-1]]这个串去求,也就是next[next[len-1]]这么长。所以递归求下去就可以了。
#include <cstdio>#include <cstring>#define N 400005char s[N];int next[N],len;void getnext(){ int i,k; next[0] = k = -1; for(i = 1;i<len;i++){ while(k>-1 && s[k+1]!=s[i]) k = next[k]; if(s[k+1] == s[i]) k++; next[i] = k; }}void print(int x){ if(x!=-1){ print(next[x]); printf("%d ",x+1); }}int main(){ while(scanf("%s",s) != EOF){ len = (int)strlen(s); getnext(); print(len-1); putchar('\n'); } return 0;}
0 0
- poj 2752 KMP的next数组应用(prefix-suffix string)
- poj 2752 kmp(next数组的应用)
- POJ 1961 KMP的next数组应用
- POJ 2406 KMP next数组的应用
- POJ 2752 KMP算法中next数组的应用
- POJ 2752 KMP中next数组的应用
- poj 2406 Power Strings(kmp next数组的应用)
- POJ 2752 Seek the Name, Seek the Fame(KMP,next数组的应用)
- POJ 2752 Seek the Name, Seek the Fame(KMP next数组的应用)
- poj 2752 kmp(next数组模板)
- poj 2752 KMP(next数组的运用)
- kmp——next数组的应用---cout the string
- HGU3336 Count the string (KMP Next数组的应用)
- poj 2406 Power Strings KMP的next数组的应用
- Count the string kmp next数组应用
- POJ 2185 KMP中next数组的应用
- poj 1961 kmp算法之next数组的意义应用
- POJ 1961 Period KMP next数组的应用
- 黑马程序员——Eclipse的常用快捷键备忘
- Hadoop2.2.0集群搭建(HA,联邦,yarn)
- 彻底解决 error: Unable to find vcvarsall.bat
- LaTeX入门资料
- python 的日志logging模块学习
- poj 2752 KMP的next数组应用(prefix-suffix string)
- Tomcate启动报错: JDWP No transports initialized
- UVA - 10006 - Carmichael Numbers (快速幂+素数判断)
- 操作系统概念学习笔记 1 加电引导过程
- 使用SublimeText 编辑Markdown 文件并预览
- P123 第36题 年月日
- vi 命令
- VS2010简单使用动态链接库流程
- Ubuntu下配置USB转串口及串口工具配置