poj2752 KMP
需要理解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的更多相关文章
- POJ-2752(KMP算法+前缀数组的应用)
Seek the Name, Seek the Fame POJ-2752 本题使用的算法还是KMP 最主要的片段就是前缀数组pi的理解,这里要求解的纸盒pi[n-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 ...
- POJ2752 Seek the Name,Seek the Fame KMP算法
KMP继续练手.题目问的是一个串前缀等于后缀的可能长度是哪些,输出来.题目考的是对KMP失配指针的理解,当最后一位失配(即'\0'那里)时,指针会移动到前缀对应匹配的部分,所以这个长度是我们要的,然后 ...
- 【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 ...
- poj-2752(拓展kmp)
题意:求一个串所有的前后缀字串: 解题思路:kmp和拓展kmp都行,个人感觉拓展kmp更裸一点: 拓展kmp: #include<iostream> #include<algorit ...
- POJ-2752 Seek the Name, Seek the Fame 字符串问题 KMP算法 求前后缀串相同数木
题目链接:https://cn.vjudge.net/problem/POJ-2752 题意 给一个字符串,求前缀串跟后缀串相同的前缀串的个数 例:alala 输出:a, ala, alala 思路 ...
- 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中的出现位置或者是次数 这个出现的次数是可 ...
- 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 ...
- POJ-2752 Seek the Name, Seek the Fame(KMP,前缀与后缀相等)
题意: 给出一个字符串str,求出str中存在多少子串,使得这些子串既是str的前缀,又是str的后缀.从小到大依次输出这些子串的长度. 这个就是next数组的应用,next数组真是很深奥啊. ...
随机推荐
- Debian下安装deb格式安装包
dpkg -i 软件包名称 就好啦 下面是相应链接: http://blog.csdn.net/lhf_tiger/article/details/7493400
- 看程序写结果(program)
看程序写结果(program) Time Limit:1000ms Memory Limit:64MB 题目描述 LYK 最近在准备 NOIP2017 的初赛,它最不擅长的就是看程序写结果了,因此它拼 ...
- Eclipse c++头文件问题(未完)
http://stackoverflow.com/questions/7905025/string-could-not-resolved-error-in-eclipse-for-c-eclipse- ...
- ios app架构设计系统文章
三. iOS应用架构谈(三):网络层设计方案(上) http://www.infoq.com/cn/articles/ios-app-arch-3-1?utm_source=infoq&utm ...
- Mvc 中ViewBag Model 查找不到解决
按照晚上修改VIew视图中web.config 很显然,没效果... 我的情况是 出现: 我想了下 看下是不是编译器的问题: 我在vs2013中建立 MVC4 然后用 vs2015打开 然后报错- ...
- js实现复制功能
JS 点击复制Copy 1.实现点击按钮,复制文本框中的的内容 1 <script type="text/javascript"> 2 function copyUrl ...
- 使用DataAnnotations实现数据验证
https://msdn.microsoft.com/zh-cn/library/system.componentmodel.dataannotations.aspx http://www.cnblo ...
- [转]redis.conf的配置解析
# redis 配置文件示例 # 当你需要为某个配置项指定内存大小的时候,必须要带上单位, # 通常的格式就是 1k 5gb 4m 等酱紫: # # 1k => 1000 bytes # 1kb ...
- C语言 复杂队列(链表队列)
//复杂的队列二 --链表队列 #include<stdio.h> #include<stdlib.h> #define datatype int struct queueli ...
- Apache Options Indexes FollowSymLinks详解
禁止显示Apache目录列表-Indexes FollowSymLinks如何修改目录的配置以禁止显示 Apache 目录列表.缺省情况下如果你在浏览器输入地址: http://localhost:8 ...