Description

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

Code

#include<iostream>
#include <string.h>
#include <string>
#include <stdio.h>
using namespace std;const int MAXN=1e5+1;
int nxt[MAXN];
char w[MAXN],t[MAXN];inline void get_next(){int p=nxt[0]=0,lenw=strlen(w);for(int i=1;i<lenw;i++){while(p&&w[p]!=w[i])    p=nxt[p-1];if(w[p]==w[i]) p++;nxt[i]=p;}}int main(){//freopen("/Users/guoyu/Desktop/algorithm/in.txt", "r", stdin);while(scanf("%s%s", w, t)!= EOF){int len1=strlen(w),len2=strlen(t);strcat(w, t);get_next();int len=strlen(w);int ans=nxt[len-1];while(ans>len1||ans>len2) ans=nxt[ans-1];if(ans==0) cout<<0<<endl;else printf("%s %d\n",w+len-ans,ans);}return 0;
}

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. Simpsons’ Hidden Talents (HDU-2594)

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

  9. 【 HDU - 2594 】Simpsons’ Hidden Talents(KMP应用,求最长前缀后缀公共子串)

    题干: Homer: Marge, I just figured out a way to discover some of the talents we weren't aware we had.  ...

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

    辛普森一家的隐藏天赋 HDU - 2594 目录 辛普森一家的隐藏天赋 HDU - 2594 题意描述:当给定字符串s1和s2时,找到s1中最长的前缀,即s2的后缀.如果有,输出相同的字符串即字符串长 ...

最新文章

  1. CEO换人、IPO延期,比特大陆艰难转型路漫漫
  2. 使用mss2sql将SqlServer转换为Mysql
  3. 如何在保留订单的同时从列表中删除重复项?
  4. pytorch 优化GPU显存占用,避免out of memory
  5. 计算机组成原理作业1王诚,计算机组成原理 王诚 董长洪 宋佳兴
  6. VB DATA控件链接SQL SERVER
  7. 变频电源外围配置的全面要点
  8. html文字依次显示,利用定时器和css3动画制作文字依次渐变显示的效果
  9. Failed to read artifact ......明明之前可以的
  10. java 算法递归案例_JAVA 几个递归算法实例
  11. 三个线性同余方程组的计算机解决方案(C程序)
  12. java计算机毕业设计学术会议信息网站源码+mysql数据库+lw文档+系统+调试部署
  13. golang: 密码中允许出现数字、大写字母、小写字母、特殊字符,但至少包含其中2种且长度在8-16之间(四种符号任意满足三种即可)
  14. msp430单片机c语言应用程序,MSP430单片机C语言应用程序设计实例精讲
  15. UR机器人C语言和Python编程控制
  16. Linux存储性能观测——vmstat命令详解
  17. 手动卸载 Office 2013 或 Office 365
  18. 最新百家姓-你排老几
  19. 485通信原理_上位机开发之单片机通信实践(一)
  20. [Python从零到壹] 五十五.图像增强及运算篇之图像平滑(均值滤波、方框滤波、高斯滤波)

热门文章

  1. 跳过校园网开机直连宽带
  2. java语言的编译器_JAVA语言编译器是一个CASE工具。()。
  3. 共享Linux服务器上的刻录机
  4. 深圳铨顺宏圆满落幕IOTE 2022第十八届国际物联网展
  5. 仿陌陌遇见社交聊天APP源码 网页聊天 LBS地址类社交APP 含Java服务端、PhP管理端等
  6. 考研最辛苦、最努力的一批人,喊累之前先看看他们……
  7. lego-loam学习笔记(一)
  8. Vue数据可视化组件库,类阿里DataV,提供SVG的边框及装饰,图表,飞线图等组件,简单易用,持续更新...
  9. matlab bwmorph spur,matlab图像处理学习笔记-数学形态与二值图像操作
  10. arclinux和其他linux发行版本的比较