http://poj.org/problem?id=2752
Seek the Name, Seek the Fame
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 27512   Accepted: 14244

Description

The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the little cat to give names to their newly-born babies. They seek the name, and at the same time seek the fame. In order to escape from such boring job, the innovative little cat works out an easy but fantastic algorithm:

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

The input contains a number of test cases. Each test case occupies a single line that contains the string S described above.

Restrictions: Only lowercase letters may appear in the input. 1 <= Length of S <= 400000.

Output

For
each test case, output a single line with integer numbers in increasing
order, denoting the possible length of the new baby's name.

Sample Input

ababcababababcabab
aaaaa

Sample Output

2 4 9 18
1 2 3 4 5

Source

 
题意:求字符串所有的相同前缀与后缀;
思路:我们知道next数组是求最长前缀与后缀,如果要求所有,我们就要向前递归next数组直到为0,即可找出所有长度的前缀与后缀
 
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <algorithm>
#include <iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include <stdio.h>
#include <string.h>
using namespace std;
char a[];
int next[];
int num[];
void getnext(char *a , int len , int *next)
{
next[] = - ;
int k = - , j = ;
while(j < len)
{
if(k == - || a[k] == a[j])
{
k++;
j++;
// if(a[k] != a[j])
next[j] = k;//next数组不能优化
// else
// next[j] = next[k];*/
}
else
k = next[k];
}
} int main()
{
while(~scanf("%s" , a))
{
memset(next , , sizeof(next));
memset(num , , sizeof(num));
int len = strlen(a);
getnext(a , len , next);
int l = ;
num[l++] = len ;
while(next[len] > )//向前递归next数组
{
num[l++] = next[len];
len = next[len];
}
for(int i = l - ; i > ; i--)
{
printf("%d " , num[i]);
}
printf("%d\n" , num[]);
} return ;
}

kmp(所有长度的前缀与后缀)的更多相关文章

  1. kmp(最长前缀与后缀)

    http://acm.hdu.edu.cn/showproblem.php?pid=1358 Period Problem Description For each prefix of a given ...

  2. POJ 2752 Seek the Name, Seek the Fame (KMP的next函数,求前缀和后缀的匹配长度)

    给一个字符串S,求出所有前缀,使得这个前缀也正好是S的后缀.升序输出所有情况前缀的长度.KMP中的next[i]的意义就是:前面长度为i的子串的前缀和后缀的最大匹配长度.明白了next[i],那么这道 ...

  3. POJ 2752 (KMP 所有可能长度的前缀后缀) Seek the Name, Seek the Fame

    题意: 求一个字符串的相同前缀后缀的所有可能的长度,这里该字符串其本身也算自己的前缀和后缀. 分析: 我们知道next数组的性质是,该字符之前的字符串的最大相同前缀后缀. 既然知道了最大的,即next ...

  4. POJ 2752 Seek the Name,Seek the Fame(KMP,前缀与后缀相等)

    Seek the Name,Seek the Fame 过了个年,缓了这么多天终于开始刷题了,好颓废~(-.-)~ 我发现在家真的很难去学习,因为你还要陪父母,干活,做家务等等 但是还是不能浪费时间啊 ...

  5. 139. 回文子串的最大长度(回文树/二分,前缀,后缀和,Hash)

    题目链接 : https://www.acwing.com/problem/content/141/ #include <bits/stdc++.h> using namespace st ...

  6. POJ 2752 Seek the Name, Seek the Fame(求所有既是前缀又是后缀的子串长度)

    题目链接:http://poj.org/problem?id=2752 题意:给你一个字符串,求出所有前缀后缀(既是前缀又是后缀的子串)的长度 思路:首先整个字符串肯定既是前缀又是后缀,为最大的前缀后 ...

  7. AcWing:139. 回文子串的最大长度(字符串Hash + 前缀和 + 后缀和 + 二分)

    如果一个字符串正着读和倒着读是一样的,则称它是回文的. 给定一个长度为N的字符串S,求他的最长回文子串的长度是多少. 输入格式 输入将包含最多30个测试用例,每个测试用例占一行,以最多1000000个 ...

  8. 【Todo】字符串相关的各种算法,以及用到的各种数据结构,包括前缀树后缀树等各种树

    另开一文分析字符串相关的各种算法,以及用到的各种数据结构,包括前缀树后缀树等各种树. 先来一个汇总, 算法: 本文中提到的字符串匹配算法有:KMP, BM, Horspool, Sunday, BF, ...

  9. 【分治-前缀积后缀积】JS Window @2018acm徐州邀请赛G

    问题 G: JS Window 时间限制: 2 Sec  内存限制: 512 MB 题目描述 JSZKC has an array A of N integers. More over, he has ...

随机推荐

  1. vue中搜索关键词,使文本标红

    UserHead.vue中搜索框: <!-- 搜索 --> <el-col :span="6" :offset="8" class=" ...

  2. 认知redis

    一.redis是什么? 1.基于key-value的内存No sql 数据库(非关系型数据库) 2.读写性能非常好 二.redisd的数据类型有哪些?特点分别是什么? 1)string 一个键对一个值 ...

  3. springboot操作rabbitmq

    ////DirectExchange directExchange = new DirectExchange("test.direct");////amqpAdmin.declar ...

  4. python——解释型语言

    编程语言分三大类   :  低级语言  .  汇编语言 .   高级语言. 现代计算机都是基于  图灵机模型 制造的.  因此计算机的内部只能接受二进制数据.而用二进制代码  0  1  描述的指令叫 ...

  5. jquery常用用法

    1.$("prve + next" )                          选择紧接着在perve后的next元素2.$("#div1 > *&quo ...

  6. Jmeter的JDBC请求执行多条SQL语句

    注:有mysqlconnector/j 3.1.1以上版本才支持执行多条sql语句 1.     下载jdbc驱动为了连接Mysql数据库,还需要有个jdbc驱动:mysql-connector-ja ...

  7. OC + RAC (三) 信号中的信号

    -(void)_test3{ RACSubject *signalofsignal = [RACSubject subject]; //信号中的信号(也就是发送的数据是信号) RACSubject * ...

  8. stack2链栈

    #include<iostream> using namespace std; template <class Object> class Stack{ private: st ...

  9. <i>和<em>、<b>和<strong>标签的区别

    在HTML4.01中:< b > < i > 是视觉要素(presentationl elements),分别表示无意义的加粗,无意义的斜体,仅仅表示样式上是粗体或斜体,而没有 ...

  10. 170819-关于EL表达式的知识点

    1 .EL表达式 [1] 简介 > JSP表达式 <%= %> 用于向页面中输出一个对象. > 到JSP2.0时,在我们的页面中不允许出现 JSP表达式和 脚本片段. > ...