POJ-2752 Seek the Name, Seek the Fame 字符串问题 KMP算法 求前后缀串相同数木
题目链接:https://cn.vjudge.net/problem/POJ-2752
题意
给一个字符串,求前缀串跟后缀串相同的前缀串的个数
例:alala
输出:a, ala, alala
思路
仔细想想,fail[len]的返回值其实就是匹配成功的最大后缀串
得到这个后缀串后,比这个串更小的串一定还是被包含在这个新的后缀串中
迭代即可
提交过程
| AC |
代码
#include <cstring>
#include <cstdio>
const int maxm=4e5+20;
char P[maxm];
int fail[maxm];
void getFail(int m){
fail[0]=fail[1]=0;
for (int i=1; i<m; i++){
int j=fail[i];
while (j && P[j]!=P[i]) j=fail[j];
fail[i+1]=((P[i]==P[j])?j+1:0);
}
}
int main(void){
int len, ans[maxm], kase=0;
while (scanf("%s", P)==1 && !(P[0]=='.' && !P[1])){
getFail(len=strlen(P));
int size=len, ptr=1; ans[0]=len;
while (size>0)
ans[ptr++]=(size=fail[size]);
for (int i=ptr-2; i>=0; i--)
printf("%d%c", ans[i], "\n "[!!i]);
}
return 0;
}
| Time | Memory | Length | Lang | Submitted |
|---|---|---|---|---|
| 485ms | 3840kB | 547 | G++ | 2018-08-02 11:27:13 |
POJ-2752 Seek the Name, Seek the Fame 字符串问题 KMP算法 求前后缀串相同数木的更多相关文章
- 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 3376 Finding Palindromes(manacher求前后缀回文串+trie)
题目链接:http://poj.org/problem?id=3376 题目大意:给你n个字符串,这n个字符串可以两两组合形成n*n个字符串,求这些字符串中有几个是回文串. 解题思路:思路参考了这里: ...
- (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(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 过了个年,缓了这么多天终于开始刷题了,好颓废~(-.-)~ 我发现在家真的很难去学习,因为你还要陪父母,干活,做家务等等 但是还是不能浪费时间啊 ...
随机推荐
- 浅谈AVL树,红黑树,B树,B+树原理及应用
背景:这几天在看<高性能Mysql>,在看到创建高性能的索引,书上说mysql的存储引擎InnoDB采用的索引类型是B+Tree,那么,大家有没有产生这样一个疑问,对于数据索引,为什么要使 ...
- Clonezilla制作镜像时报错: errextfsclone.c:bitmap free count err
在检查时选中下面的选项, 如下图: -fsck-src-part Interactively check and repair source file
- rem 自适应布局 bootstrap 移动端适配
移动端适配用:rem 自使用布局用:bootstrap
- Pyhton学习——Day58
From表单验证 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- BZOJ 1016 最小生成树计数(矩阵树定理)
我们把边从小到大排序,然后依次插入一种权值的边,然后把每一个联通块合并. 然后当一次插入的边不止一条时做矩阵树定理就行了.算出有多少种生成树就行了. 剩下的交给乘法原理. 实现一不小心就会让程序变得很 ...
- HDU 5763 Another Meaning (KMP/哈希+DP)
题目大意:给你两个串,一长一短,如果长串中某个子串和短串完全相同,则这个子串可以被替换成"#",求长串所有的表达形式....... 比如"hehehehe"和& ...
- 升级ruby到2.0
本文部分内容转载,如侵犯个人利益请联系博客管理员及时删除,或留言之评论区 一.安装库 Yum install –y gcc* openssl* wget 二.安装ruby wget https://c ...
- 光盘文件的挂载和yum源配置
一.挂载光盘文件 1.将光盘推入 2.新建挂载点 mkdir /mnt/cdrom 3.挂载 3.1临时挂载 mount /dev/dcrom /mnt/cdrom 或者 mount –t iso ...
- LeetCode OJ 之 Number of Digit One (数字1的个数)
题目: Given an integer n, count the total number of digit 1 appearing in all non-negative integers les ...
- NHibernate3剖析:Query篇之NHibernate.Linq增强查询
系列引入 NHibernate3.0剖析系列分别从Configuration篇.Mapping篇.Query篇.Session策略篇.应用篇等方面全面揭示NHibernate3.0新特性和应用及其各种 ...