poj--2752Seek the Name, Seek the Fame KMPnext数组的应用
来源:互联网 发布:淘宝怎么打新品标 编辑:程序博客网 时间:2024/04/30 04:39
题目可以装换成求以最后一个字符结尾的与某一前缀相同的所有后缀的长度;所以需要利用KMP函数中next数组的性质。
我们可以先求出前n-1个字每的next值,然后再求出第n个字母所有可能的(而不是最长的)next值。
当然我们可以先求出所有字母的next值,然后再倒着扫一遍,即next[len],next[next[len]]……..。这样也可以将所有的长度求出来。
注意每一个串的本身是满足一定要求的。
代码如下:
#include<iostream>#include<cstdio>#include<cstring>using namespace std;char str[500000];int next[500000];int ans[500000];int cnt;void Getnext(){ int j=0; int n=strlen(str+1); next[1]=0; for(int i=2;i<=n;i++) { if(i==n) { while(j>0) { if(str[j+1]==str[i]) ans[cnt++]=j+1; j=next[j]; } if(str[j+1]==str[i]) ans[cnt++]=j+1; } while(j>0&&str[j+1]!=str[i]) j=next[j]; if(str[j+1]==str[i]) j++; next[i]=j; }}int main(){ while(scanf("%s",str+1)!=EOF) { cnt=0; ans[cnt++]=strlen(str+1); Getnext(); for(int i=cnt-1;i>=0;i--) printf("%d ",ans[i]); printf("\n"); } return 0;}
0 0
- poj--2752Seek the Name, Seek the Fame KMPnext数组的应用
- POJ 2752 Seek the Name, Seek the Fame(KMP,next数组的应用)
- Seek the Name, Seek the Fame POJ 2752【KMP next数组的应用】
- POJ 2752 Seek the Name, Seek the Fame(next数组的应用)
- 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 next数组的应用)
- Seek the Name, Seek the Fame next数组的应用
- POJ 2752 Seek the Name, Seek the Fame(KMP next数组应用)
- POJ 2752 Seek the Name, Seek the Fame(next数组的理解)
- 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+next数组的运用)
- POJ 2752-Seek the Name, Seek the Fame(KMP的next数组运用)
- POJ - 2752 Seek the Name, Seek the Fame (KMP的next[]应用)
- poj seek the name,seek the fame
- poj---Seek the Name, Seek the Fame
- Seek the Name, Seek the Fame POJ
- Kafka学习笔记
- 黑马程序员------C语言-----流程控制(循环结构:while,for)
- Redis常用命令总结
- hibernate学习:一对一单向联合外键
- 浏览器插件之ActiveX开发(三)
- poj--2752Seek the Name, Seek the Fame KMPnext数组的应用
- 将WordPress部署到七牛云存储实现CDN加速
- hibernate学习:Component组件
- 那些搁浅的爱情和梦想
- ubuntu12.04下安装搜狗输入法
- [面试经]STL bitset用法总结
- Git常用命令总结
- 用YourUninstaller将JDK卸载干净
- 清除eclipse中Launch configuration