【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不是一个 ...
随机推荐
- HDOJ/HDU 2562 奇偶位互换(交换位置~)
Problem Description 给定一个长度为偶数位的0,1字符串,请编程实现串的奇偶位互换. Input 输入包含多组测试数据: 输入的第一行是一个整数C,表示有C测试数据: 接下来是C组测 ...
- Google文档
本博文的主要内容有 .Google文档的介绍 1.Google文档的介绍 https://zh.wikipedia.org/wiki/Google%E6%96%87%E4%BB%B6 http://d ...
- Coprimes - SGU 102(求互质数,水)
题目大意:给你一个正整数N,求出来不超过N 的并且与N互质的正整数的个数. 就是一个大水题~~~ 代码: #include<stdio.h> #include<string.h> ...
- java通过解析文件获取apk版本等信息
import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import ...
- 【MySQL性能优化】改进MySQL Order By Rand()的低效率
<a href="http://click.aliyun.com/m/9153/">点击查看原文</a> 正 文: 最近由于需要研究了一下MYSQL的随 ...
- 开发期间的GWT设置---加快编译速度
随着项目功能的完善,GWT模块(Module)越来越多,当要以web模式编译给测试组使用时,编译的总时间越来越多,我的机器编译完8个模块,需要10分钟左右. 抽空研究了一下GWT的编译参数和GWT编译 ...
- 移动开发框架,第【一】弹:QuoJs 官方文档(汉化版)
作者:一只猿 原文地址: http://www.92ez.com 转载请注明出处,谢谢 帮助说明 如果您认为QuoJS只是一个触摸事件管理器,那你就错了,它是一个功能丰富的类库,无需第三方JavaSc ...
- cocos2d-x3.0 ListView
.h #include "cocos2d.h" #include "cocos-ext.h" #include "ui/CocosGUI.h" ...
- C# 该行已经属于还有一个表 的解决方法
产生错误的代码: DataTable dtContract_src = Oper.GetDataTable("select * from T_Contract where ProjectID ...
- 树莓派安装mjpg-streamer视频监控 分类: Raspberry Pi 2015-04-12 23:41 144人阅读 评论(0) 收藏
原来使用Motion在树莓派上跑1280x720分辨率的三颗摄像头.占用内存太严重,关闭诸多功能之后还是不行.故转战mjpg-streamer. 首先安装所需软件 sudo apt-get insta ...