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算法的更多相关文章

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

  2. 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中的出现位置或者是次数 这个出现的次数是可 ...

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

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

    Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11602   Ac ...

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

  6. Seek the Name, Seek the Fame(Kmp)

    Seek the Name, Seek the Fame Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (J ...

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

    Seek the Name,Seek the Fame 过了个年,缓了这么多天终于开始刷题了,好颓废~(-.-)~ 我发现在家真的很难去学习,因为你还要陪父母,干活,做家务等等 但是还是不能浪费时间啊 ...

  8. KMP + 求相等前后缀--- POJ Seek the Name, Seek the Fame

    Seek the Name, Seek the Fame Problem's Link: http://poj.org/problem?id=2752 Mean: 给你一个字符串,求这个字符串中有多少 ...

  9. KMP POJ 2752 Seek the Name, Seek the Fame

    题目传送门 /* 题意:求出一个串的前缀与后缀相同的字串的长度 KMP:nex[]就有这样的性质,倒过来输出就行了 */ /************************************** ...

随机推荐

  1. MySQL 备份和恢复

      详细地址:http://imysql.cn/mysql_backup_and_recover

  2. OpenGL1-6讲小结

    首先是第一讲,GL窗体的搭建,依葫芦画瓢,很多代码虽然解释了,最后看起来还是比较生涩.一开始按照上一篇的链接去敲的代码,结果最后while死循环了,我也不知道问题出哪儿,后来去找了个源码,还附带了更加 ...

  3. CodeForces 628D Magic Numbers (数位dp)

    题意:找到[a, b]符合下列要求的数的个数. 1.该数字能被m整除 2.该数字奇数位全不为d,偶数位全为d 分析: 1.dp[当前的位数][截止到当前位所形成的数对m取余的结果][当前数位上的数字是 ...

  4. Poj OpenJudge 百练 1062 昂贵的聘礼

    1.Link: http://poj.org/problem?id=1062 http://bailian.openjudge.cn/practice/1062/ 2.Content: 昂贵的聘礼 T ...

  5. Linux进程间通信方法总结

    ①匿名管道(pipe) 匿名管道(pipe)管道是一种半双工的通信方式,数据只能单向流动.如果要进行双工通信,需要建立两个管道.管道只能在具有亲缘关系的进程间使用,例如父子进程或兄弟进程. ②有名管道 ...

  6. 9更令人兴奋的WebGL演示

    Firefox OS,asm.js和推动浏览器性能改进,画布和WebGL技术是打开一个世界的可能性.我上9令人兴奋的帆布演示,然后把它与9 WebGL演示,但我想超越这篇文章.我发现9个更多的精神We ...

  7. JS去除数组中重复值的四种方法

    JS去除数组中重复值的四种方法 1 /// <summary>            o[this[i]] = "";  }      }       newArr.p ...

  8. linux命令详解之useradd命令

    useradd命令使用方法,还包括用户账号的添加.删除与修改.用户口令的管理.用户组的管理方法. Linux系统是一个多用户多任务的分时操作系统,任何一个要使用系统资源的用户,都必须首先向系统管理员申 ...

  9. php威盾解密的例子分享

    例子,批量解密  代码如下 复制代码 <?php/************************************威盾PHP加密专家解密算法 By:zhrt*http://www.111 ...

  10. postgresql 开启远程访问

    1.如果服务器启用了防火墙,需要在防火墙上开启 5432 端口. 2.修改 PostgreSQL 配置文件 postgresql.conf.postgresql.conf,Linux 配置文件所在路径 ...