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. php 连接mssql

    以前用的都是mysql,今天突然想用下mssql,起先用的是sql server200. 第一种方法 打开mssql.dll拓展.然后把mssql.secure_connection = off改为o ...

  2. freemarker报错之十五

    1.错误描述 六月 04, 2014 11:04:03 下午 freemarker.log.JDK14LoggerFactory$JDK14Logger error 严重: Template proc ...

  3. dijit.byId("grid") is undefined

    1.错误描述 TypeError:dijit.byId(...) is undefined     (68 out of range 3) 2.错误原因    var gridName = dijit ...

  4. iOS - XMPP Openfire 服务器的搭建

    前言 提前下载好相关软件,且安装目录最好安装在全英文路径下.如果路径有中文名,那么可能会出现一些莫名其妙的问题. 提前准备好的软件: jdk-8u91-macosx-x64.dmg mysql-5.7 ...

  5. 申请Jetbrain教育帐号,免费使用一年

    JetBrains是一家捷克的软件开发公司.旗下IDE产品有(不限于):(1) IntelliJ,IDEA  Java集成开发工具:(2) PHPStorm,PHP 集成开发工具:(3) PyChar ...

  6. RobotFramework自动化测试框架-常用断言关键字

    断言关键字 描述 Should Be Empty 判断是否为空,如果不为空,执行失败,示例: ${value} Set Variable Hello Should Be Empty ${value} ...

  7. css3动画结束捕捉事件整理

    //捕捉webkitAnimationEnd事件 element.addEventListener('webkitAnimationEnd', end, false); //捕捉webkitTrans ...

  8. 【SPOJ】Longest Common Substring(后缀自动机)

    [SPOJ]Longest Common Substring(后缀自动机) 题面 Vjudge 题意:求两个串的最长公共子串 题解 \(SA\)的做法很简单 不再赘述 对于一个串构建\(SAM\) 另 ...

  9. emacs配置

    原配置 (global-set-key [f9] 'compile-file) (global-set-key [f10] 'gud-gdb) (global-set-key (kbd "C ...

  10. 关系型数据库工作原理-事务管理(二)(翻译自Coding-Geek文章)

    本文翻译自Coding-Geek文章:< How does a relational database work>. 原文链接:http://coding-geek.com/how-dat ...