【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不是一个 ...
随机推荐
- HDU-4902 Nice boat
Nice boat Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Tot ...
- HDU-2176 取(m堆)石子游戏
http://acm.hdu.edu.cn/showproblem.php?pid=2176 第三种博弈,但一定要注意优化时间 取(m堆)石子游戏 Time Limit: 3000/1000 MS ( ...
- MFC图形处理
关于MFC绘图功能入门 创建Dialog based MFC工程 打开Resource view 下的Dialog主界面 添加Picture control控件,为此控件添加CStatic的co ...
- 《University Calculus》-chaper8-无穷序列和无穷级数-泰勒定理的证明
泰勒定理: 证明:
- Centos6快速yum lamp
yum install httpd httpd-devel mysql mysql-server mysql-devel php php-mysql php-common php-gd php-mb ...
- ExtractNewFolderPath
unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, Syst ...
- 海思android4.4 SDK编译Latin输入法
原来的HiSTBAndroidV500R001C01SPC020\device\hisilicon\bigfish\packages\apps\HiLatinIME\Android.mk内容例如以下: ...
- COMPACT 行记录格式
CREATE TABLE `mytest` ( `t1` varchar() DEFAULT NULL, `t2` varchar() DEFAULT NULL, `t3` ) DEFAULT NUL ...
- qt 状态栏
有段时间没有写过博客了.假期去上海旅游,所以一直没有能够上网.现在又来到这里,开始新的篇章吧! 今天的内容主要还是继续完善前面的那个程序.我们要为我们的程序加上一个状态栏. 状态栏位于主窗口的 ...
- WCF - 地址
WCF顾名思义 即解决在windows平台下与各种平台中的程序之间通信的问题 而终结点则是WCF通信的唯一手段 终结点承载了所有通信的功能 一个WCF服务是通过对应的终结点发布出来的 发布出来的数据称 ...