题目大意

给定一个字符串S,求出所有既是S的前缀又是S的后缀的子串长度

题解

从末尾位置倒推,经过的失配函数值就是题目要求求的

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
#define MAXN 400005
int f[MAXN];
char p[MAXN];
int main()
{
int j;
while(scanf("%s",p)!=EOF)
{
vector<int> ivec;
int len=strlen(p);
f[0]=j=-1;
for(int i=1;i<len;i++)
{
while(j>=0&&p[j+1]!=p[i]) j=f[j];
if(p[j+1]==p[i]) j++;
f[i]=j;
}
j=len-1;
ivec.push_back(len);
while(f[j]>=0)
{
ivec.push_back(f[j]+1);
j=f[j];
}
for(int i=ivec.size()-1;i>=0;i--)
printf("%d ",ivec[i]);
printf("\n");
}
return 0;
}

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 【KMP】

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

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

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

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

  6. POJ 2752 Seek the Name,Seek the Fame(KMP,前缀与后缀相等)

    Seek the Name,Seek the Fame 过了个年,缓了这么多天终于开始刷题了,好颓废~(-.-)~ 我发现在家真的很难去学习,因为你还要陪父母,干活,做家务等等 但是还是不能浪费时间啊 ...

  7. KMP + 求相等前后缀--- POJ Seek the Name, Seek the Fame

    Seek the Name, Seek the Fame Problem's Link: http://poj.org/problem?id=2752 Mean: 给你一个字符串,求这个字符串中有多少 ...

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

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

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

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

    最近在公司离职的前辈写的代码哪里看到了__sync_fetch_and_add这个东东.比较好奇.找些资料学习学习 http://www.lxway.com/4091061956.htm http:/ ...

  2. .net中的Array,ArrayList和List

    Array:任意类型,定长 ArrayList:任意类型,不定长 List:特定类型,不定长 Array和ArrayList是通过存储object类型实现可以存储任意类型数据,使用时需要拆箱和装箱

  3. java转义字符(转载)

    转载自:http://blog.163.com/dingyi_57@126/blog/static/110479195200911229337281/ 一.为什么要使用转义字符? 1.  HTML中& ...

  4. 设置Tomcat应用自动部署目录

    只需要在Tomcat/conf目录下面新建文件夹Catalina/localhost,然后再localhost文件夹下面新建一个[应用名字.xml]文件即可,有多少个应用就新建多少个xml文件即可,x ...

  5. 一步步学习ASP.NET MVC3 (1)——基础知识

    请注明转载地址:http://www.cnblogs.com/arhat 首先在这里我想声明一下,这个ASP.NET MVC3系列是我在授课过程中的一些经验,有什么不对的地方,请大家指出,我们共同的学 ...

  6. appfabric 简单应用

    http://msdn.microsoft.com/en-us/library/ee790941(v=azure.10).aspx Preparing the Visual Studio Projec ...

  7. GO语言函数与类型

    package main import () import ( "fmt" "reflect" "errors" ) type age in ...

  8. 对CURL的一些研究

    http://www.kuqin.com/article/23candcplusplus/586014.html 前两天看到有人求客户端socket 发HTTP包的代码,受flw版主启发找了一些per ...

  9. "Money, Money, Money"

    acdream1408:http://115.28.76.232/problem?pid=1408 题意:给你一个x,让你构造a,b,是的na+bm可以组成大于x的所有的数.a>1,b>1 ...

  10. redis info 各信息意义

    redis_version:2.4.16 # Redis 的版本redis_git_sha1:00000000redis_git_dirty:0arch_bits:64multiplexing_api ...