链接:

https://vjudge.net/problem/POJ-2752#author=0

题意:

给定若干只含小写字母的字符串(这些字符串总长≤400000),在每个字符串中求出所有既是前缀又是后缀的子串长度。

例如:ababcababababcabab,既是前缀又是后缀的子串:ab,abab,ababcabab,ababcababababcabab。

思路:

从最后开始, 先求出1-len的前缀等于后缀部分, 因为这个前缀等于后缀, 所以再在前缀上找满足条件即可.不断递归, 直到找不到为止.

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
//#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
#include <string>
#include <assert.h>
#include <iomanip>
#include <iostream>
#include <sstream>
#define MINF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int MAXN = 1e6+10; int Next[MAXN];
char s[MAXN], p[MAXN]; void GetNext()
{
int len = strlen(p);
Next[0] = -1;
int j = 0;
int k = -1;
while (j < len)
{
if (k == -1 || p[k] == p[j])
{
++k;
++j;
Next[j] = k;
}
else
k = Next[k];
}
} int main()
{
while (~scanf("%s", p))
{
GetNext();
stack<int> St;
St.push((int)strlen(p));
int len = strlen(p);
while (Next[len] != 0)
{
St.push(Next[len]);
len = Next[len];
}
while (!St.empty())
{
printf("%d ", St.top());
St.pop();
}
puts("");
} return 0;
}

POJ-2752-Seek the Name(KMP, 循环节)的更多相关文章

  1. KMP POJ 2752 Seek the Name, Seek the Fame

    题目传送门 /* 题意:求出一个串的前缀与后缀相同的字串的长度 KMP:nex[]就有这样的性质,倒过来输出就行了 */ /************************************** ...

  2. poj 2406 Power Srings (kmp循环节) (经典)

    <题目链接> 题目大意: 给出一个字符串,求其字串在该字符串中循环的最大周期. 解题分析: length=len-Next[len],len为该字符串的最小循环节,如果len%length ...

  3. Period(KMP,循环节问题)

    题意: 求给你个串,前i位子串由某个字符串重复k次得到,求所有的i和k 分析: i-next[i]恰好是一个循环节 #include <map> #include <set> ...

  4. FZU 1901 Period II(KMP循环节+公共前后缀)

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=1901 题目大意:题目大意求出所有p满足s[i]=s[i+p](i<=len-p) 解题思路: 其实就是要 ...

  5. POJ 2406 Power Strings next数组循环节应用、

    题意:就给出个字符串做*的定义.a^0 = "" (the empty string) and a^(n+1) = a*(a^n).    题目要求n的最大值. 思路: 化简上面的 ...

  6. 【kmp+求所有公共前后缀长度】poj 2752 Seek the Name, Seek the Fame

    http://poj.org/problem?id=2752 [题意] 给定一个字符串,求这个字符串的所有公共前后缀的长度,按从小到达输出 [思路] 利用kmp的next数组,最后加上这个字符串本身 ...

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

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

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

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

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

随机推荐

  1. airflow原理

    官网: http://airflow.apache.org/installation.html 原理: https://www.cnblogs.com/cord/p/9450910.html 原理介绍 ...

  2. 使用Minikube运行一个本地单节点Kubernetes集群

    使用Minikube是运行Kubernetes集群最简单.最快捷的途径,Minikube是一个构建单节点集群的工具,对于测试Kubernetes和本地开发应用都非常有用. ⒈安装Minikube Mi ...

  3. Vue里标签嵌套限制问题解决------解析DOM模板时注意事项:

    受到html本身的一些限制,像<ul>.<ol>.<table>.<select>这样的元素里允许包含的元素有限制,而另一些像<option> ...

  4. LINK : fatal error LNK1104: cannot open file的解决方法

    结果是编译时通过了,但连接(F7)时却显示: LINK : fatal error LNK1104: cannot open file“Debug/1.exe” ============== 上一次运 ...

  5. pip安装源

    目录 介绍 永久配置安装源 Windows MacOS.Linux 配置文件内容 介绍 """ 1.采用国内源,加速下载模块的速度 2.常用pip源: -- 豆瓣:htt ...

  6. Online Meeting CodeForces - 420B (思维)

    大意: 给定某一段连续的上线下线记录, 老板上线或下线时房间无人, 并且每次会议都在场, 求哪些人可能是老板. 结论1: 从未出现过的人一定可以是老板. 结论2: 出现过的人中老板最多只有1个. 结论 ...

  7. Java Web DNS域名解析

    一.什么是DNS DNS(Domain Name System,域名系统),因特网上作为域名和IP地址相互映射的一个分布式数据库,能够使用户更方便的访问互联网,而不用去记住能够被机器直接读取的IP数串 ...

  8. Android获取网络时间的方法

    一.通过免费或者收费的API接口获取 1.免费 QQ:http://cgi.im.qq.com/cgi-bin/cgi_svrtime 淘宝:http://api.m.taobao.com/rest/ ...

  9. 异常-Throwable的几个常见方法

    package cn.itcast_04; import java.text.ParseException; import java.text.SimpleDateFormat; import jav ...

  10. 关于C++内存对齐

    关于C++内存对齐 C++11从标准层面引入了一些和内存对齐相关的特性,标准库也增加了对应的组件,这里稍微总结一下. 取得某个类型的对齐值 C++中的Object(对象)是指一块满足以下条件的内存区域 ...