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

这题意思呢就是在串1找前缀,在串2找后缀,要求最长且相同。将两个串连接起来不就是很简单了

#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=5e4+10;
char  a[2*maxn],b[maxn];
char s[2*maxn];
int ne[2*maxn];
void get(int len)
{ne[0]=-1;int j=-1;for(int i=0;i<len;){if(j==-1||s[i]==s[j]) ne[++i]=++j;else j=ne[j];}
}
int main()
{while(~scanf("%s%s",a,b)){strcpy(s,a);strcat(s,b);int len=strlen(s);int len2=strlen(b);int len1=strlen(a);get(len);int n=ne[len];if(n>=len1||n>=len2){if(n==len1&&n==len2) printf("%s %d\n",a,n);else if(len1<len2) printf("%s %d\n",a,len1);else printf("%s %d\n",b,len2);}else{bool flag=0;for(int i=0;i<ne[len];i++){printf("%c",s[i]);flag=1;}if(flag) printf(" ");printf("%d\n",n);}}
}

I - 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. HDU2594(Simpsons’ Hidden Talents)

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

  4. B - Simpsons’ Hidden Talents

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

  5. HDU-2594 Simpsons’ Hidden Talents

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

  6. Simpsons’ Hidden Talents (HDU-2594)

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

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

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

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

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

  9. HDU2594——Simpsons’ Hidden Talents

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

  10. kuangbin专题十六 KMP扩展KMP HDU2594 Simpsons’ Hidden Talents

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

最新文章

  1. oracle修改备库状态,Oracle-CRSCTL命令显示备库状态不正确
  2. TCP之深入浅出send和recv
  3. Oracle 事务概述
  4. 一位跨平台开发者的自白
  5. PHP socket
  6. js文件中使用jstl或者其他标签
  7. MySQL创建价格_mysql 建表时的价格用什么类型定义?
  8. 分享一个真正高收益,一本万利的行业
  9. 深入css布局 (3)完结 — margin问题与格式化上下文
  10. php之将用户信息写入数据库
  11. linux内存管理之DMA
  12. 7-60 致命的珠宝 (10分)
  13. 服务器网赚项目,草根站长的一个网赚项目的分析和总结
  14. 手机的小窗口怎么弄_vivo怎么弄小窗口 vivo怎样弄小窗口
  15. vue中模拟试卷出题,
  16. 新辰:台北90后创业炸鸡配啤酒 来自星星的你 不成功也难
  17. 在线教育凛冬将至!强敌环伺的尚德机构,能否突出重围?
  18. rar,zip文件加密判断
  19. 切比雪夫不等式例题讲解_2019高考数学大题押题(名师视频讲解,高考最后的提分机会)...
  20. 实验二 插值方法(android)

热门文章

  1. 程序员的祖师爷到底有多么牛逼?
  2. Spring的maven项目单元测试报错 : Error:java:JDK isn't specified for module
  3. 盘点:三十五个非主流开源数据库
  4. 用python画小黄人
  5. 如何切换到老版本的IE浏览器
  6. 搜狗搜索或成为企鹅号流量入口:腾讯欲实现自己的流量闭环
  7. 微信小程序在 wxml 文件中使用 Array.includes 方法
  8. STM8L USART串口使用
  9. java:1:错误:写入Hello时出错:Hello.class(拒绝访问)
  10. 小米手机升级MIUI11后,要记得关闭这4个按钮,不然电池就会不耐用