Seek the Name, Seek the Fame(Kmp)
Seek the Name, Seek the Fame
Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 12 Accepted Submission(s) : 7
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:)
Restrictions: Only lowercase letters may appear in the input. 1 <= Length of S <= 400000.

所以对于这道题,求出len处的next值,并递归的向下求出所有的next值,得到的就是答案。
所以每次只需要找此段匹配的长度就好
/***************************************************************************/
代码:用了个递归,浪了一下,竟然还没PE;kmp的原理,从len开始直接相当于了位置坐标加一,慢慢体味。。。
#include<stdio.h>
#include<string.h>
const int MAXN=;
char s[MAXN];
int p[MAXN],len;
void getp(){
int i=,j=-;
p[]=-;
while(i<len){
if(j==-||s[i]==s[j]){
i++;j++;
p[i]=j;
}
else j=p[j];
}
}
void print(int x){
if(x<=)return;
print(p[x]);
printf("%d ",x);
}
int main(){int i,j;
while(~scanf("%s",s)){
len=strlen(s);
getp();
/*for(i=0;i<=len;i++)printf("%d ",p[i]);puts("");
j=len;
while(j>=0){
for(i=p[j];i<j;i++)printf("%c",s[i]);puts("");
j=p[j];
}*/
print(len);puts("");
}
return ;
}
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long LL;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define P_ printf(" ")
const int INF=0x3f3f3f3f;
const int MAXN=400010;
int p[MAXN]; void getp(char* s){
int len=strlen(s);
int i=0,j=-1;
p[0]=-1;
while(i<len){
if(j==-1||s[i]==s[j]){
i++;j++;
p[i]=j;
}
else j=p[j];
}
}
/*
void kmp(char *s,char* m,int& ans){
int len=strlen(m);
getp(s);
int len2=strlen(s);
int j=0,i=0;
while(i<len){
if(j==-1||s[j]==m[i]){
i++;j++;
if(j==len2){
ans++;
}
}
else j=p[j];
}
}
*/
int main(){
char s[MAXN];
int a[MAXN];
int tp;
while(~scanf("%s",s)){
getp(s);
tp=0;
int len=strlen(s);
while(len>0){
a[tp++]=len;
len=p[len];
}
sort(a,a+tp);
// int k=unique(a,a+tp)-a;
for(int i=0;i<tp;i++){
if(i)P_;
PI(a[i]);
}puts("");
}
return 0;
}
Seek the Name, Seek the Fame(Kmp)的更多相关文章
- poj-------------(2752)Seek the Name, Seek the Fame(kmp)
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11831 Ac ...
- POJ 2752 - Seek the Name, Seek the Fame (KMP)
题意:给一个字符串s,问s的某个前缀与后缀相同的情况时,长度是多少. 此题使用KMP的next数组解决. next数组中,j=next[i],next[i]表示S[0...i-1]的某个后缀(字符串S ...
- poj2406 Power Strings(kmp)
poj2406 Power Strings(kmp) 给出一个字符串,问这个字符串是一个字符串重复几次.要求最大化重复次数. 若当前字符串为S,用kmp匹配'\0'+S和S即可. #include & ...
- (KMP)Seek the Name, Seek the Fame -- poj --2752
http://poj.org/problem?id=2752 Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536 ...
- poj 2752 Seek the Name, Seek the Fame (KMP纯模版)
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13840 Ac ...
- POJ 2751:Seek the Name, Seek the Fame(Hash)
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24077 Ac ...
- POJ 2406 Power Strings(KMP)
Description Given two strings a and b we define a*b to be their concatenation. For example, if a = & ...
- LightOJ 1258 Making Huge Palindromes(KMP)
题意 给定一个字符串 \(S\) ,一次操作可以在这个字符串的右边增加任意一个字符.求操作之后的最短字符串,满足操作结束后的字符串是回文. \(1 \leq |S| \leq 10^6\) 思路 \( ...
- codeM编程大赛E题 (暴力+字符串匹配(kmp))
题目大意:S(n,k)用k(2-16)进制表示1-n的数字所组成的字符串,例如S(16,16)=123456789ABCDEF10: 解题思路: n最大50000,k最大100000,以为暴力会超时. ...
随机推荐
- FreeMarker的模板文件语法
FreeMarker的模板文件并不比HTML页面复杂多少,FreeMarker模板文件主要由如下4个部分组成: 1,文本:直接输出的部分 2,注释:<#-- ... -->格式部分,不会输 ...
- DICOM:C-GET与C-MOVE对照剖析
背景: 之前专栏中介绍最多的两款PACS各自是基于dcmtk的dcmqrscp以及Orthanc.和基于fo-dicom的DicomService(自己开发的).该类应用场景都是针对于局域网,因此在使 ...
- Spring源代码由浅入深系列五 GetBean
获取bean的过程如上图所看到的.下一章将继续图示解说createBean的过程. blog宗旨:用图说话 附:文件夹 Spring源代码由浅入深系列四 创建BeanFactory Spring源代码 ...
- CSS入门教程——定位(positon)
CSS入门教程——定位(positon) CSS定位在网页布局中是起着决定性作用. 定位 CSS的定位功能是很强大的,利用它你可以做出各种各样的网页布局.本节就介绍一些CSS常用的定位语句. 1. ...
- Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作
详细的Spring MVC框架搭配在这个连接中: Maven 工程下 Spring MVC 站点配置 (一) Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作 这篇主 ...
- 虚拟化之KVM virsh常用命令篇
1,查看运行的虚拟机 virsh list 2,查看所有的虚拟机(关闭和运行的虚拟机) virsh list --all 3,连接虚拟机 virsh console +域名(虚拟机的名称) 4,退出虚 ...
- 一个SQL 建表格式
CREATE TABLE [dbo].[SysSample]([Id] [varchar](50) NOT NULL,[Name] [varchar](50) NULL,[Age] [int] NUL ...
- JQ图片跟着鼠标走
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- visual studio 2015 修改类class 文件模板
第一步:找到模板文件 路径:C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\ItemTemplates\CSharp\C ...
- IOS创建单例的两种方法
1.0 苹果官方写法: static AccountManager *DefaultManager = nil; + (AccountManager *)defaultManager { if ( ...