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 ...
随机推荐
- spring mvc 提交表单的例子
1. 构建MAVEN项目,然后转换成web格式,结构图如下: 2. 通过@RequestMapping来进行配置,当输入URL时,会以此找到对应方法执行,首先调用setupForm方法,该方法主要是生 ...
- 菜鸟级SQL Server21天自学通(文档+视频)
SQL语言的主要功能就是同各种数据库建立联系,进行沟通.按照ANSI(美国国家标准协会)的规定,SQL被作为关系型数据库管理系统的标准语言.SQL语句可以用来执行各种各样的操作,例如更新数据库中的数据 ...
- #include <map>
//tuple多元数组,必须是静态数组,类似结构体 //配合array,vector使用 //std::tuple<数组元素类型>数组变量名(数组元素变量名); #include < ...
- 04737_C++程序设计_第10章_面向对象设计实例
10.6.2 使用包含的参考程序及运行结果. 头文件cpp10.h 源文件cpp10.cpp 源文件Find10.cpp 头文件cpp10.h #if ! defined(CPP10_H) #defi ...
- Windows7中搭建Android x86_64及armv8-a操作步骤
1. 从https://developer.android.com/tools/sdk/ndk/index.html 下载android-ndk-r10d-windows-x86_64. ...
- Android建立模拟器进行调试
安装好android开发环境后.用到下面几个命令.android, adb, emulator android - 最主要的android命令.能够进行sdk更新,列出设备源,生成虚拟设备等. adb ...
- 深入理解Linux网络技术内幕——中断与网络驱动程序
接收到帧时通知驱动程序 在网络环境中.设备(网卡)接收到一个数据帧时,须要通知驱动程序进行处理. 有一下几种通知机制: 轮询: 内核不断检查设备是否有话要说.(比較耗资源,但在一些情况 ...
- excel导出、导入功能
public class ExcelHelper { #region 数据导出至Excel文件 /// </summary> /// web导出Excel文件,自动返回可下载的文件流 // ...
- ThinkPHP第二十四天(JQuery常用方法、TP自动验证)
---恢复内容开始--- 1.JQuery常用方法 A:JS中可以用json格式数据当做数组使用,如var validate={username:false,pwd:false,pwded:false ...
- Linux系统环境变量的四个配置文件的关系
Linux系统环境变量配置有四个文件分别是: /etc/environment,/etc/profile,/etc/bash.bashrc,~/.bashrc 各配置文件意义 /etc/environ ...