H - Seek the Name, Seek the Fame
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
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
using namespace std;
#define MAXN 400001
typedef long long LL;
/*
真正理解Next[]数组的含义
Next[j] = k的含义是
在数组里0-k的前缀和k-j+1-k的后缀相同,k是前j-1个元素里最大的相同前缀后缀长度 那么题目要求求出所有相同前缀后缀长度,显然字符串总长度是一个解
由Next[]数组的定义,Next[len]也是一个解(如果有解)——如果Next[len]==0显然说明无解
那么通过递归的思想继续求更小的解:由于Next[len]==k那么说明 其他解如果存在 只能在[0,k]和
[len-k+1,len]中产生,而由于他们完全相同可以只考虑其中一个,比如[0,k]
此时问题的形式是求一个序列所有前缀后缀 所以可以递归求解
*/
char s[MAXN];
int Next[MAXN];
void kmp_pre(int m)
{
int j,k;
j = ;k = Next[] = -;
while(j<m)
{
if(k==-||s[j]==s[k])
Next[++j] = ++k;
else
k = Next[k];
}
}
void Print(int tmp)
{
if(Next[tmp])
{
Print(Next[tmp]);
printf("%d ",Next[tmp]);
}
}
int main()
{
while(scanf("%s",s)!=EOF)
{
int l = strlen(s);
kmp_pre(l);
int tmp = l;
Print(tmp);
printf("%d\n",l);
}
}
H - Seek the Name, Seek the Fame的更多相关文章
- 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 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10204 Ac ...
- POJ 2752 Seek the Name, Seek the Fame(next数组运用)
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24000 ...
- POJ 2751:Seek the Name, Seek the Fame(Hash)
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24077 Ac ...
- (KMP)Seek the Name, Seek the Fame -- poj --2752
http://poj.org/problem?id=2752 Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536 ...
- poj 2752 Seek the Name, Seek the Fame (KMP纯模版)
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13840 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】
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11602 Ac ...
- 【hash】Seek the Name, Seek the Fame
[哈希和哈希表]Seek the Name, Seek the Fame 题目描述 The little cat is so famous, that many couples tramp over ...
随机推荐
- php的类型转换
转自:http://www.tianzhigang.com/article.asp?id=280 PHP的数据类型转换属于强制转换,允许转换的PHP数据类型有: (int).(integer):转换成 ...
- redis-缓存穿透,缓存雪崩,缓存击穿,并发竞争
目录 缓存穿透 定义 解决方案 利用互斥锁 采用异步更新策略 使用布隆过滤器 空置缓存 缓存雪崩 定义 解决方案 给缓存的加一个随机失效时间 使用互斥锁 双缓存策略 缓存击穿 定义 解决方案 使用互斥 ...
- int(3)和int(11)区别
- C/C++ Python的函数默认参数
发现C/C++ Python的函数可以使用默认参数,来减少传参时候的参数个数. 但是:这样的默认参数最好是不变对象! #include <stdio.h> #include <st ...
- RabbitMQ~消息的产生和管理(15672)
上一讲说了rabbitmq在windows环境的部署,而今天主要说一下消息在产生后,如何去查看消息,事实上,rabbitmq为我们提供了功能强大的管理插件,我们只要开启这个插件即可,它也是一个网站,端 ...
- 11.Layers, Containers and Interfaces
图层.容器和接口 当设计一个Ventuz场景时,某些节点的组合或设计会反复出现.例如在演示中使用的按钮或滑块,在整个过程中的呈现和外观都是一致的,唯一变化的是尺寸.位置和标签. 在设计复杂的演示时,另 ...
- python框架之Flask基础篇(一)
一.第一个hello world程序 # coding=utf-8 from flask import Flask app = Flask(__name__) @app.route('/') def ...
- Android 从服务器获取时间戳转换为年月日
用JAVA相关类转换.代码如下: Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(NumberUtils.ge ...
- Android基础TOP3_1:纵横屏切换
在Res下建立layout-port文件夹 为竖屏时加载的界面: 建立layout-land 文件夹 为横屏加载的界面
- html——标签选择器
交集选择器:标签+类(ID)选择器{属性:值:}.即要满足使用了某个标签,还要满足使用了类(id)选择器. <!DOCTYPE html> <html> <head> ...