【POJ2752】【KMP】Seek the Name, Seek the Fame
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
/*
登科后
唐代
孟郊 昔日龌龊不足夸,今朝放荡思无涯。
春风得意马蹄疾,一日看尽长安花。
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <utility>
#include <iomanip>
#include <string>
#include <cmath>
#include <queue>
#include <assert.h>
#include <map>
#include <ctime>
#include <cstdlib>
#include <stack>
#define LOCAL
const int MAXN = + ;
const int INF = ;
const int SIZE = ;
const int MAXM = + ;
const int maxnode = 0x7fffffff + ;
using namespace std;
int l1, l2;
char a[MAXN];
int next[MAXN];//不用开太大了..
int Ans[MAXN];
void getNext(){
//初始化next数组
next[] = ;
int j = ;
for (int i = ; i <= l1; i++){
while (j > && a[j + ] != a[i]) j = next[j];
if (a[j + ] == a[i]) j++;
next[i] = j;
}
return;
}
/*int kmp(){
int j = 0, cnt = 0;
for (int i = 1; i <= l2; i++){
while (j > 0 && a[j + 1] != b[i]) j = next[j];
if (a[j + 1] == b[i]) j++;
if (j == l1){
cnt++;
j = next[j];//回到上一个匹配点
}
}
return cnt;
}*/ /*void init(){
scanf("%s", a + 1);
scanf("%s", b + 1);
l1 = strlen(a + 1);
l2 = strlen(b + 1);
}*/ int main(){
int T; while (scanf("%s", a + ) != EOF){
int tot = ;
l1 = strlen(a + );
getNext();
int tmp = next[l1];
if (tmp != ) Ans[tot++] = tmp;
while (next[tmp] != ){
tmp = next[tmp];
Ans[tot++] = tmp;
}
for (int i = tot - ; i >= ; i--) printf("%d ", Ans[i]);
printf("%d\n", l1);
}
/*scanf("%s", a + 1);
l1 = strlen(a + 1);
getNext();
for (int i = 1; i <= l1; i++) printf("%d" , next[i]);*/
return ;
}
【POJ2752】【KMP】Seek the Name, Seek the Fame的更多相关文章
- POJ2752 Seek the Name, Seek the Fame 【KMP】
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11602 Ac ...
- 【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算法分析记录】【求前后缀相同的子串的长度】
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14106 Ac ...
- 【KMP】【最小表示法】NCPC 2014 H clock pictures
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1794 题目大意: 两个无刻度的钟面,每个上面有N根针(N<=200000),每个 ...
- 【动态规划】【KMP】HDU 5763 Another Meaning
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 题目大意: T组数据,给两个字符串s1,s2(len<=100000),s2可以被解读成 ...
- 【洛谷】3375 KMP字符串匹配
[算法]KMP [题解][算法]字符串 #include<cstdio> #include<algorithm> #include<cstring> using n ...
- HDOJ 2203 亲和串 【KMP】
HDOJ 2203 亲和串 [KMP] Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 【kmp】bzoj3620: 似乎在梦中见过的样子
考察kmp理解题 Description “Madoka,不要相信 QB!”伴随着 Homura 的失望地喊叫,Madoka 与 QB 签订了契约. 这是 Modoka 的一个噩梦,也同时是上个轮回中 ...
- 【KMP】Censoring
[KMP]Censoring 题目描述 Farmer John has purchased a subscription to Good Hooveskeeping magazine for his ...
- 【KMP】OKR-Periods of Words
[KMP]OKR-Periods of Words 题目描述 串是有限个小写字符的序列,特别的,一个空序列也可以是一个串.一个串P是串A的前缀,当且仅当存在串B,使得A=PB.如果P≠A并且P不是一个 ...
随机推荐
- activity跳转关闭软件盘
之前试过 if(getWindow().getAttributes().softInputMode==WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPE ...
- Linux的IP设置参考
位置:etc/network/interfaces 内容: 第一段是网口1自动从DHCP处获得IP 第二段是网口2静态分配IP 如果是IPv6,请把 iface eth0 inet dhcp(stat ...
- Apache+PHP 环境上传文件配置
打开php.ini 配置文件,查找 File Uploads ,在这个区域有以下3个选项: file_uploads = On 是否允许HTTP文件上传.默认值为On允许HTTP文件上传,此选项不能设 ...
- Codevs 3304 水果姐逛水果街Ⅰ 线段树
题目: http://codevs.cn/problem/3304/ 时间限制: 2 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题解 题目描述 D ...
- 在word 2013中输入latex公式
注意:版权所有,转载请注明出处 向word输入LaTeX公式,插件有很多,前面在使用的是一个叫做Aurora的插件,结果不是免费的,用了一段时间就要收费是,所以就不用了,从网上找到别人的介绍,可以使用 ...
- Nearly prime numbers - SGU 113(素数)
题目大意:判断一个数是否是两个素数的乘积,如果是,输出Yes,否则No. 分析:先打表求出来一部分素因子,用素数对素数判定还是比较快的. 代码如下: ========================= ...
- c#基础语言编程-Path和Directory
引言 在程序常会对文件操作,在对文件操作中需要对文件路径的进行定位,在.Net中针对寻找文件提供两个静态类以供调用,Path和Directory. Path类 来自命名空间SYstem.IO,Path ...
- 【Android - MD】之NavigationView的使用
NavigationView是Android 5.0新特性--Material Design中的一个布局控件,可以结合DrawerLayout使用,让侧滑菜单变得更加美观(可以添加头部布局). Nav ...
- 初次接触GWT,知识点总括
初次接触GWT,知识点概括 前言 本人最近开始研究 GWT(Google Web Toolkit) ,现将个人的一点心得贴出来,希望对刚开始接触 GWT的程序员们有所帮助,也欢迎讨论,共同进步. 先说 ...
- 【android】ImageView的src和background的区别以及两者的妙用
一.ImageView中XML属性src和background的区别: background会根据ImageView组件给定的长宽进行拉伸,而src就存放的是原图的大小,不会进行拉伸 .src是图片内 ...