POJ 2752 Seek the Name, Seek the Fame(求所有既是前缀又是后缀的子串长度)
题目链接:http://poj.org/problem?id=2752
题意:给你一个字符串,求出所有前缀后缀(既是前缀又是后缀的子串)的长度
思路:首先整个字符串肯定既是前缀又是后缀,为最大的前缀后缀。
假设next[len] = k,也即:s[1,k] = s[len-k+1,len]此时s[1,k]是前缀后缀。
处理完next[len]后跳转到next[k+1],用这种方法可以得到所有的前缀后缀。
code:
#include <cstdio>
#include <cstring>
const int MAXN = ;
char str[MAXN];
int next[MAXN];
int ans[MAXN];
void GetNext(int len)
{
int i = ;
int j = -;
next[] = -;
while (i < len)
{
if (- == j || str[i] == str[j]) next[++i] = ++j;
else j = next[j];
}
} int main()
{
while (scanf("%s", str) == )
{
int len = strlen(str);
GetNext(len);
int t = -;
ans[++t] = len;
while (next[len] > )
{
ans[++t] = next[len];
len = next[len];
}
for (int i = t; i > ; --i) printf("%d ", ans[i]);
printf("%d\n", ans[]);
}
return ;
}
POJ 2752 Seek the Name, Seek the Fame(求所有既是前缀又是后缀的子串长度)的更多相关文章
- POJ 2752 Seek the Name, Seek the Fame (KMP的next函数,求前缀和后缀的匹配长度)
给一个字符串S,求出所有前缀,使得这个前缀也正好是S的后缀.升序输出所有情况前缀的长度.KMP中的next[i]的意义就是:前面长度为i的子串的前缀和后缀的最大匹配长度.明白了next[i],那么这道 ...
- POJ-2752 Seek the Name, Seek the Fame(KMP,前缀与后缀相等)
题意: 给出一个字符串str,求出str中存在多少子串,使得这些子串既是str的前缀,又是str的后缀.从小到大依次输出这些子串的长度. 这个就是next数组的应用,next数组真是很深奥啊. ...
- 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 ...
- 「LOJ#10036」「一本通 2.1 练习 2」Seek the Name, Seek the Fame (Hash
题目描述 原题来自:POJ 2752 给定若干字符串(这些字符串总长 ≤4×105 \le 4\times 10^5 ≤4×105),在每个字符串中求出所有既是前缀又是后缀的子串长度. 例如:abab ...
- 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 ...
- POJ 2752 Seek the Name, Seek the Fame(KMP求公共前后缀)
题目链接:http://poj.org/problem?id=2752 题目大意:给你一串字符串s找到所有的公共前后缀,即既是前缀又是后缀的子串. 解题思路: 如图所示 假设字符串pi与jq为符合条件 ...
- POJ 2752 Seek the Name,Seek the Fame(KMP,前缀与后缀相等)
Seek the Name,Seek the Fame 过了个年,缓了这么多天终于开始刷题了,好颓废~(-.-)~ 我发现在家真的很难去学习,因为你还要陪父母,干活,做家务等等 但是还是不能浪费时间啊 ...
- POJ 2752 Seek the Name, Seek the Fame (KMP)
传送门 http://poj.org/problem?id=2752 题目大意:求既是前缀又是后缀的前缀的可能的长度.. 同样是KMP,和 HDU 2594 Simpsons' Hidden Tale ...
- POJ:2753-Seek the Name, Seek the Fame
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Description The little cat is s ...
随机推荐
- 你是否决绝平庸,你有勇气来学C/C++吗,有勇气来检验你是否经得起世界五百强的面试
如果你来传智播客学习 你的目标就是要积累工作经验 有机会参加世界五百强的面试 秒杀世界五百强的面试 赢得高薪的offer! C/C++课程大纲 C语言3周21天 完全掌握C语言的本质,成为一名合 ...
- HDU 5727 Necklace(二分图匹配)
[题目链接]http://acm.hdu.edu.cn/showproblem.php?pid=5727 [题目大意] 现在有n颗阴珠子和n颗阳珠子,将它们阴阳相间圆排列构成一个环,已知有些阴珠子和阳 ...
- POJ 2799 IP Networks
network address是前(32-n)随意 后n位全零 network mask是前(32-n)全一 后n位全零 本题主要利用位移操作,1ULL表示无符号长整型的常数1,这样写可防止不必要的溢 ...
- UNIX文化与历史--初学者必看
UNIX文化与历史 UNIX这个名字早已被众多用户所熟知.作为一个操作系统,它以其独特的魅力----即开放性.可移植性.和多用户多任务等特点,不仅赢得了广大用户的喜爱,同时也受到许多计算机厂商的青睐. ...
- bootstrap-dialog的使用
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 2.Visual Studio 2013中的默认快捷键
这篇大致是IDE的使用技巧,常用的也就那么几个. 我自己用的最多的是注释.取消注释.格式调整.运行测试.开始调试.断开调试.重新开始调试.删除行ctrl+L.保存.全部保存.打开资源管理器.搜索等几个 ...
- MFC知识点整理
1. 在使用VS2010生成基于MFC的应用程序时,在“Visual C++”下选择“MFC”,对话框中间区域会出现三个选项:MFC ActiveX Control.MFC Application和M ...
- C#将图片转化为黑白图片
最近项目需要将上传的图片转化为黑白图片 在网上找了很多资料,测试通过,上代码 using System; using System.Collections.Generic; using System. ...
- VIM中格式化json
在vim输入以下命令就可以格式化:%!python -m json.tool可以在~/.vimrc增加快捷键map <F4><Esc>:%!python -m json.too ...
- XX cannot be resolved to a type
http://www.cnblogs.com/xuxm2007/archive/2011/10/20/2219104.html 我是project->clean...即可 原理:将工程中的.cl ...