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

主要考察next数组的理解,具体没什么难点,主要是在两个字符串连接时候需要加一个其他字符,否则next计算时有可能会出现最长公共前后缀合并起来刚好变长的情况。

#include <iostream>
#include <cstdio>
#include <string.h>
using namespace std;
int Next[100000];
void getnext(int next[], const string str)
{int m = str.length();int j = 0, k = -1;next[0] = -1;while (j < m){if (k == -1 || str[j] == str[k]){j++;k++;next[j] = k;}elsek = next[k];}
}
int main()
{string s1;string s2;memset(Next, 0, sizeof(Next));while (cin >> s1){cin >> s2;s1 = s1 + "#" + s2;getnext(Next, s1);if (Next[s1.length()] == 0)cout << Next[s1.length()] << endl;else{for (int i = 0; i < Next[s1.length()]; i++)cout << s1[i];cout << " " << Next[s1.length()] << endl;}}return 0;
}

Simpsons’ Hidden Talents——kmp入门相关推荐

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

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

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

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2594 题目描述: 给定两个字符串s1,s2,问是否存在一个最长的子串,使得这个子串既是s1的前缀又是 ...

  3. HDU - 2594 Simpsons’ Hidden Talents(KMP的next数组)

    题目链接:点击查看 题目大意:给出两个字符串s1和s2,求出s1的前缀和s2的后缀可以匹配的最大长度,并输出其匹配的子字符串 题目分析:因为涉及到了前后缀的关系,我想的是可以将s1和s2连接成一个字符 ...

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

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

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

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

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

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

  7. HDU2594(Simpsons’ Hidden Talents)

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

  8. B - Simpsons’ Hidden Talents

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

  9. HDU-2594 Simpsons’ Hidden Talents

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

最新文章

  1. 回归分析中的“回归”
  2. 【React系列】状态(State)和生命周期
  3. 完整的OTT直播点播系统都有哪些功能?
  4. CodeForces - 1312C Adding Powers(思维+位运算)
  5. 帝国CMS二次元COS漫展信息分享网站模板
  6. 霍夫变换是怎么发明的?
  7. css-3d旋转相册
  8. 网络工程师之子网划分
  9. Delphi入门教程
  10. 自己用java写一个http和https代理服务器
  11. html 条纹背景,CSS3 一组条纹背景图案
  12. 使用EFI安装win7-64位,在不能使用U盘的情况下
  13. sqar+matlab,基于MATLAB的数字图像预测压缩编码
  14. 3.1.2 消费者客户端的线程模型
  15. 地理信息系统(GIS)的前沿技术综述
  16. java-php-python-springboot线上教学平台计算机毕业设计
  17. 联想IDEAPAD 320C-15笔记本显卡驱动问题
  18. 加壳学习系列(二)-壳代码
  19. 谷歌亚马逊或遭英国监管;昔日“大数据第一股”面临退市;特斯拉辅助驾驶系统索赔案胜诉丨每日大事件...
  20. 麻省理工学院、南加大等美国名校的网络安全:勉强及格

热门文章

  1. 厦理期末给爷过-Python
  2. 《这就是搜索引擎》爬虫部分摘抄总结
  3. JuiceFS 在数据湖存储架构上的探索
  4. 2018-10-29 A股主要指数的市盈率(PE)估值高度
  5. 高效App渠道统计如何进行
  6. 黑马程序员SSM_MybatisPlus笔记(自用)
  7. yun yun de
  8. PHP关于实现腾讯云直播的推流和拉流
  9. 领导要我6点下班前创建1000个有效的手机号,现在5点半了!random模块10分钟搞定!
  10. 【20210919】LaTex入门:overleaf使用