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数组的应用。

转载于:https://www.cnblogs.com/ShuraK/p/8626091.html

[poj2752]Seek the Name, Seek the Fame_KMP相关推荐

  1. 【hash】Seek the Name, Seek the Fame

    [哈希和哈希表]Seek the Name, Seek the Fame 题目描述 The little cat is so famous, that many couples tramp over ...

  2. KMP POJ 2752 Seek the Name, Seek the Fame

    题目传送门 1 /* 2 题意:求出一个串的前缀与后缀相同的字串的长度 3 KMP:nex[]就有这样的性质,倒过来输出就行了 4 */ 5 /**************************** ...

  3. KMP Seek the Name,Seek the Fame

    Seek the Name,Seek the Fame 时间限制: 1 Sec 内存限制: 128 MB 题目描述 给定一个字符串s,从小到大输出s中既是前缀又是后缀的子串的长度. 字符串长度不超过四 ...

  4. Seek the Name, Seek the Fame(KMP-next数组详解)

    Seek the Name, Seek the Fame 目录 Seek the Name, Seek the Fame 解题过程 举个例子 AC代码 The little cat is so fam ...

  5. poj-2752 Seek the Name, Seek the Fame **

    /* * KMP **/ #include <cstdio>#include <cstring>using namespace std; const int maxL = 40 ...

  6. Seek the Name, Seek the Fame POJ - 2752 (理解KMP函数的失配)既是S的前缀又是S的后缀的子串

    题意:给一个字符串S, 求出所有前缀pre,使得这个前缀也正好是S的后缀. 输出所有前缀的结束位置. 就是求前缀和后缀相同的那个子串的长度  然后从小到大输出,主要利用next数组求解. 例如 &qu ...

  7. 【POJ - 2752】Seek the Name, Seek the Fame (KMP,公共前缀后缀长度及个数)

    题干: The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked t ...

  8. python seek函数whence_file.seek(offset[, whence])

    file.seek(offset[, whence]) 描述 (Description) 方法seek()设置文件在偏移处的当前位置. whence参数是可选的,默认为0,表示绝对文件定位,其他值为1 ...

  9. seek 方法java,Seek.java

    package stuManage; import java.sql.*; import java.awt.*; import java.awt.event.*; import javax.swing ...

最新文章

  1. 0909 编译原理1
  2. C语言中extern关键字的使用
  3. python数据可视化的特点_python的数据分析到底是啥?python数据可视化怎么做?
  4. django1.8读书笔记模版高级进阶
  5. 阿里巴巴的AI都能做英文阅读理解了 你还不赶快多背几个单词?
  6. Mountain Lion 10.8
  7. python把英语句子成分字母_英语句子成分
  8. TA-LIB】之MACD
  9. STM32F03 SPI通信 连续传送24位数据(DMA或者不用)
  10. 06.输入系统:第10课第20节_输入系统_多点触摸_电容屏驱动程序
  11. 双评价技术指南2020_2020年双11 千元以下人体工学椅选购指南,不交智商税
  12. 微信小程序 slot插槽基本使用
  13. 为何很多 App 图标使用白色做底色?
  14. 企业微信直播服务器,企业微信直播平台的利用
  15. 基于JAVA的实现学生卡管理系统
  16. 两位小数乘两位小数竖式_小数乘法竖式
  17. 【Python学习】爬虫报错处理bs4.FeatureNotFound
  18. HDU - 5546
  19. 【随机共振】基于随机共振的高灵敏度GPS信号捕获算法
  20. 2021年中国3D+TSV设备市场趋势报告、技术动态创新及2027年市场预测

热门文章

  1. Android中使用logwrapper来重定向应用程序的标准输出
  2. storm之topology的启动
  3. Android调用系统照相机
  4. A10 平板开发一硬件平台搭建
  5. “Runtime Error”不产生dump文件的解决办法
  6. (转)Flex4中的皮肤(2):Skin State
  7. SetTimer OnTimer WM_TIMER
  8. uploadify动态改变参数
  9. python工程控件_工程脚本插件方案 - c集成Python基础篇
  10. Linux下yum命令详解