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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. poj2752 Seek the Name, Seek the Fame(next数组的运用)

    题目链接:id=2752" style="color:rgb(202,0,0); text-decoration:none; font-family:Arial; font-siz ...

  4. POJ2752 Seek the Name, Seek the Fame 【KMP】

    Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11602   Ac ...

  5. Seek the Name, Seek the Fame (poj2752

    Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14561   Ac ...

  6. 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 ...

  7. 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 ...

  8. Seek the Name, Seek the Fame(Kmp)

    Seek the Name, Seek the Fame Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (J ...

  9. 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 ...

随机推荐

  1. 基于嵌入式操作系统VxWorks的多任务并发程序设计(1)――基本概念

    1引言 嵌入式系统定义义为:嵌入到对象体系中的专用计算机系统."嵌入性"."专用性"与"计算机系统"是嵌入式统的三个基本要素,对象系统则是指 ...

  2. 【mongodb系统学习之一】mongodb的简单安装

    linux中mongodb的安装(最简单的): 1.下载mongodb安装包,这里用mongodb-linux-x86_64-2.6.9.gz 提供一个下载地址:http://pan.baidu.co ...

  3. Error Code: 1305. FUNCTION student.rand_string does not exist

    1.错误描述 13:52:42 call new_procedure Error Code: 1305. FUNCTION student.rand_string does not exist 0.0 ...

  4. Count:2org.apache.batik.transcoder.TranscoderException: null

    1.错误描述 Count:2org.apache.batik.transcoder.TranscoderException: null Enclosed Exception: The current ...

  5. java.lang.NumberFormatException

    1.错误描述 Exception in thread "main" java.lang.NumberFormatException: For input string: " ...

  6. .Net+SQL Server企业应用性能优化笔记—精确查找瓶颈

    首先我们需要部署一个测试环境,将Web项目的源代码拷到测试环境Web服务器IIS上,使得可以直接通过IE访问我们的网站.SQL Server环境可以部署在同一台机器上,条件允许的话有专门的数据库测试服 ...

  7. 关于省市联动的bug

    一,问题描述 1.1,原来的思路 1.1.1,初始化加载省份 $.ajax({ 'type' : 'POST', 'dataType' : 'json', 'url' : '${rc.contextP ...

  8. Dynamics CRM中一个查找字段引发的【血案】

    摘要: 本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复267或者20180311可方便获取本文,同时可以在第一间得到我发布的最新的博文信息,follow me!我的网站是 www.luoyon ...

  9. 【Webpack的使用指南 01】Webpack入门

    使用Webpack有一段时间了,但是感觉之前学的用的都比较零散,所以在这里整理一下Webpack的使用知识,从入门到进阶. 创建项目 首先创建最简单的一个项目 npm init 得到以下项目结构: 安 ...

  10. 解决Android Studio 3.0导入module依赖后unable to merge index

    解决Android Studio 3.0导入module依赖后unable to merge index 项目需要使用im, 在项目里导入了腾讯im的几个module依赖, 项目无法编译, 报错una ...