需要理解next[]的意义。之前看到大牛的博客,next[]讲的非常清楚。

利用next[],当前位子的前面那一段和next[当前位子]的前面那一段是相同的。又next[next[当前位子]]与next[当前位子]有一段相同,所以当前位子的前面一段和

next[next[当前位子]]的前一段有相同即可。

#include<stdio.h>
#include<string.h>
#include<stack>
using namespace std;
#define maxn 400010
int next[maxn];
char s[maxn];
void getnext()
{
int j,k,len=strlen(s);
next[]=-;
j=;
k=-;
while(j<len)
{
if(k==-||s[j]==s[k])
{
j++;
k++;
next[j]=k;
}
else k=next[k];
}
}
int main()
{
int i,j,flag;
while(scanf("%s",s)!=EOF)
{
flag=;
stack<int>sta;
getnext();
i=strlen(s);
sta.push(i);
while(next[i]!=)
{
sta.push(next[i]);
i=next[i];
}
while(!sta.empty())
{
int ans=sta.top();
sta.pop();
if(flag==)
{
flag=;
printf("%d",ans);
}
else printf(" %d",ans);
}
printf("\n");
}
}

poj2752 KMP的更多相关文章

  1. POJ-2752(KMP算法+前缀数组的应用)

    Seek the Name, Seek the Fame POJ-2752 本题使用的算法还是KMP 最主要的片段就是前缀数组pi的理解,这里要求解的纸盒pi[n-1]有关,但是还是需要使用一个循环来 ...

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

  3. POJ2752 Seek the Name,Seek the Fame KMP算法

    KMP继续练手.题目问的是一个串前缀等于后缀的可能长度是哪些,输出来.题目考的是对KMP失配指针的理解,当最后一位失配(即'\0'那里)时,指针会移动到前缀对应匹配的部分,所以这个长度是我们要的,然后 ...

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

    Description The little cat is so famous, that many couples tramp over hill and dale to Byteland, and ...

  5. poj-2752(拓展kmp)

    题意:求一个串所有的前后缀字串: 解题思路:kmp和拓展kmp都行,个人感觉拓展kmp更裸一点: 拓展kmp: #include<iostream> #include<algorit ...

  6. POJ-2752 Seek the Name, Seek the Fame 字符串问题 KMP算法 求前后缀串相同数木

    题目链接:https://cn.vjudge.net/problem/POJ-2752 题意 给一个字符串,求前缀串跟后缀串相同的前缀串的个数 例:alala 输出:a, ala, alala 思路 ...

  7. KMP——POJ-3461 Oulipo && POJ-2752 Seek the Name, Seek the Fame && POJ-2406 Power Strings && POJ—1961 Period

    首先先讲一下KMP算法作用: KMP就是来求在给出的一串字符(我们把它放在str字符数组里面)中求另外一个比str数组短的字符数组(我们叫它为ptr)在str中的出现位置或者是次数 这个出现的次数是可 ...

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

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

    题意:    给出一个字符串str,求出str中存在多少子串,使得这些子串既是str的前缀,又是str的后缀.从小到大依次输出这些子串的长度. 这个就是next数组的应用,next数组真是很深奥啊. ...

随机推荐

  1. docker containerd shim分析

    // containerd-shim is a small shim that sits in front of a runtime implementation that allows it to ...

  2. 软件工程实训项目案例--Android移动应用开发

    实训过程 角色分工 1.项目经理:负责项目的组织实施,制定项目计划,并进行跟踪管理 2.开发人员:对项目经理及项目负责 3.需求分析员:负责系统的需求获取和分析,并协助设计人员进行系统设计 4.系统设 ...

  3. 边工作边刷题:70天一遍leetcode: day 89

    Word Break I/II 现在看都是小case题了,一遍过了.注意这题不是np complete,dp解的time complexity可以是O(n^2) or O(nm) (取决于inner ...

  4. codeforces 721B B. Passwords(贪心)

    题目链接: B. Passwords time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  5. jquery和css自定义video播放控件

    下面介绍一下通过jquery和css自定义video播放控件. Html5 Video是现在html5最流行的功能之一,得到了大多数最新版本的浏览器支持.包括IE9,也是如此.不同的浏览器提供了不同的 ...

  6. Git版本控制工具(一)----git的安装及创建版本库

    ​[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...

  7. iOS中NSLog的输出格式

    %@ 对象%d %i 整数%u 无符整形%f 浮点/双字%x, %X 二进制整数%o 八进制整数%zu size_t%p 指针 ; NSLog(@"%p",&tem); & ...

  8. 在文本中匹配链接并添加A标签

    (?<!href="|">)(https?:\/\/[\w\-\.!~?&=+\*\'(),\/]+)((?!\<\/\a\>).)* 这个正则可以 ...

  9. 09SpringMvc_再次讲一下SpringMvc的工作流:

    整个SpringMvc的流程图:

  10. MVC ajaxSubmit上传图片

    注意事项: 1.提交form,必须引用jquery.form.min.js 2.不要使用mvc自带的Ajax.Form() 1.页面cshtml <form name="frmInpu ...