[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 ...
随机推荐
- RTSP协议分析
RTSP 协议分析 1.概述: RTSP(Real Time Streaming Protocol),实时流传输协议,是TCP/IP协议体系中的一个应用层协议,由哥伦比亚大学.网景和RealNetw ...
- c语言 第二次实验报告
一·实验题目,设计思路,实现方法 11-7 找鞍点 一个矩阵元素的"鞍点"是指该位置上的元素值在该行上最大.在该列上最小. 本题要求编写程序,求一个给定的n阶方阵的鞍点. 利用双重 ...
- 芝麻HTTP:Python爬虫入门之Urllib库的基本使用
1.分分钟扒一个网页下来 怎样扒网页呢?其实就是根据URL来获取它的网页信息,虽然我们在浏览器中看到的是一幅幅优美的画面,但是其实是由浏览器解释才呈现出来的,实质它是一段HTML代码,加 JS.CSS ...
- ASP.Net Core的Code Fist代码先行操作方法
Asp.Net core的Code Fist(代码先行)主要有以下几步: 1.创建实体类 2.创建数据库上下文 3.填加连接字符串 4.依赖注入 5.添加基架工具并执行初始迁移 6搭建模型的基本架构 ...
- 第二篇:操纵MySQL数据库(2) - 基于ORM思想的SQLAlchemy库
前言 本文讲解在Python语言中使用SQLAlchemy库操纵MySQL数据库的方法. 由于具体内容涉及较多,本文仅以插入及展示数据为例,更多内容请查阅有关文档. ORM ORM也即对象 - 关系映 ...
- Android动态类生成预加载-dexmaker使用
一.dexmaker简单介绍 dexmaker是运行在Android Dalvik VM上,利用Java编写,来动态生成DEX字节码的API.如果读者了解AOP编程的话,应该听说过cglib or A ...
- 【BZOJ4237】稻草人(CDQ分治,单调栈)
[BZOJ4237]稻草人(CDQ分治,单调栈) 题面 BZOJ 题解 \(CDQ\)分治好题呀 假设固定一个左下角的点 那么,我们可以找到的右下角长什么样子??? 发现什么? 在右侧是一个单调递减的 ...
- [SDOI2012]Longge的问题
题目大意: 网址:https://www.luogu.org/problemnew/show/P2303 大意:给定一个N,求\(\Sigma_{i=1}^N gcd(i, N);\). 题目解法: ...
- [Luogu2057]善意的投票
题目戳我 题目描述 幼儿园里有n个小朋友打算通过投票来决定睡不睡午觉.对他们来说,这个问题并不是很重要,于是他们决定发扬谦让精神.虽然每个人都有自己的主见,但是为了照顾一下自己朋友的想法,他们也可以投 ...
- iOS逆向工程,(狗神)沙梓社大咖免费技术分享。
序言 简介:本文针对于广大iOS开发者,作为一名开发者,仅仅专注于一门语言可能已经不适用现在的市场需求,曾经因高薪和需求量巨大,而火爆一时的移动端开发者(Android,ios),如今的路却是不再那么 ...