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 ...
随机推荐
- Celery-4.1 用户指南: Periodic Tasks (定时任务)
简介 celery beat 是一个调度器:它以常规的时间间隔开启任务,任务将会在集群中的可用节点上运行. 默认情况下,入口项是从 beat_schedule 设置中获取,但是自定义的存储也可以使用, ...
- springmvc下的省市县三级联动
转自:https://blog.csdn.net/rentian1/article/details/77662635
- Struts2+Hibernate+Spring 整合示例
转自:https://blog.csdn.net/tkd03072010/article/details/7468769 Struts2+Hibernate+Spring 整合示例 Spring整合S ...
- pl/sql使用部分整理
在工作中使用pl/sql工具,总结一下常用操作,以便以后复习,也希望帮助朋友们熟悉pl/sql操作! 1.pl/sql记住登录密码PL/SQL Developer->tools->Pref ...
- activity的四种加载模式介绍
四种加载模式的介绍: a) Standard : 系统默认模式,一次跳转即会生成一个新的实例: b) SingleTop : 和 standard 类似,唯一的区别就是当跳转的对象是位于栈顶 ...
- php学习笔记-for循环
for(init;condition;statement) { func(); } for循环的执行逻辑是先执行一次init语句,然后判断condition是否为true,是则执行func(),再执行 ...
- IB使用
A:给控件添加方法或变量. 1.窗口上拖个控件 NSButton 2..点右上那张狗脸(Editor)对上的. 3.右键控件.拖到头文件中. 4 .选择加响应方法或变量.
- hdu6331 Problem M. Walking Plan
传送门 题目大意 给你一个n点m条边的有向图,q次询问,给定s,t,k,求由s到t至少经过k条边的最短路. 分析 我们设dp[i][j][k]为从i到j至少经过k条边的最短路,sp[i][j]意为从i ...
- 8.View类
Basic Concepts 在Modle/View 结构中,View从Model中提取数据,并显示给用户.View显示数据的方式不一定与Model中数据排列方式相同,也可能与底层数据结构完 ...
- debug---null Pointer Exception--一步步查找(1)
找到对应的226行代码: 通过debug打断点,然后选中需要查看的代码,右击,选择Evaluate Expresstion,选择确认,就可以弹出具体的值,发现真的为null. 通过simon帮忙分析, ...