题目链接: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(求所有既是前缀又是后缀的子串长度)的更多相关文章

  1. POJ 2752 Seek the Name, Seek the Fame (KMP的next函数,求前缀和后缀的匹配长度)

    给一个字符串S,求出所有前缀,使得这个前缀也正好是S的后缀.升序输出所有情况前缀的长度.KMP中的next[i]的意义就是:前面长度为i的子串的前缀和后缀的最大匹配长度.明白了next[i],那么这道 ...

  2. POJ-2752 Seek the Name, Seek the Fame(KMP,前缀与后缀相等)

    题意:    给出一个字符串str,求出str中存在多少子串,使得这些子串既是str的前缀,又是str的后缀.从小到大依次输出这些子串的长度. 这个就是next数组的应用,next数组真是很深奥啊. ...

  3. 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 ...

  4. 「LOJ#10036」「一本通 2.1 练习 2」Seek the Name, Seek the Fame (Hash

    题目描述 原题来自:POJ 2752 给定若干字符串(这些字符串总长 ≤4×105 \le 4\times 10^5 ≤4×105),在每个字符串中求出所有既是前缀又是后缀的子串长度. 例如:abab ...

  5. 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 ...

  6. POJ 2752 Seek the Name, Seek the Fame(KMP求公共前后缀)

    题目链接:http://poj.org/problem?id=2752 题目大意:给你一串字符串s找到所有的公共前后缀,即既是前缀又是后缀的子串. 解题思路: 如图所示 假设字符串pi与jq为符合条件 ...

  7. POJ 2752 Seek the Name,Seek the Fame(KMP,前缀与后缀相等)

    Seek the Name,Seek the Fame 过了个年,缓了这么多天终于开始刷题了,好颓废~(-.-)~ 我发现在家真的很难去学习,因为你还要陪父母,干活,做家务等等 但是还是不能浪费时间啊 ...

  8. POJ 2752 Seek the Name, Seek the Fame (KMP)

    传送门 http://poj.org/problem?id=2752 题目大意:求既是前缀又是后缀的前缀的可能的长度.. 同样是KMP,和 HDU 2594 Simpsons' Hidden Tale ...

  9. 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 ...

随机推荐

  1. HDU 5972 Regular Number(ShiftAnd+读入优化)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5972 [题目大意] 给出一个字符串,找出其中所有的符合特定模式的子串位置,符合特定模式是指,该子串 ...

  2. svn 问题汇总

    1.当删除了原来的仓库时,再次新建,更新版本时会出现这个问题:

  3. cors技术

    简称跨域资源共享: 若是配置nodejs: 需在公共路由添加三句话:代码如下: // 全局头设置 app.all('*', function(req, res, next) { res.set({ ' ...

  4. SVN使用Tips

    1. 如果在本地删除了某个文件,在Cornerstone上的本地仓库会出现D的标志,并且文件不存在. 这时,只需要将该文件提交到服务器上,本地仓库就会清除了已删除的文件的标识,同时,服务器上也会自动删 ...

  5. Java实现串口通信的小样例

    用Java实现串口通信(windows系统下),须要用到sun提供的串口包 javacomm20-win32.zip.当中要用到三个文件,配置例如以下: 1.comm.jar放置到 JAVA_HOME ...

  6. getCacheDir()、getFilesDir()、getExternalFilesDir()、getExternalCacheDir()的作用

    getCacheDir()方法用于获取/data/data/<application package>/cache目录 getFilesDir()方法用于获取/data/data/< ...

  7. C# 文件帮助类

    using System; using System.Data; using System.Configuration; using System.Linq; using System.Web; us ...

  8. This project references NuGet package(s) that are missing on this computer.

    Install Nuget. Right click on the solution and select "Enable NuGet Package Restore". Clic ...

  9. isEmpty()

    String a = new String(); 此时a是分配了内存空间,但值为空,是绝对的空,是一种有值(值存在为空而已) String b = ""; 此时b是分配了内存空间, ...

  10. .net 资源

    基于.net构架的留言板项目大全源码 http://down.51cto.com/zt/70 ASP.net和C#.net通用权限系统组件功能教程 http://down.51cto.com/zt/1 ...