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. DeepMind 综述深度强化学习:智能体和人类相似度竟然如此高!
  2. 常用oracle数据库函数总结
  3. html5关于定位功能的实现
  4. 用VS2010调试微软开放的部分源码
  5. linux安装libfastcommon+fastdfs+nginx
  6. nginx 重定向_虚拟机中安装nginx,重定向到resin
  7. 从外部CorDapp扩展和覆盖流
  8. python神经网络教程16_Python深度学习之神经网络视频
  9. java 复合_【福利】java新手做的复合型计算器!
  10. android触屏设备event模拟,android 使用命令模拟点击 滑动
  11. gie拉取Can‘t update master has no tracked branch
  12. python与爬虫-02复杂的HTML解析
  13. 湖北省211大学计算机分数线,武汉7所211大学湖北省录取分数线2020
  14. 如何用微信小程序,每天给自己赚个鸡腿?
  15. Git 详细安装教程(最新保姆级)
  16. python快速入门【四】-----各类函数创建
  17. 蓝桥杯 兰顿蚂蚁【模拟】
  18. day 和datepart 日期函数
  19. 网页不收录的5大原因及解决办法
  20. csc_matix函数

热门文章

  1. 【Vue入门实践】Element-UI 树形组件el-tree的组件封装 =>实现组织机构树Tree => 使用vue-content-menu定制可编辑树结构editableTree
  2. 英语不好能学会python吗_英语不好可以学习python编程吗?老男孩python入门
  3. http方法有哪些?get、post、put、delete,为什么一般只用过get和post?什么时候用put、delete,为什么没用过
  4. Activiti 数据库表结构 ACT_GE_PROPERTY
  5. 自然场景文本检测识别技术综述【转】
  6. cocos studio
  7. 订单系统——提交订单
  8. 如何下载网页上网站上的视频
  9. 如何利用人性的弱点在互联网中找到利润高的项目
  10. 开源框架 crux的生成