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. git 和码云的上传文件代码操作

    Git与Github的连接与使用 一 安装git软件 1.git介绍 ''' git是一款免费.开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目.​ 分布式相比于集中式的最大区别在于开发 ...

  2. .NetCore模拟Postman的BasicAuth生成Authrization

    一.思路 BasicAuth 是一种简单权限,传输UserName=<userName>,Password=<password> 1.用:连接Username,Password ...

  3. MySQL的删除语句

    虽然现在数据库空间越来越大,但处理数据时候还是有要删除的时候,以下整理了一些最常用的删除语句. 分成两种 一个是删除指定数据,另一个删除所有数据. 一.删除指定数据 DELETE FROM 表名 WH ...

  4. 朴素贝叶斯算法——实现新闻分类(Sklearn实现)

    1.朴素贝叶斯实现新闻分类的步骤 (1)提供文本文件,即数据集下载 (2)准备数据 将数据集划分为训练集和测试集:使用jieba模块进行分词,词频统计,停用词过滤,文本特征提取,将文本数据向量化 停用 ...

  5. Installation of the latest version of netease-cloud-music on Fedora 30 linux platform

    Installation of the latest version of netease-cloud-music on Fedora 30 linux platform Abtract As we  ...

  6. Linux shell中自动完成登录

    在写shell脚本时,需要登录到不同的服务器上执行相关命令,在未建立信任之前如何批量操作. 1.ssh 首次登录服务器时会提示RSA key fingerprint输入yes/no,可以通过下面的方法 ...

  7. python字符串前面的u,还有r

    以u或U开头的字符串表示unicode字符串 如果你想要用非英语写文本,那么你需要有一个支持Unicode的编辑器.(了解一下unicode和ascll码还有utf-8) u'你好'        # ...

  8. pspice中参数的意义

    摘自:http://royroyyy.blog.163.com/blog/static/137650617201102610471196/ 有源器件在符号库中的名称(NAME)通常以关键字开头,后根长 ...

  9. PHP与CI学习笔记

    CodeIgniter框架学习 安装 下载好包后,解压复制 system 和 application 目录到网站目录下 配置 配置目录 打开index.php设置好 $system_path . $a ...

  10. 调用搜狐IP地址库,根据不同访问者的IP,显示访问地址

    <script src="http://pv.sohu.com/cityjson?ie=utf-8"></script>   <script type ...