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 过了个年,缓了这么多天终于开始刷题了,好颓废~(-.-)~ 我发现在家真的很难去学习,因为你还要陪父母,干活,做家务等等 但是还是不能浪费时间啊 ...
随机推荐
- Thingworx新建Thing的数据库表变化
为了在Thingworx的基础上建立统一的可视化平台,并且对软件产品具有自主控制权,不依赖于Thingworx软件(防止因Thingworx的升级.Bug导致的自主扩展功能受制),所以最近在研究Thi ...
- easyui的增删改
陈旧的开发模式PM:“我要这个功能”后端:“这个先找前端做个模板”前端:“模板做完了”后端:“我来对接一下,这里样式不对”前端:“我改完了”后端:“功能交付”PM:“春节要加这个活动”后端:“这个先找 ...
- 路飞学城Python-Day32【小结】
import socket from multiprocessing import Process def talk(conn): while True: try: data = conn.recv( ...
- 流媒体应用程序Mobdro或存在安全隐患
Mobdro是一款流媒体应用程序,可以安装在任何Android设备上,包括手机,平板电脑,亚马逊的Fire TV Stick和Google的Chromecast.它现在已经流行了一段时间,特别是在围绕 ...
- 从YV12到NV12
Media SDK的decoder,vpp,encoder对输入输出格式有着严格的限制,现在仅仅支持NV12.那么如何从其他格式转化为NV12是日常工作中经常遇到的事情.本篇文章以此为目的,讨论如何将 ...
- 报错:SyntaxError: Non-ASCII character '\xe4' in file
SyntaxError: Non-ASCII character '\xe1' in file recommendation.py on line 1, but no encoding declare ...
- 简单实现双向数据绑定mvvm。
- JavaString库
String库 .length() 字符串的长度,一个字符串为空(空字符串对象)和null(不指向任何对象)是两个概念,中文字符和英文字符是一样的计数(一个中文是一个字符,一个英文字母是一个字符) . ...
- Linux 上安装 Zookeepr
一.下载Zookeeper 百度网盘:https://pan.baidu.com/s/1BHV6vHcHIuj7lalvvR7w_g 密码:csvk 二.解压缩包 tar -zxvf zookeepe ...
- HDU 4334 Contest 4
本来以为是一道水题,好吧,做了才知道,出题的人有多牛.二分搜索是不可能的了,因为会超内存... 看到别人的搜索两个集合的提示,我就自己一边去想了.终于想出来了: 可以这样做,先把每两个集合的和值枚举出 ...