poj 2752 Seek the Name, Seek the Fame(理解KMP的失配函数!)
来源:互联网 发布:nginx 文件访问权限 编辑:程序博客网 时间:2024/06/05 01:17
链接:
http://poj.org/problem?id=2752
题目大意:
给一个字符串S, 求出所有前缀pre,使得这个前缀也正好是S的后缀。 输出所有前缀的结束位置。
例如 “ababcababababcabab”, 以下这些前缀也同时是S的后缀
ab : 位置2
abab : 位置4
ababcabab : 位置9
ababcababababcabab : 位置 18
分析与总结:
这题,关键在于对KMP的失配函数的理解。只要真正理解了,那么做出来完全不成问题。
下面是后来在网上找的一个图片,很形象.
代码:
#include<iostream>#include<cstdio>#include<cstring>using namespace std;const int MAXN = 400005;char T[MAXN];int f[MAXN];bool first;void getFail(char* p,int* f){ int n=strlen(p); f[0]=f[1]=0; for(int i=1; i<n; ++i){ int j=f[i]; while(j && p[i]!=p[j]) j=f[j]; f[i+1] = p[i]==p[j]?1+j:0; }}// 偷懒直接用递归输出void output(int i){ if(i){ output(f[i]); if(first){ first=false; printf("%d",i); } else printf(" %d",i); }}int main(){ int nCase; while(gets(T)){ getFail(T,f); int j=strlen(T); first=true; output(j); puts(""); } return 0;}
—— 生命的意义,在于赋予它意义士。
原创 http://blog.csdn.net/shuangde800 , By D_Double (转载请标明)
- poj 2752 Seek the Name, Seek the Fame(理解KMP的失配函数!)
- poj 2752 Seek the Name, Seek the Fame(理解KMP的失配函数!) .
- poj 2752 Seek the Name, Seek the Fame(KMP 失配函数用法)
- POJ 2752 Seek the Name, Seek the Fame kmp失配函数next应用
- POJ - 2752 Seek the Name, Seek the Fame(KMP next数组的理解)
- poj 2752 Seek the Name, Seek the Fame(KMP)
- poj 2752 Seek the Name, Seek the Fame---kmp
- POJ 2752 Seek the Name, Seek the Fame KMP
- [poj 2752] Seek the Name, Seek the Fame[KMP]
- poj 2752 Seek the Name, Seek the Fame (KMP)
- POJ 2752 Seek the Name, Seek the Fame (KMP)
- KMP poj 2752 Seek the Name, Seek the Fame
- POJ 2752 Seek the Name, Seek the Fame KMP
- poj 2752 Seek the Name, Seek the Fame(KMP)
- POJ 2752 Seek the Name, Seek the Fame(KMP)
- POJ 2752 Seek the Name, Seek the Fame / KMP
- poj 2752 Seek the Name, Seek the Fame(KMP)
- Seek the Name, Seek the Fame - POJ 2752 KMP
- RTSC简介(2)---组件从产生到使用的过程
- java目录
- Can't connect to MySQL server on 'localhost' (10061)的解决办法!
- Android中view和surfaceview的区别
- zoj1526
- poj 2752 Seek the Name, Seek the Fame(理解KMP的失配函数!)
- 最新伤感心情QQ日志:转过身,绝望地放手
- SQLServer函数大全
- Alienware TactX 更换了德国樱桃微动DG6
- Android仿QQ主界面
- 爱上她是为了忘记你,还是害怕一个人孤寂
- 第九周 专业知识
- 罂栗花的伤感空间日志:爱情、只是宿命摆下的一个局
- Activity 中 Intent 使用方法。