POJ-2752-Seek the Name(KMP, 循环节)
链接:
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, 循环节)的更多相关文章
- KMP POJ 2752 Seek the Name, Seek the Fame
题目传送门 /* 题意:求出一个串的前缀与后缀相同的字串的长度 KMP:nex[]就有这样的性质,倒过来输出就行了 */ /************************************** ...
- poj 2406 Power Srings (kmp循环节) (经典)
<题目链接> 题目大意: 给出一个字符串,求其字串在该字符串中循环的最大周期. 解题分析: length=len-Next[len],len为该字符串的最小循环节,如果len%length ...
- Period(KMP,循环节问题)
题意: 求给你个串,前i位子串由某个字符串重复k次得到,求所有的i和k 分析: i-next[i]恰好是一个循环节 #include <map> #include <set> ...
- FZU 1901 Period II(KMP循环节+公共前后缀)
题目链接:http://acm.fzu.edu.cn/problem.php?pid=1901 题目大意:题目大意求出所有p满足s[i]=s[i+p](i<=len-p) 解题思路: 其实就是要 ...
- POJ 2406 Power Strings next数组循环节应用、
题意:就给出个字符串做*的定义.a^0 = "" (the empty string) and a^(n+1) = a*(a^n). 题目要求n的最大值. 思路: 化简上面的 ...
- 【kmp+求所有公共前后缀长度】poj 2752 Seek the Name, Seek the Fame
http://poj.org/problem?id=2752 [题意] 给定一个字符串,求这个字符串的所有公共前后缀的长度,按从小到达输出 [思路] 利用kmp的next数组,最后加上这个字符串本身 ...
- POJ 2752 Seek the Name, Seek the Fame (KMP)
传送门 http://poj.org/problem?id=2752 题目大意:求既是前缀又是后缀的前缀的可能的长度.. 同样是KMP,和 HDU 2594 Simpsons' Hidden Tale ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- [转帖]RedHat 如何更改网卡名 从ens192 改为eth0的问题
RedHat 如何更改网卡名 从ens192 改为eth0的问题 2017年03月27日 17:50:47 the_conquer_zzy 阅读数 2416 版权声明:本文为博主原创文章,遵循CC ...
- Mysql创建、使用循环函数
创建函数 create procedure names() begin declare i int default 0; while i < 3000 do INSERT INTO studen ...
- 链表操作Java实现
单链表 存储结构 public class ListNode { int i; ListNode next; ListNode(int i) { this.i = i; } public String ...
- Smarty内置函数之capture
capture的作用是: 捕获模板输出的数据并将其存储到一个变量,而不是把它们输出到页面,任何在 {capture name="foo"}和{/capture}之间的数据将被存储到 ...
- nodejs+gulp+webpack基础知识
nodejs+gulp+webpack基础知识 2019年08月22日 11:49:40 天府云创 阅读数 22 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文 ...
- 六一广告页H5全屏滚动效果实现
明天就六一儿童了(放假了),在这里提前祝大家周末快乐,每逢节假日公司必然会推出h5活动页的需求,这次六一儿童节也不例外,产品这次倒是没提什么互动效果需求,只不过根据UI妹子给的设计图,图片与图片中颜色 ...
- mysql 批量 insert 数据丢失问题
这两天发现mysql 批量insert 比如600条数据后,页面马上select就查询到580条,但是等几秒钟再查询就有600条(也有部分情况是永久只能查到580条了) 查看mybatis的日志发现循 ...
- S=a+aa+aaa...(js)
/*求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字. 例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制.*/ let readline = ...
- 最新Cocoapods 安装及使用
1.移除现有Ruby默认源 gem sources --remove https://rubygems.org/ 2.使用新的源 gem sources -a https://ruby.taobao. ...
- postgres 索引
索引是一种特殊的查询表,可以使用搜索引擎的数据库以加快数据检索.简单地说,索引是表中的数据的一个指针,在一个数据库中的索引是非常相似,如:一本书的目录. 例如,如果想在一本书中引用的所有页面讨论某个话 ...