POJ 2752 Seek the Name, Seek the Fame(KMP中next的理解)题解
题意:
要求你给出每个前后缀相同的串的长度,比如: "alala"的前缀分别为{"a", "al", "ala", "alal", "alala"}, 后缀分别为{"a", "la", "ala", "lala", "alala"}. 其中有{"a", "ala", "alala"}是相同的,所以答案是1,3,5。
思路:
本题需要灵活运用next数组,要理解next的含义才能做。
next[i]代表长度为i的子串的前后缀匹配值,我们需要先求出当前主串的最大匹配度k,也就是前k个和后k个一样。然后我们怎么得到s[1...k-1]和s[len-k+2...len]是否相等(这里的数字不是下标),即前k-1个和后k-1个怎么比较是否相同。这里就需要深入理解next了。我们得到了k那么next[next[k]]代表着前k个的前缀和后k个的后缀的最大匹配度,然后如此递归即可得到答案。这里建议画图理解一下
代码:
#include<iostream>
#include<algorithm>
const int N = 400000+5;
const int INF = 0x3f3f3f3f;
using namespace std;
int fail[N],ans[N];
char p[N];
void getFail(){
fail[0] = -1;
int j = 0,k = -1;
int len = strlen(p);
while(j <= len){
if(k == -1 || p[j] == p[k]){
fail[++j] = ++k;
}
else{
k = fail[k];
}
}
}
/*int KMP(){
int num = 0;
getFail();
int i = 0,j = 0;
int lent = strlen(t),lenp = strlen(p);
while(i < lent){
if(j == -1 || t[i] == p[j]){
i++;
j++;
if(j == lenp) num++;
}
else{
j = fail[j];
}
}
return num;
}*/
int main(){
int cnt,Case = 1;
while(scanf("%s",p) != EOF){
getFail();
cnt = 1;
int len = strlen(p);
ans[0] = len; //本身肯定是
len = fail[len]; //最大前后缀匹配值
while(fail[len] >= 0){
ans[cnt++] = len;
len = fail[len];
}
for(int i = cnt - 1;i >=0;i--){
if(i != cnt-1) printf(" ");
printf("%d",ans[i]);
}
printf("\n");
}
return 0;
}
POJ 2752 Seek the Name, Seek the Fame(KMP中next的理解)题解的更多相关文章
- (KMP)Seek the Name, Seek the Fame -- poj --2752
http://poj.org/problem?id=2752 Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536 ...
- Seek the Name, Seek the Fame POJ - 2752
Seek the Name, Seek the Fame POJ - 2752 http://972169909-qq-com.iteye.com/blog/1071548 (kmp的next的简单应 ...
- KMP POJ 2752 Seek the Name, Seek the Fame
题目传送门 /* 题意:求出一个串的前缀与后缀相同的字串的长度 KMP:nex[]就有这样的性质,倒过来输出就行了 */ /************************************** ...
- 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: 10204 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 ...
- POJ 2752 Seek the Name, Seek the Fame(next数组运用)
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24000 ...
- POJ 2752 Seek the Name,Seek the Fame(KMP,前缀与后缀相等)
Seek the Name,Seek the Fame 过了个年,缓了这么多天终于开始刷题了,好颓废~(-.-)~ 我发现在家真的很难去学习,因为你还要陪父母,干活,做家务等等 但是还是不能浪费时间啊 ...
- poj 2752 Seek the Name, Seek the Fame (KMP纯模版)
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13840 Ac ...
随机推荐
- CH0102 64位整数乘法 数论
正解:数论/一个神仙想法 解题报告: 先放传送门qwq 两种方法,都还挺妙的就都写了qwq 第一种是快速幂 把b用二进制表示成,ck*2k+ck-1*2k-1+...+c0*20 然后就可以表示成,a ...
- 在mysql Navicat中怎样设置ID自动递增
1.打开设计表 2.在添加或变更表结构时,把id字段设置为整型,下面的选项就会出现auto increment的选择框,勾选中就可以了.
- sql优化 性能快速定位
sql server sql性能快速定位 简介 对于写出实现功能的SQL语句和既能实现功能又能保证性能的SQL语句的差别是巨大的.很多时候开发人员仅仅是把精力放在实现所需的功能上,而忽略了其所写代码的 ...
- java-JProfiler(四)-HelloWorld示例
1.程序代码 package jProfiler; public class Test extends Thread{ public static void main(String[] args) t ...
- java-JProfiler(二)-进行本地JVM的性能监控-tomcat
监视本地的Tomcat, 看似是本地,其实JProfiler GUI在一个单独的JVM里启动,他与被监视的目标jvm之间通过socket通讯,目的为了不干扰目标JVM.所以监视本地Tomcat与监视远 ...
- [vue]webpack&vue组件工程化实践
[vue]全局组件和局部组件(嵌套+props引用父组件数据) [vue]组件篇 [vue]组件的创建(componet)和销毁(keep-alive缓存)和父子dom同步nextTick [vue] ...
- Legal or Not(模板题)
本来以为这题能用并查集做的,但一想不对 例如A-> B,A->C如果用并查集的话B与C就不能连了,但实际B可以是C的徒弟,所以这题是考拓扑排序. #include<stdio.h&g ...
- storm并发机制,通信机制,任务提交
一.storm的并发 (1)Workers(JVMs):在一个物理节点上可以运行一个或多个独立的JVM进程.一个Topology可以包含一个或多个worker(并行的跑在不同的物理机上),所以work ...
- nginx之gzlib压缩,expires缓存
gzip压缩网页内容的压缩编码与传输速度优化我们观察news.163.com的头信息请求:Accept-Encoding:gzip,deflate,sdch响应:Content-Encoding:gz ...
- python 模拟windows键盘按键的封装
代码:在执行的时候,把光标放在指定的地方,在此例中,点击运行后把光标放到结果区域,粘贴的时候是粘贴到光标所在的问题,如过是运行脚本在web元素输入框中输入的话,不能移动光标到其他位置 #encodin ...