Simpsons’ Hidden Talents

Problem 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

题意描述:

输入a,b两个串求a的前缀与b串的后缀相同的部分

程序代码:

#include<stdio.h>
#include<string.h>
void get_next(char *s);
int kmp(char *s1,char *s2);
int next[50010];
char a[50010],b[50010];
int main()
{int t;while(scanf("%s%s",a,b)!=EOF){t=kmp(b,a);if(t==0)printf("0\n");elseprintf("%s %d\n",b+(strlen(b)-t),t);}return 0;
}
void get_next(char *s)
{int i,j,lens;i=1;j=0;next[0]=0;lens=strlen(s);while(i<lens){if(j==0&&s[i]!=s[j]){next[i]=0;i++;}else if(j>0&&s[i]!=s[j])j=next[j-1];else{next[i]=j+1;i++;j++;}}
}
int kmp(char *s1,char *s2)
{int i,j,lens1;get_next(s2);i=0;j=0;lens1=strlen(s1);while(i<lens1){if(j==0&&s1[i]!=s2[j])i++;else if(j>0&&s1[i]!=s2[j])j=next[j-1];else {i++;j++;}}return j;
}

HDU-2594-Simpsons’ Hidden Talents (kmp)相关推荐

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

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

  2. Simpsons’ Hidden Talents (HDU-2594)

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

  3. 【 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.  ...

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

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

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

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

  6. Simpsons’ Hidden Talents(HDU-2594)

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

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

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

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

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

  9. Simpsons’ Hidden Talents(扩展KMP)

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

最新文章

  1. Ubuntu系统执行shell 脚本的方法
  2. CodeForces 518A - Chewbaсca and Number(思路)
  3. 浏览器缓存机制学习总结
  4. DataTables中给某列添加样式
  5. VMware vsphere client报错问题
  6. java存储三元组_在4store中添加三元组
  7. chmod、chown函数的使用
  8. 密码学专题 OpenSSL中SSL相关指令
  9. elasticsearch初次查询超时_ElasticSearch的工作流程
  10. java stl分解_[STL训练]寻梦-题解(Java代码)极简思路
  11. 嵌入式C语言自我修养 08:变参函数的格式检查
  12. 中国基站射频设备行业市场供需与战略研究报告
  13. [Web开发] 如何改变IE滚动条的颜色
  14. 损失函数——交叉熵由来
  15. 如何创建“搜狗词库”
  16. 计算机英语emulated,【英语词汇】 imitate、mimic、mock、 simulate、emulate 这组词都有...
  17. jdk文件夹里点哪个是安装_jdk在哪个文件夹里面
  18. layui怎么设置select默认选中,修改回显
  19. Python敏感词汇检测
  20. 树莓派自带wifi工作不正常

热门文章

  1. Linux-dmidecode 下面查看主机BIOS信息命令
  2. SpringBoot启动源码探究---getRunListener()
  3. Linux下扩容系统容量和删除unknown PV
  4. inline-block,一个奇怪的问题:中间div掉下来
  5. 02-虚拟机克隆配置
  6. 理解浏览器和nodeJs中的事件循环(Event Loop)
  7. 【Linux】VirtualBox安装ubuntu排错LowGraphic
  8. [导入]正则表达式学习心得体会(2)
  9. 【elasticsearch】ES启动报错 uncaught exception in thread [main]org.elasticsearch.bootstrap.Startup
  10. BigInteger类详解