POJ2752 (Seek the Name, Seek the Fame,kmp)

传送门

Seek the Name, Seek the Fame

Time Limit: 2000MS   Memory Limit: 65536K
     

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

POJ Monthly--2006.01.22,Zeyuan Zhu

根据next函数的性质直接倒推就行了。。

 1 #include<set>
 2 #include<queue>
 3 #include<cstdio>
 4 #include<cstdlib>
 5 #include<cstring>
 6 #include<iostream>
 7 #include<algorithm>
 8 using namespace std;
 9 const int N = 400010;
10 #define For(i,n) for(int i=1;i<=n;i++)
11 #define Rep(i,l,r) for(int i=l;i<=r;i++)
12 #define Down(i,r,l) for(int i=r;i>=l;i--)
13
14 char s[N];
15 int next[N],n,ans[N];
16
17 void BuildNext(char s[]){
18     next[0]=next[1]=0;
19     For(i,n-1){
20         int j=next[i];
21         while(j&&s[i]!=s[j]) j=next[j];
22         if(s[i]==s[j])  next[i+1]=j+1;
23         else            next[i+1]=0;
24     }
25 }
26
27 int main(){
28     while(scanf("%s",&s)!=EOF){
29         n=strlen(s);BuildNext(s);
30         int p=next[n];ans[ans[0]=1,1]=n;
31         while(p){
32             ans[++ans[0]]=p;
33             p=next[p];
34         }
35         Down(i,ans[0],2) printf("%d ",ans[i]);printf("%d\n",ans[1]);
36     }
37     return 0;
38 }
时间: 2024-10-12 13:40:23

POJ2752 (Seek the Name, Seek the Fame,kmp)的相关文章

sdut 2416:Fruit Ninja II(第三届山东省省赛原题,数学题)

Fruit Ninja II Time Limit: 5000MS Memory limit: 65536K 题目描述 Have you ever played a popular game named "Fruit Ninja"?Fruit Ninja (known as Fruit Ninja HD on the iPad and Fruit Ninja THD for Nvidia Tegra 2 based Android devices) is a video game de

JSP内置对象---request对象(用户登录页面(返回值和数组:gerParameter,getParameterValues))

创建两个jsp页面:reg.jsp 和 request.jsp reg.jsp: <%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%> <%@ page import="java.text.*" %><% String path = request.getContextPath(

搞定单模式匹配(简单,KMP)

模式匹配是查找的一种,分为单模式匹配和多模式匹配.查找,就是在一个集合中查找一个或多个元素,查找一个元素就叫单模式匹配,查找多个元素就是多模式匹配,这里只探讨单模式匹配.虽然模式匹配看上去与数字的查找不一样,但是本质上任然是一种查找,比如在“aabaabaabaac”中查找“aabaac”,对计算机来说,处理的仍然是在集合{aabaab, abaaba, baabaa, aabaab, abaaba, baabaa, aabaac}中查找“aabaaac”,这是计算机的流处理特性决定的.所谓的简

在 eclipse 上安装 genymotion 插件不成功后的解决办法(谁弄到装不了,你懂的)

之前按照网上的教程去安装 genymotion 插件,发现安装不了,试了很多次也就成功了一次,但是安装成功的 eclipse 有问题,所以就删除了:悲剧就这么产生了,之后就一直都没有成功过.谁让下个插件都会这样,这里不解释...(此处省略10000个字),如图: genymotion 是一个很不错的软件,跟谷歌官方的模拟器最大的区别就是它的启动速度特别快,而且模拟器的交互也做得挺好的,所以这个软件是做 Android 开发的一个不可或缺的软件,下面来说一下具体安装过程吧.      第一步:点开

leetcode链表--13、median-of-two-sorted-arrays(两个排序数组的中位数,时间复杂度)

题目描述 There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). 题目解析:本题关键之处在于时间复杂度要求为O(log(m+n)),本题如果采用合并并排序两数组的方式,时间复杂度为O((m+n)*log(n+m))但是在牛客网上

React Native开发的通讯录应用(使用JavaScript开发原生iOS应用,vczero)

0.前言: 项目地址:https://github.com/vczero/React-Native-App 欢迎大家提issues讨论任何问题,包括“试衣间”.... 一.项目介绍 基于React-Native & Node通讯录App (1)主要完成的功能有: 基于文件系统的Node.js服务端; 通讯录功能(分类页 + 列表页 + 拨号邮箱邮件) 公告功能(列表页 + 详情页) 通讯录和内容管理功能 webview内嵌实例 效果如下图所示:       (2)安装启动程序 (1)首先进入ad

TControlStyle.csParentBackground的作用(附Delphi里的所有例子,待续)

Only applicable when Themes are enabled in applications on Windows XP. Causes the parent to draw its background into the control's background. This is useful for controls that need to show their parent's theme elements, such as aTPanel or TFrame that

ElementTree 解析xml(minidom解析xml大文件时,MemoryError)

在使用minido解析xml文件时,因为文件过大,结果报错MemoryError.查询后得知是因为minidom在解析时是将所有文件放到内存里的,很占用内存,所以要考虑换一种方法来处理xml文件. ElementTree相比minidom消耗内存更小,下面是ElementTree的一些简单用法 XML源文件中的部分内容: #导入ElementTree from xml.etree import ElementTree #读入并解析XML文件,读入的是树形结构 doc = ET.parse(XML

Cyclic Nacklace ---hdu3746(循环节,kmp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3746 给你一个字符串,让你在后面加尽量少的字符,使得这个字符串成为一个重复串. abca---添加bc,成为abcabc abcd---添加abcd,成为abcdabcd aa---无需添加 经典的求最小循环节. 首先给出结论:一个字符串的最小循环节为:len-next[len] #include<stdio.h> #include<iostream> #include<algo