kuangbin专题十六 KMP&&扩展KMP HDU2594 Simpsons’ Hidden Talents
Marge: Yeah, what is it?
Homer: Take me for example. I want to find out if I have a talent in politics, OK?
Marge: OK.
Homer: So I take some politician’s name, say Clinton, and try to find the length of the longest prefix
in Clinton’s name that is a suffix in my name. That’s how close I am to being a politician like Clinton
Marge: Why on earth choose the longest prefix that is a suffix???
Homer: Well, our talents are deeply hidden within ourselves, Marge.
Marge: So how close are you?
Homer: 0!
Marge: I’m not surprised.
Homer: But you know, you must have some real math talent hidden deep in you.
Marge: How come?
Homer: Riemann and Marjorie gives 3!!!
Marge: Who the heck is Riemann?
Homer: Never mind.
Write a program that, when given strings s1 and s2, finds the longest prefix of s1 that is a suffix of s2.
the second line contains s2. You may assume all letters are in
lowercase.OutputOutput consists of a single line that contains the longest
string that is a prefix of s1 and a suffix of s2, followed by the length
of that prefix. If the longest such string is the empty string, then
the output should be 0.
The lengths of s1 and s2 will be at most 50000.Sample Input
clinton
homer
riemann
marjorie
Sample Output
0
rie 3 思路就是直接拼接,然后处理即可。刚开始什么都没考虑,直接交了。后来一点点发现了问题。8Y 列举几个特殊样例即可 ab
abab abab
ab abc
ab ab
cabc len1为s1的长度,len2为s2的长度,len为总长 可以发现如果Next[len]<==0 肯定为0 否则可能为0 就是循环节的长度大于s1 或者满足了不大于s1但是最终长度大于s2 还有可能就是一个很短的循环节循环了好多次。
#include<stdio.h>
#include<string.h>
char s[],s1[];
int len,lentemp1,lentemp2,Next[]; void prekmp() {
int i,j;
j=Next[]=-;
i=;
while(i<len) {
while(j!=-&&s[i]!=s[j]) j=Next[j];
Next[++i]=++j;
}
} int main() {
//freopen("in","r",stdin);
while(~scanf("%s",s)) {
lentemp1=strlen(s);
scanf("%s",s1);
lentemp2=strlen(s1);
strcat(s,s1);
len=strlen(s);
prekmp();
if(Next[len]>) {
int i=Next[len];
while(i>lentemp1||i>lentemp2) {//小于s1&&小于s2 找满足条件的最大i
i=Next[i];
}
if(i<=) printf("0\n");
else {
for(int j=;j<i;j++) printf("%c",s[j]);
printf(" %d\n",i);
}
} else printf("0\n");
}
}
kuangbin专题十六 KMP&&扩展KMP HDU2594 Simpsons’ Hidden Talents的更多相关文章
- HDU2594 Simpsons’ Hidden Talents —— KMP next数组
题目链接:https://vjudge.net/problem/HDU-2594 Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Oth ...
- hdu2594 Simpsons’ Hidden Talents kmp
Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- hdu2594 Simpsons' Hidden Talents【next数组应用】
Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
- kuangbin专题十六 KMP&&扩展KMP HDU2609 How many (最小字符串表示法)
Give you n ( n < 10000) necklaces ,the length of necklace will not large than 100,tell me How man ...
- hdu2594 Simpsons’ Hidden Talents LCS--扩展KMP
Homer: Marge, I just figured out a way to discover some of the talents we weren’t aware we had.Marge ...
- kuangbin专题十六 KMP&&扩展KMP HDU2328 Corporate Identity
Beside other services, ACM helps companies to clearly state their “corporate identity”, which includ ...
- kuangbin专题十六 KMP&&扩展KMP HDU1238 Substrings
You are given a number of case-sensitive strings of alphabetic characters, find the largest string X ...
- kuangbin专题十六 KMP&&扩展KMP HDU3336 Count the string
It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...
- kuangbin专题十六 KMP&&扩展KMP POJ3080 Blue Jeans
The Genographic Project is a research partnership between IBM and The National Geographic Society th ...
随机推荐
- python ConfigParser 读取配置文件
- Vue2.0+Webpack+Element+Axios+vueRouter技术栈使用过程总结
搭建项目架构 目采用Webpack+Vue-router的架构方式,开始安装(一切操作都在windows系统上完成) 1.按Win+R,然后在文本框中输入cmd,回车打开命令行,输入vue-cli安装 ...
- Oracle Management Packs
http://kerryosborne.oracle-guy.com/2008/10/oracle-management-packs/ There has been quite a bit of co ...
- java.lang.ClassCastException: org.apache.catalina.connector.RequestFacade cannot be cast to org.springframework.web.multipart.MultipartHttpServletRequest
转自:https://blog.csdn.net/iteye_17476/article/details/82651580 java.lang.ClassCastException: org.apac ...
- C语言枚举类型enum-(转)-温故而知新
在实际编程中,有些数据的取值往往是有限的,只能是非常少量的整数,并且最好为每个值都取一个名字,以方便在后续代码中使用,比如一个星期只有七天,一年只有十二个月,一个班每周有六门课程等. 以每周七天为例, ...
- cygwin选择安装包选项搭建NDK开发环境/配置cygwin的root权限
9.Search是可以输入你要下载的包的名称,能够快速筛选出你要下载的包.那四个单选按钮是选择下边树的样式,默认就行,不用动.View默认是Category,建议改成full显示全部包再查,省的一些包 ...
- C程序设计语言(K&R) 笔记2
(1) #include <stdio.h> main(){ int c; while((c = getchar()) != EOF){ putchar(c); } } 注意,因为 != ...
- sublime3 There are no packages available for installation
我的是网上下载的绿色版 1.找到sublime\Data\Packages 删除Packages control相关的文件夹和文件 下载https://packagecontrol.io/Packa ...
- LINUX中错误 SELinux is disabled
解决: setenforce: SELinux is disabled 那么说明selinux已经被彻底的关闭了 如果需要重新开启selinux,请按下面步骤: vi /etc/selinux/con ...
- Luogu 3625 [APIO2009]采油区域
想了很久的dp,看了一眼题解之后感觉自己被安排了. 发现从一个矩形中选择三个不相交的正方形一共只有六种取法. 那么我们可以处理出四个值: $f_{i, j}$分别表示以$(i, j)$为右下角,左下角 ...