POJ2752 Seek the Name,Seek the Fame KMP算法
KMP继续练手.题目问的是一个串前缀等于后缀的可能长度是哪些,输出来.题目考的是对KMP失配指针的理解,当最后一位失配(即'\0'那里)时,指针会移动到前缀对应匹配的部分,所以这个长度是我们要的,然后接着这个新的前缀的失配指针移到的部分,与这个前缀的后缀也是匹配的..这样一直滚下去就可以了得到所有可能的值.
贴一记代码.
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
#define mxp 400050 int f[mxp+50];
char P[mxp+50]; void getFail(const char *P,int *f)
{
int m=strlen(P);
f[0]=f[1]=0;
for(int i=1;i<m;++i){
int j=f[i];
while(j&&P[i]!=P[j]) j=f[j];
f[i+1]= P[i]==P[j]? j+1:0;
}
} int main()
{
while(scanf("%s",P)!=EOF)
{
getFail(P,f);
int n=strlen(P);
vector<int> ans;
while(n){
ans.push_back(n);
n=f[n];
}
reverse(ans.begin(),ans.end());
printf("%d",ans[0]);
for(int i=1;i<ans.size();++i){
printf(" %d",ans[i]);
}
puts("");
}
return 0;
}
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 ...
- 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 ...
- POJ2752 Seek the Name, Seek the Fame 【KMP】
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11602 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 过了个年,缓了这么多天终于开始刷题了,好颓废~(-.-)~ 我发现在家真的很难去学习,因为你还要陪父母,干活,做家务等等 但是还是不能浪费时间啊 ...
- KMP + 求相等前后缀--- POJ Seek the Name, Seek the Fame
Seek the Name, Seek the Fame Problem's Link: http://poj.org/problem?id=2752 Mean: 给你一个字符串,求这个字符串中有多少 ...
- KMP POJ 2752 Seek the Name, Seek the Fame
题目传送门 /* 题意:求出一个串的前缀与后缀相同的字串的长度 KMP:nex[]就有这样的性质,倒过来输出就行了 */ /************************************** ...
随机推荐
- 数值积分NIntegrate中的具体算法
数值积分方法很多,Mathematica中至提供了NIntegrate.具体算法可参照官方帮助. http://reference.wolfram.com/language/tutorial/NInt ...
- Makefile中=、:=、+=、?=的区别
=,就是基本的赋值 :=,覆盖原来的值 +=,添加新值 ?=,如果没有赋值则赋值
- 2_1我的第一个应用hello world[wp8特色开发与编程技巧]
2_1hello world -5min 大家好,我是徐文康,在上一个视频当中我们已经讲了,如何根据自己电脑系统去下载相应的SDK. 你可能花了很多时间去安装以及配置好了这个开发环境,如果还没有配置好 ...
- SQL语句执行顺寻
SQL语句执行的时候是有一定顺序的.理解这个顺序对SQL的使用和学习有很大的帮助. 1.from 先选择一个表,或者说源头,构成一个结果集. 2.where 然后用where对结果集进行筛选.筛选出需 ...
- RHEL(RedHat Enterprise Linux)5/6 ISO镜像下载
本文贴出了RHEL(RedHat Enterprise Linux)发行版本中常用的服务器版本的ISO镜像文件,供大家下载学习使用,贴出的版本有RedHat Enterprise Linux(RHEL ...
- 运行yum报错Error: Cannot retrieve metalink for repository: epel. Please verify its path and try again
今天给Centos通过rpm -Uvh装了个epel的扩展后,执行yum就开始报错: Error: Cannot retrieve metalink for repository: epel. Ple ...
- 【面试虐菜】—— LVS负载均衡
Load Balancer(负载均衡器): Load Balancer是整个集群系统的前端,负责把客户请求转发到Real Server上.Load Balancer通过Ldirectord监测各Rea ...
- 提高Linux安全性--hosts.allow, hosts.deny 文件修改方法
有一种办法来提高Linux安全性--修改 hosts.allow , hosts.deny 这2个文件来配置 允许某个ip访问, 或者禁止访问. 可以通过这种方式设置限制 sshd 的远程访问, 只允 ...
- (一)使用log4net生成日志文件
1.引入log4net.dll 1.1 Nuget安装 或 http://logging.apache.org/log4net/下载log4net的源代码,编译后把log4net.dll引入项目. 2 ...
- WPF-数据绑定:日期时间格式
WPF-数据绑定:日期时间格式绑定后自定义格式的例子. 我刚才遇到的问题是绑定完之后,星期始终显示为英文.需要一个属性ConverterCulture制定区域. 如下: {Binding dateti ...