[poj2752]Seek the Name, Seek the Fame_KMP
Seek the Name, Seek the Fame poj-2752
题目大意:给出一个字符串p,求所有既是p的前缀又是p的后缀的所有字符串长度,由小到大输出。
注释:$1\le strlen(p)\le 4\cdot 10^5$。
想法:显然,这样的所有的字符串必须满足一些性质。我们预处理出所有p的next数组。然后对于next[len],如果它的next与末尾字符相同,显然这也是一个满足条件的子串,就这样,我们一直跳next,知道==-1停止。
最后,附上丑陋的代码... ...
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 400000
using namespace std;
char p[N+10];
int next[N+10];
int ans[N+10];
void GetNext()//预处理next数组
{
int pLen=strlen(p);
int k=-1;
int j=0;
next[0]=-1;
while(j<pLen)
{
if(k==-1||p[j]==p[k])
{
++k;
++j;
next[j]=k;
}
else k=next[k];
}
}
void original()//初始化
{
memset(next,0,sizeof next);
}
int main()
{
while(~scanf("%s",p))
{
original();
int len=strlen(p);
GetNext();
int cnt=0;
int t=next[len-1];
while(t!=-1)//递归处理
{
if(p[t]==p[len-1]) ans[++cnt]=t+1;
t=next[t];
}
for(int i=cnt;i>=1;i--)
{
printf("%d ",ans[i]);
}
printf("%d\n",len);//不要忘记最后整体字符串也是正确的。
}
return 0;
}
小结:KMP的精髓还是next数组,它的匹配过程还是次要的,重点是next数组的应用。
[poj2752]Seek the Name, Seek the Fame_KMP的更多相关文章
- POJ2752 Seek the Name, Seek the Fame —— KMP next数组
题目链接:https://vjudge.net/problem/POJ-2752 Seek the Name, Seek the Fame Time Limit: 2000MS Memory Li ...
- poj-------------(2752)Seek the Name, Seek the Fame(kmp)
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11831 Ac ...
- poj2752 Seek the Name, Seek the Fame(next数组的运用)
题目链接:id=2752" style="color:rgb(202,0,0); text-decoration:none; font-family:Arial; font-siz ...
- POJ2752 Seek the Name, Seek the Fame 【KMP】
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11602 Ac ...
- Seek the Name, Seek the Fame (poj2752
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14561 Ac ...
- POJ 2752 Seek the Name, Seek the Fame [kmp]
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17898 Ac ...
- poj 2752 Seek the Name, Seek the Fame【KMP算法分析记录】【求前后缀相同的子串的长度】
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14106 Ac ...
- Seek the Name, Seek the Fame(Kmp)
Seek the Name, Seek the Fame Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (J ...
- poj 2752 Seek the Name, Seek the Fame(KMP需转换下思想)
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10204 Ac ...
随机推荐
- 使用ffserver实现转发实时流媒体(摄像头捕获)
本系统为ubuntu 10.04LTS 说明1:本实验在本机成功测试通过: 说明2:本实验仅仅测试了视频流,未测试音频流. 1.配置ffserver.conf -------------------- ...
- Flex和Servlet结合上传文件
Flex和Servlet结合上传文件 1.准备工作 (1)下载文件上传的组件,commons-fileupload-1.3.1.jar (2)下载文件输入输出jar,commons-io-2.4.ja ...
- Unhandled event loop exception No more handles
1.错误描述 2.错误原因 3.解决办法
- do while 循环和while循环的区别
do while 循环和while循环的区别 1.do while循环是先执行循环体,然后判断循环条件,如果为真,则执行下一步循环,否则终止循环: while循环是先判断循环条件,如果条件为真则 ...
- 用OpenStack界面轻松创建虚拟机的你,看得懂虚拟机启动的这24个参数么?
看这篇文章之前,保证看过以下文章: 我是虚拟机内核我困惑?! Qemu,KVM,Virsh傻傻的分不清 裸用KVM创建虚拟机,体验virtualbox为你做的10件事情 大家从OpenStack页面上 ...
- 【转载】Java并发编程:volatile关键字解析(写的非常好的一篇文章)
原文出处: 海子 volatile这个关键字可能很多朋友都听说过,或许也都用过.在Java 5之前,它是一个备受争议的关键字,因为在程序中使用它往往会导致出人意料的结果.在Java 5之后,volat ...
- [LOJ2230][BJOI2014]大融合
题面戳我 sol LCT维护子树size. 开一个数组\(sz_i\)表示一个节点的所有虚儿子的size和,\(sum_i\)表示以一个节点为根的子树的\(size\)和,可见\(sz_u=\sum_ ...
- Java学习者的建议:把自己从一个疯狂下载者&资料的奴隶变成一个真正的学习者
Java学习者的建议:把自己从一个疯狂下载者&资料的奴隶变成一个真正的学习者 你下载的资料看过了多少,请大家好好想想,然后回答一下很多人为了The.Economist花了不少时间,为了下载一个 ...
- python 全栈开发,Day3(正式)
一.基础数据类型 基础数据类型,有7种类型,存在即合理. 1.int 整数 主要是做运算的 .比如加减乘除,幂,取余 + - * / ** %...2.bool 布尔值 判断真假以及作为条件变量3. ...
- Spring源码学习:第0步--环境准备
Spring源码现在已托管于GitHub,相比于以前直接从官网下载一个压缩包的方式来说,确实方便了不少. GitHub地址:https://github.com/spring-projects/spr ...