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 ...
随机推荐
- LoadRunner-关联问题(栏目列表较多关联不了想要的id)
新建了课程后之后有很多栏目,每个栏目对应一个partid,但我只想要期中一个. http://*********/course/work/workInfo.action?hwid=1547&c ...
- JDK 伪共享解决方案
关于AtomicReference AtomicReference是由JAVA5引入的,用于对一个对象引用进行原子操作,我们可以看到AtomicReference的实现是用CAS技术对引用进行指令级别 ...
- 查看win信任的证书办法机构(CA机构的公钥)
cmd mmc
- 深入了解oracle存储过程的优缺点
定义: 存储过程(Stored Procedure )是一组为了完成特定功能的SQL 语句集,经编译后存储在数据库中.用户通过指定存储过程的名字并给出参数(如果该存储过程带有参数)来执行它.存储过程是 ...
- 帝国cms栏目别名怎样调用?栏目名称太短了
在用帝国cms创建栏目时一般会填写栏目名称(较短)和栏目别名(为空则与栏目名相同),栏目别名可以设置长一些作为栏目标题,可是如何调用帝国cms栏目别名呢?默认的模板标题调用是<title> ...
- OC convertRect
举个例子: redView = [[UIView alloc]initWithFrame:CGRectMake(50, 100, 100, 100)]; redView.backgroundColor ...
- 为什么*p++等于*(p++)?
你要先搞懂i++与++i的区别.i++是先赋值再自增,对于指针也是一样的.所以*p++是先取值,然后p再自增.加个括号还是一样的,*(p++)括号里面的内容还是p++,所以还是要先取值然后p再自增. ...
- UUID生成字符串
在向数据库插入新数据时,可能需要插入字符串形式的ID,这时使用UUID可以生成随机字符串: String str = UUID.randomUUID().toString();
- 查看项目中的laravel的版本
方法1: 使用php artisan --version 方法2: 在项目文件中找vendor\laravel\framework\src\Illuminate\Foundation\Applicat ...
- java实现Comparable接口和Comparator接口,并重写compareTo方法和compare方法
原文地址https://segmentfault.com/a/1190000005738975 实体类:java.lang.Comparable(接口) + comareTo(重写方法),业务排序类 ...