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 ...
随机推荐
- MYSQL 巧用count,sum进行统计数据
SELECT a.user,count(b.order_id) as subcount,sum(if(b.verifysta='Y',1,0)) as passcount FROM vicidial_ ...
- poj2509---抽k根烟就换一支,求能抽烟的总数
#include <stdio.h> #include <stdlib.h> int main() { int now,k; while(scanf("%d %d&q ...
- swig模板下拉框应用
<div class="form-group"> <label><span class="fa fa-asterisk red"& ...
- Ubuntu下屏幕录像、后期处理不完全攻略
提要 如果要做成果展示或者效果演示,通常需要录取屏幕生成视频文件,在windows中我们可以用屏幕录像专家在录像, vegas 来做后期处理,Ubuntu可以么? 答案时当然可以!虽然第一次用觉得有点 ...
- 腾讯TGideas语义化标签(转)
--------引子--------------- 家里有个熊孩子,经常会有一些意想不到的事情发生:回家的时候,他会笑呵呵冲过来,大声喊着“臭爸爸”:你让他把鞋穿上,他会提起鞋子往楼下扔...在小孩的 ...
- Ubuntu常用命令整理
最近开始用Ubuntu系统了,各种命令很不熟练,想收集一下,以便以后查阅,用这个时常更新的随笔 1.Ubuntu设置与修改用户密码 设置ROOT密码方法:sudo passwd root ,然后输入密 ...
- 桦仔 笔记4-徐 模仿灾难发生时还原adventurework数据库 示例 stopat
1 --模仿灾难发生时还原adventurework数据库 示例 stopat 2 3 BACKUP DATABASE AdventureWorks 4 TO DISK= 'D:\MSSQL\Data ...
- 20141011C#面向对象基础
什么是对象?—— 一切皆为对象.Object 生活中常说的“东西”,就是我们程序里所指的对象. 归类——找模型——抽象 类:class,对某类众多对象共同的特点进行抽象出来的模型 对象——(抽象的过程 ...
- 2014.9.16HTML表单CSS
(一)表格 合并单元格(少用) (合并列) 1.先选中要合并的2个或多个单元格,然后点击以下图标 代码:<td colspan="2"> </td> 2.设 ...
- VC++学习之GDI概述
VC++学习之GDI概述 图形设备接口(GDI)是一个可执行程序,它接受Windows应用程序的绘图请求(表现为GDI函数调用),并将它们传给相应的设备驱动程序,完成特定于硬件的输出,象打印机输出和屏 ...