POJ 2752 Seek the Name, Seek the Fame
来源:互联网 发布:硬件防火墙设置端口 编辑:程序博客网 时间:2024/05/24 00:41
寻找前缀与后缀相同的长度。理解next数组的意义即可。PS:一开始直接做超时,之后我是从n到2的寻找,中间不断压缩next,算是借鉴了并查集中的思想吧。(必须逆序否则压缩next后结果不对)。另当前缀长队为总长度时不满足条件,直接加上。
#include<cstdio>#include<iostream>#include<cstring>#include<algorithm>#define INF 100000000#define MAXN 400005#define MAXS 400005using namespace std;char sub[MAXN];int next[MAXN];int out[MAXN];int n;void getnext(){ int i=0; int j=-1; next[0]=-1; while(i<=n) { if(sub[i]==sub[j]||j==-1) { i++; j++; next[i]=j; } else j=next[j]; }}int fd(int a,int b)//压缩{ if(a<=b)return a; else return next[a]=fd(next[a],b);}int main(){ while(scanf("%s",sub)!=EOF) { n=strlen(sub); getnext(); int k=1; out[0]=n;//长度为本来长度时下面条件不满足,直接加上 for(int i=n-1;i>=1;i--)//逆序 { int j=next[n]; if((fd(j,i))==i)out[k++]=i; } for(int i=k-1;i>=0;i--) printf("%d ",out[i]); printf("\n"); } return 0;}
- poj seek the name,seek the fame
- poj---Seek the Name, Seek the Fame
- Seek the Name, Seek the Fame POJ
- Seek the Name, Seek the Fame POJ
- POJ-2752 Seek the Name, Seek the Fame
- 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 (next)
- POJ:2752Seek the Name, Seek the Fame
- POJ 2752 - Seek the Name, Seek the Fame
- poj 2752 Seek the Name, Seek the Fame
- poj 2752 Seek the Name, Seek the Fame
- POJ-2752-Seek the Name, Seek the Fame
- POJ 2752 Seek the Name, Seek the Fame
- poj 2752 Seek the Name, Seek the Fame
- poj 2752 Seek the Name, Seek the Fame
- POJ 2752 Seek the Name, Seek the Fame
- poj 2752 Seek the Name, Seek the Fame---kmp
- Anagrams
- POJ 1961 Period
- c++类继承中内存的布局
- 深入理解ARM体系架构(S3C6410)---认识S3C6410
- Activity切换效果(overridePendingTransition)
- POJ 2752 Seek the Name, Seek the Fame
- 深入理解ARM体系架构(S3C6410)---arm7,arm9,arm11区别
- 做CEO最主要的工作是Kill ideas,我对李彦宏创业七剑的一点理解
- 【原创分享】嵌入式linux应用之内核移植定制篇-前篇(linux-3.8.12 mini2440)--20130824
- 理解嵌入式开发中的一些硬件相关的概念
- 使用注册机激活MyEclipse 10失败的解决方法
- C++校验日期串合法性
- 基于google Zxing实现二维码、条形码扫描,仿微信二维码扫描效果
- java枚举详解实例