POJ2752 Seek the Name, Seek the Fame —— KMP next数组
题目链接:https://vjudge.net/problem/POJ-2752
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 21220 | Accepted: 11065 |
Description
Step1. Connect the father's name and the mother's name, to a new string S.
Step2. Find a proper prefix-suffix string of S (which is not only the prefix, but also the suffix of S).
Example: Father='ala', Mother='la', we have S = 'ala'+'la' = 'alala'. Potential prefix-suffix strings of S are {'a', 'ala', 'alala'}. Given the string S, could you help the little cat to write a program to calculate the length of possible prefix-suffix strings of S? (He might thank you by giving your baby a name:)
Input
Restrictions: Only lowercase letters may appear in the input. 1 <= Length of S <= 400000.
Output
Sample Input
ababcababababcabab
aaaaa
Sample Output
2 4 9 18
1 2 3 4 5
Source
题解:
找出即使前缀又是后缀的子串。
kmp的next数组的利用:对len通过next[]数组进行回溯,直到长度为1。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
typedef long long LL;
const double eps = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 2e6+; char x[MAXN];
int Next[MAXN]; void get_next(char x[], int m)
{
int i, j;
j = Next[] = -;
i = ;
while(i<m)
{
while(j!=- && x[i]!=x[j]) j = Next[j];
Next[++i] = ++j;
}
} int ans[MAXN];
int main()
{
while(scanf("%s", x)!=EOF)
{
int len = strlen(x);
get_next(x, len); int cnt = ;
for(int length = len; length!=; length = Next[length])
ans[++cnt] = length; for(int i = cnt; i>=; i--)
printf("%d ", ans[i]);
printf("\n");
}
}
POJ2752 Seek the Name, Seek the Fame —— KMP next数组的更多相关文章
- POJ 2752 Seek the Name, Seek the Fame (KMP next 数组 变形)
题意:给一个字符串S,判断在什么下标的时候,前缀和后缀相等,输出前缀和后缀相等的点. 分析:next数组的一种很巧妙的用法 next数组表示的意义是当前下标前面k字符和开头的前面k个字符相等 所以就会 ...
- poj-------------(2752)Seek the Name, Seek the Fame(kmp)
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11831 Ac ...
- POJ2752 Seek the Name, Seek the Fame 【KMP】
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11602 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 ...
- Seek the Name, Seek the Fame(Kmp)
Seek the Name, Seek the Fame Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (J ...
- POJ 2752 Seek the Name,Seek the Fame(KMP,前缀与后缀相等)
Seek the Name,Seek the Fame 过了个年,缓了这么多天终于开始刷题了,好颓废~(-.-)~ 我发现在家真的很难去学习,因为你还要陪父母,干活,做家务等等 但是还是不能浪费时间啊 ...
- KMP + 求相等前后缀--- POJ Seek the Name, Seek the Fame
Seek the Name, Seek the Fame Problem's Link: http://poj.org/problem?id=2752 Mean: 给你一个字符串,求这个字符串中有多少 ...
- KMP POJ 2752 Seek the Name, Seek the Fame
题目传送门 /* 题意:求出一个串的前缀与后缀相同的字串的长度 KMP:nex[]就有这样的性质,倒过来输出就行了 */ /************************************** ...
- 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 ...
随机推荐
- 自定义Navigation按钮及Title
导航栏自带的按钮,时常不能满足要求,所以深深需要进行各种定制. 写一个UINavigationItem的category // UINavigationItem+CB_ChangeButton.h ...
- mysql合并和时间函数
sql:利用group_concat()方法,参数为需要合并的字段,合并的字段分隔符默认为逗号,可通过参数separator指定,该方法往往配合group by 一起使用.利用group_concat ...
- 因chmod /usr致使raspberryPi重装
一.系统安装noobs 设置用户名及密码,设置超级用户root密码: sudo passwd root,回车后按提示输入两次root的密码(注意,输入时是不会提示*号的,直接输入即可) 二.源及软件 ...
- Win10 - 默认图片查看器恢复
1. 新建一个文本文件“1.txt” 2. 在“1.txt”中添加如下代码,并保存: Windows Registry Editor Version 5.00 ; Change Extension's ...
- 【Todo】Java类面试题分析
Java 面试中的重要话题 多线程,并发及线程基础数据类型转换的基本原则垃圾回收(GC)Java 集合框架数组字符串GOF 设计模式SOLID (单一功能.开闭原则.里氏替换.接口隔离以及依赖反转)设 ...
- Linux Shell高级技巧
Linux Shell高级技巧(一) http://www.cnblogs.com/stephen-liu74/archive/2011/12/22/2271167.html一.将输入信息转换为大写字 ...
- 关于Memcached的CAS和Set方法造成Socket泄漏的问题
为了解决多并发下写Memcached的冲突方案,我们项目组引入了CAS机制.类同于Java并发包中的CAS(Compareand set)原子操作,用来处理同一个Item被多个线程更改的并发问题.Me ...
- ci中 chrome对favicon.ico请求,导致log报错 解决方法
chrome浏览器会自动对favicon.ico进行请求,所以当没有为网站添加 favicon.ico 时会出现404的页面错误在实际开发过程中,CI框架 打开 ERROR log,会一直显示ERRO ...
- 【转载】C# sleep 和wait的区别
eep和wait都是使线程暂时停止执行的方法,但它们有很大的不同. 1. sleep是线程类Thread 的方法,它是使当前线程暂时睡眠,可以放在任何位置. 而wait,它是使当前线程暂时放弃对象的使 ...
- iOS提交应用至App Store流程及真机调试 一,证书、配置文件
前言:你要有苹果开发人员账号,我用的是个人账号.其次xcode为xcode 7,因为xcode 7须要 os x 系统 10.11 或更高的版本号, 所以os x 系统也须要 10.11 或更高的版本 ...