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 ...
随机推荐
- 需要network lightweight filter disk 上的文件netft.sys
小米wifi在win10下安装不成功,需要network lightweight filter disk 上的文件ntflt.sys 默认路径有问题,改成下面的路径好了! 选择下面第一个路径安装成功了 ...
- tomcat使用manager管理app时需要身份验证问题
我们可以通过图形用户界面来管理tomcat,启动tomcat,在地址栏中输入: Java代码 http://localhost:8080 就可以看见tomcat的欢迎页面,点击左边的tomcat ma ...
- Win 2008 R2安装SQL Server 2008“性能计数器注册表配置单元一致性”失败的解决办法
Win 2008 R2安装SQL Server 2008“性能计数器注册表配置单元一致性”失败的解决办法(2011-02-23 19:37:32) 转载▼ 今天在惠普服务器上安装数据库2008时, ...
- MSSQL 日期查询 包含NULL值
方一: 以下做法保证数据里面没有NULL值 '') '') '') '') exec sp_executesql N'select ide_code as ''系统编号'',name as ''申请专 ...
- CSS技巧: CSS隐藏文字的方法(CSS text-indent: -9999px;)
建站过过程中朋友喜欢把网站名称用H1表示,但从美观考虑,要用logo图片来代替h1,这时需要隐藏h1内的这段文字,但又不能对搜索引擎不友好,否则就失去了定义h1标签的意义. 在CSS中如何以图代字,找 ...
- 提示crontab command not found的解决方法
操作步骤 1. 确认crontab是否安装: 执行 crontab 命令如果报 command not found,就表明没有安装 2. 安装 crontab 执行 yum insta ...
- [转] php foreach用法和实例
PHP 4 引入了 foreach 结构,和 Perl 以及其他语言很像.这只是一种遍历数组简便方法.foreach 仅能用于数组,当试图将其用于其它数据类型或者一个未初始化的变量时会产生错误.有两种 ...
- Luogu 1314 [NOIP2011] 聪明的质监员
二分答案 + 前缀和. 题面中式子的意思是每一个区间$[l, r]$的贡献是这个区间内$w_i \geq W$的个数乘以这些$i$的$v_i$和. 很快发现了答案具有单调性,可以做两遍二分,分别看看小 ...
- Cadence如何自定义快捷键
第一步:将pcbenv复制到SPB_Data下,并覆盖原来的文件 第二步:打开pcbenv文件,将script和views文件夹以及env复制到cadence安装目录\SPB_16.3\share\p ...
- android加载字体内存泄漏的处理方法
在开发android app的开发过程中,会使用到外部的一些字体.外部字体在加载的时候,容易造成内存泄漏. 比如: Typeface tf=Typeface.createFromAsset(getAs ...