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 过了个年,缓了这么多天终于开始刷题了,好颓废~(-.-)~ 我发现在家真的很难去学习,因为你还要陪父母,干活,做家务等等 但是还是不能浪费时间啊 ...
随机推荐
- 搭建hadoop、hdfs环境--ubuntu(完全分布式)
最近在学习hadoop相关知识,就在本机上安装了hadoop,遇到了一些坑,也学到了不少.仅此记录我的安装过程,及可能遇到的问题.供参考.交流沟通见页末. 软件准备 > 虚拟机(VMware) ...
- sklearn学习5-----模型评估(1) 分类度量
一.分类度量 1.混淆矩阵: 2.classification_report 3.汉明损失 4.jaccard相似系数得分 5.准确率.召回率和F_measure 3.
- bootstrap fileinput控件 + django后台上传、回显简单使用
一.控件下载:https://github.com/kartik-v/bootstrap-fileinput/ 官网:http://plugins.krajee.com/file-input 需要引入 ...
- 线性回归(regression)
简介 回归分析只涉及到两个变量的,称一元回归分析.一元回归的主要任务是从两个相关变量中的一个变量去估计另一个变量,被估计的变量,称因变量,可设为Y:估计出的变量,称自变量,设为X. 回归分析就是要找出 ...
- 【codeforces 508E】Artur and Brackets
[题目链接]:http://codeforces.com/problemset/problem/508/E [题意] 让你构造一个括号字符串; 使得每个从左往右数第i个左括号在这个括号序列中与之匹配的 ...
- [Hibernate]Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
使用Hibernate官方文档上的下面代码进行測试时报出这个异常. org.hibernate.HibernateException: Access to DialectResolutionInfo ...
- Java多线程之~~~线程安全容器的非堵塞容器
在并发编程中,会常常遇到使用容器.可是假设一个容器不是线程安全的.那么他在多线程的插入或者删除的过程 中就会出现各种问题.就是不同步的问题.所以JDK提供了线程安全的容器,他能保证容器在多线程的情况下 ...
- 省赛i题/求1~n内全部数对(x,y),满足最大公约数是质数的对数
求1~n内全部数对(x,y),gcd(x,y)=质数,的对数. 思路:用f[n]求出,含n的对数.最后用sum[n]求和. 对于gcd(x,y)=a(设x<=y,a是质数),则必有gcd(x/a ...
- Mysql 数据迁移后 启动出错
今天上班后不知道为什么,mysql一直无法启动,折腾了半天于是决定重装 我本地的server用的是wamp , 重装的时候, 要进行数据备份 , 我使用的最简单粗暴的备份方式, 就是直接进入到mysq ...
- 怎样訪问pcie整个4k的配置空间
眼下用于訪问PCIe配置空间寄存器的方法须要追溯到原始的PCI规范. 为了发起PCI总线配置周期,Intel实现的PCI规范使用IO空间的CF8h和CFCh来分别作为索引和数据寄存器,这样的方法能够訪 ...