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,以为暴力会超时. ...
随机推荐
- Unique Binary Search Trees II 解答
Question Given n, generate all structurally unique BST's (binary search trees) that store values 1.. ...
- 剑指offer-面试题5.从尾到头打印链表
题目:输入一个链表的头结点,从尾到头反过来打印出每个结点的值. 刚看到这道题的小伙伴可能就会想,这还不简单,将链表反转输出. 但是这种情况破坏了链表的结构. 如果面试官要求不破坏链表结构呢,这时候我们 ...
- kindeditor在sae上传文件修改,适合php
kindeditor在sae上传文件修改,适合php 当前位置: 首页 > 论坛 > 经验共享 用户登录 新用户注册 主题: kindeditor在sae上传文件修改,适合ph ...
- NicEdit - WYSIWYG Content Editor, Inline Rich Text Application
NicEdit - WYSIWYG Content Editor, Inline Rich Text Application By calling the nicEditors.allTextarea ...
- 上传form表单
<form name="theForm" method="post" action="index.php?m=back&c=Goods& ...
- CSS3初步
一.CSS与CSS3的区别 非常简单,CSS代表"Casading Style Sheets",就是样式表,是一种替代并为网站添加样式的标记性语言.现在所使用的CSS基本是在199 ...
- RMAN传输表空间迁移数据
实验环境: 源数据库:oracle 10g(Release 10.2.0.1.0) 目标数据库:oracle 10g(Release 10.2.0.1.0) 待传输的表空间:TEST 1.在tes ...
- The Building Blocks- Components of EA Part 2- Process, People, Network and Time
1. Zachman Framework Information (Data) - Answer the Question 'What?' Contextual: List of Things imp ...
- Maven 工程下 Spring MVC 站点配置 (三) C3P0连接池与@Autowired的应用
Maven 工程下 Spring MVC 站点配置 (一) Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作 前两篇文章主要是对站点和数据库操作配置进行了演示,如果单 ...
- Sqlserver系列(三) 小技巧
(1)取别名 as 省略 as XX= select 'a' as name select 'a' name select name='a' 示例