辛普森一家的隐藏天赋 HDU - 2594

目录

辛普森一家的隐藏天赋 HDU - 2594

题意描述:当给定字符串s1和s2时,找到s1中最长的前缀,即s2的后缀。如果有,输出相同的字符串即字符串长度。

解题思路:本题利用next数组和kmp字符串匹配,因为会一直匹配直到s2全部匹配结束,返回kmp中s1的前缀与s2后缀对应的相同字符串的长,如果没有相匹配的,会返回0;所以根据题,只需要判断一下返回的值是否为0再输出相应就结果即可。

注意定义next数组的时候如果用输入输出流的话,就不要用“next”定义了,会显示没有明确的符号。

AC代码


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.

荷马:玛吉,我只是想出了一种方法来发现我们不知道的一些天赋。
玛吉:是啊,怎么了?
荷马:以我为例。我想知道我是否有政治才能,好吗?
玛吉:好的。
荷马:所以我取一些政治家的名字,比如克林顿,并试图找出
克林顿名字中最长前缀的长度, 即我名字中的后缀。这就是我离成为像克林顿
玛吉这样的政治家有多接近 :为什么选择最长的前缀作为后缀???
荷马:嗯,我们的才能深深地隐藏在我们自己的内心深处,玛吉。
玛吉:那你离得有多近?
荷马: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

题意描述:当给定字符串s1和s2时,找到s1中最长的前缀,即s2的后缀。如果有,输出相同的字符串即字符串长度。

解题思路:本题利用next数组和kmp字符串匹配,因为会一直匹配直到s2全部匹配结束,返回kmp中s1的前缀与s2后缀对应的相同字符串的长,如果没有相匹配的,会返回0;所以根据题,只需要判断一下返回的值是否为0再输出相应就结果即可。

注意定义next数组的时候如果用输入输出流的话,就不要用“next”定义了,会显示没有明确的符号。

AC代码:

#include<stdio.h>
#include<iostream>
#include<string.h>
int ne[50050];
char f[50050],s[50050];
using namespace std;
int a,b;
void getnext()
{int j=0,k=-1;ne[0]=-1;while(j<b){if(k==-1||s[j]==s[k])//s[k]前缀,s[j]后缀 {j++;k++;ne[j]=k;}else{k=ne[k];}  }
}int kmp()
{//int sum=0;int i=0,j=0;getnext();while(i<a){if(j==-1||f[i]==s[j]){i++;j++;}elsej=ne[j];}return j;
}int main()
{while(cin>>s)//因为是多实例,这里用scanf输入的时候使用~或!=EOF!!!不然会时间超限{cin>>f;memset(ne,0,sizeof(ne));a=strlen(f);b=strlen(s);int n=kmp();if(n!=0){for(int i=0;i<n;i++)printf("%c",s[i]);//    putchar(s[i]);cout<<" "<<n<<endl;}elsecout<<"0"<<endl;}return 0;
}

Simpsons’ Hidden Talents辛普森一家的隐藏天赋(next数组和kmp字符串匹配)相关推荐

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

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

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

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

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

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

  4. HDU2594(Simpsons’ Hidden Talents)

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

  5. B - Simpsons’ Hidden Talents

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

  6. HDU-2594 Simpsons’ Hidden Talents

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

  7. Simpsons’ Hidden Talents (HDU-2594)

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

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

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

  9. HDU2594——Simpsons’ Hidden Talents

    Problem Description Homer: Marge, I just figured out a way to discover some of the talents we weren' ...

最新文章

  1. CPU值满resmgr:cpu quantum造成的Oracle等待事件解决办法
  2. 双目图像重叠的视差计算_双目视觉(stereo vision)
  3. c51汇编语言随机数函数,汇编语言随机数发生器
  4. python windows安装readline
  5. word自带公式编辑_怎样在word2013中快速插入数学公式
  6. Kirill And The Game CodeForces - 842A
  7. 第三方软件源_手机上的天气软件哪个更准确?
  8. Apprentissage du français partie 1
  9. Python爬虫实践: 获取百度贴吧内容
  10. C/C++——new一个二维数组
  11. 视觉SLAM十四讲学习笔记-第三讲-相似、仿射、射影变换和eigen程序、可视化演示
  12. python关键词对联_keras基于CNN和序列标注的对联机器人
  13. 1078. 字符串压缩与解压 (20)-PAT乙级真题
  14. Java学习之JDBC(1)
  15. myeclipse导入项目
  16. 用python画圣诞树的代码
  17. Kotlin实战练习——自定义圆形图片三种实现方式
  18. 微型计算机显示器的标准接口,显示器原理及接口显示器BIOS编程I(原理部分)
  19. Windows10如何设置定时开机
  20. # 设置防火墙白名单

热门文章

  1. 选择了考研,你后悔过吗?
  2. PCB原理图绘制(7)——PCB的设置与布线
  3. Android 自定义scheme及多端唤起使用方法
  4. 听肖邦Chopin: Waltz No.19 in A minor, Op.Posth的美好
  5. 一只一元甜筒,一年卖了1200万,宜家的销售阴谋!
  6. 罗斯蒙特248HANAU2NS温度变送器
  7. 【论文笔记】Are We Ready for Vision-Centric Driving Streaming Perception? The ASAP Benchmark
  8. GridView 72般绝技 转自清清月儿收藏
  9. 阿里程序员写了一个新手都写不出的低级bug,被骂惨了。
  10. hadoop开启后用http访问出错