原文:
Homer: Marge, I just figured out a way to discover some of the talents we weren’t aware we had.
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.
Input
Input consists of two lines. The first line contains s1 and the second line contains s2. You may assume all letters are in lowercase.
Output
Output 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
题意描述:
找出第一个子串的前缀和第二个子串后缀相同的部分,如果有就输出没有的话就输出0;
解题思路:
用next数组存储第一个子串的前缀和后缀的信息,然后比较两个子串如果相等就往后继续找记录j的数值 ,如果不相等就让第一个子串从头开始找第二个子串中与他相同的子串
解题技巧:
如果在找的时候要是不相等就让第二个子串的当前位置不变,让第一个子串回到当前next数组中next[j]的值的位置,因为要找的 是第二个子串的后缀在后面要么会出现与第一个子串前缀相同的后缀,要么就不会出现;
AC代码:

#include<stdio.h>
#include<string.h>
char s[100005],p[50005];
int next[100005];
int n,m;
void Get_next()
{int i=1,j=0;next[0]=-1;while(i<n){if(j==-1||s[i]==s[j]){i++;j++;next[i]=j;}else{j=next[j];}}
}
void kmp()
{int i=0,j=0;while(i<m){if(j==-1||s[j]==p[i]){i++;j++;}else{j=next[j];}}if(j==0)printf("0\n");else{for(i=0; i<j; i++)printf("%c", s[i]);printf(" %d", j);printf("\n");}
}
int main()
{while(scanf("%s %s", s,p)!=EOF){n=strlen(s);m=strlen(p);Get_next();kmp();}return 0;
}  中文题目:
荷马:玛吉,我刚刚想出了一个方法来发现一些我们不知道自己有什么才能。
玛吉:是的,是什么?
荷马:以我为例。我想知道我是否有政治天赋,好吗?
玛吉:好的。
荷马:所以我取了一些政治家的名字,比如克林顿,然后试着找出最长前缀的长度。
在克林顿的名字里,那是我名字的后缀。这就是我和克林顿这样的政治家的关系
玛吉:为什么选择最长的前缀作为后缀?是吗?
荷马:嗯,我们的才能深深地隐藏在我们自己的内心,玛吉。
玛吉:那你离这儿有多远?
荷马:0!
玛吉:我一点也不惊讶。
荷马:但你知道,你内心深处一定隐藏着一些真正的数学天赋。
玛吉:怎么了?
荷马:里曼和马乔里给了3个!!!
玛吉:谁是黎曼?
荷马:没关系。
编写一个程序,当给定字符串s1和s2时,该程序将查找s1的最长前缀,即s2的后缀。
输入
输入由两行组成。第一行包含s1,第二行包含s2。您可以假设所有字母都是小写的。
输出
输出由一行组成,该行包含前缀为s1的最长字符串和后缀为s2,后跟该前缀的长度。如果最长的这样的字符串是空字符串,那么输出应该是0。
S1和S2的长度最多为50000。
Sample Input
clinton
homer
riemann
marjorie
Sample Output
0
rie 3

kmp---Simpsons’ Hidden Talents(初学者能看懂的算法)相关推荐

  1. HDU 2594 Simpsons’ Hidden Talents (字符串-KMP)

    Simpsons' Hidden Talents Problem Description Homer: Marge, I just figured out a way to discover some ...

  2. kmp总结(相关例题1. Simpsons’ Hidden Talents 2.Oulipo)

    kmp相关及相关例题 文章目录 kmp相关及相关例题 一.kmp算法最常规使用方法 二.相关例题 1. Simpsons' Hidden Talents 2.Oulipo 一.kmp算法最常规使用方法 ...

  3. Simpsons’ Hidden Talents(KMP ,两个串的前后缀匹配)

    Simpsons' Hidden Talents 题目 给两个串,求S1的前缀和S2的后缀的最大匹配 思路 拼接两个串,处理出nxt数组,nxt[k] 即为所求,因为它们的最大匹配不能超过原串的长度, ...

  4. Simpsons’ Hidden Talents(辛普森一家的隐藏天赋 )(kmp经典模板题) HDU - 2594

    题目:Simpsons' Hidden Talents(辛普森一家的隐藏天赋 ) 中文大意 Homer: Marge, I just figured out a way to discover som ...

  5. HDU2594(Simpsons’ Hidden Talents)

    Simpsons' Hidden Talents Problem Description Homer: Marge, I just figured out a way to discover some ...

  6. B - Simpsons’ Hidden Talents

    B - Simpsons' Hidden Talents Homer: Marge, I just figured out a way to discover some of the talents ...

  7. HDU-2594 Simpsons’ Hidden Talents

    HDU-2594 Simpsons' Hidden Talents 题目链接:HDU-2594 题目大意:给定两个字符串 问第一个字符串前缀与第二个字符串的后缀的最大的重复部分有多长 不为0的话将他们 ...

  8. 单片机编程好学吗?单片机初学者怎样看懂代码?

    单片机在很多人看来好像门槛很高,在某些人看来很简单. 所以,单片机编程好不好学,这取决于谁去学,有没有基础,有没有兴趣. 我自己是通过自学学会的,我个人认为相对java那些纯软件,单片机比较好学. 单 ...

  9. Simpsons’ Hidden Talents (HDU-2594)

    Simpsons' Hidden Talents (HDU-2594) Homer: Marge, I just figured out a way to discover some of the t ...

最新文章

  1. AI一分钟 | 小米智能音箱mini版曝光,或售199元;特朗普被指利用AI竞选成功
  2. HTML DOM教程 22-HTML DOM Form 对象
  3. 【C语言进阶深度学习记录】三十三 C语言中动态内存分配
  4. 启动django项目 默认启动与指定端口启动
  5. svn 合并问题 MERGE of '/svn/web': 200 OK (http://xx.xx.xx.xx)
  6. linux socket 加锁,Linux使用openssl对socket加密【1】
  7. 如何利用jq来实现复选框的全选,反选!
  8. 每次打印只能打打印一页
  9. webpack4入门笔记——loader
  10. Unity3D常用API
  11. PS颜色校正(Photoshop颜色显示偏色)
  12. 本人新书-Redis开发与运维-目录
  13. golang GC机制
  14. 用python学概率与统计(第十二章)拟合度检验和独立性检验
  15. shell中用grep查找并且不输出_grep无法查找shell传过来的变量?先注意一下文本格式吧!...
  16. cordic ip核 vivado_Xilinx Vivado CORDIC IP核求解atan 反正切
  17. nand2tetris 第二章 布尔运算
  18. UDIMM和RDIMM内存条区别
  19. 如何是音乐再ios展台
  20. 兄弟连IT教育-九年的发展!

热门文章

  1. pojo层、dao层、service层、controller层的作用
  2. ABPA 对文件的存取
  3. 前端H5实现调用麦克风,录音功能
  4. simulink笔记——DSSS直接序列扩频
  5. 如何确保采购过程中的产品质量
  6. 0240 计算机维修技术,0240.2016《计算机维修技术》西南大学网上作业题和答案.doc...
  7. Peluso麦克风:P-414麦克风评测
  8. uni-app项目之电影预告
  9. Tokyo Cabinet及Tokyo Tyrant tcb tch比较分析
  10. CPU综合设计实验报告