【KMP】 hdu2594 Simpsons’ Hidden Talents
来源:互联网 发布:如何修复缺少网络协议 编辑:程序博客网 时间:2024/05/19 01:32
Simpsons’ Hidden Talents
http://acm.hdu.edu.cn/showproblem.php?pid=2594
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.
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.
The lengths of s1 and s2 will be at most 50000.
Sample Input
clintonhomerriemannmarjorie
Sample Output
0rie 3
题解:把两个字符串接在一起,然后按kmp求出next数组。
#include<cstdio>#include<cstring>using namespace std;char s[100005],t[50005];int next[100005];void get_next(){ int i=0,j=-1; next[0]=-1; for(;s[i];) if(j==-1||s[i]==s[j]) { ++i;++j; next[i]=j; } else j=next[j];}int main(){ int a,b; for(int i;~scanf("%s%s",s,t);) { a=strlen(s); b=strlen(t); strcat(s,t); get_next(); for(i=a+b;next[i]>a||next[i]>b;i=next[i]); if(next[i]<=0) puts("0"); else { for(int k=0;k<next[i];++k) printf("%c",s[k]); printf(" %d\n",next[i]); } } return 0;}
- 【KMP】 hdu2594 Simpsons’ Hidden Talents
- hdu2594 Simpsons’ Hidden Talents kmp
- HDU2594 Simpsons’ Hidden Talents 【KMP】
- hdu2594 Simpsons’ Hidden Talents(KMP)
- HDU2594 Simpsons’ Hidden Talents【KMP】
- HDU2594 Simpsons’ Hidden Talents KMP
- HDU2594 Simpsons’ Hidden Talents KMP
- Simpsons' Hidden Talents hdu2594 kmp
- hdu2594 Simpsons’ Hidden Talents (kmp)
- 【hdu2594】Simpsons’ Hidden Talents——KMP
- HDU2594 Simpsons’ Hidden Talents(KMP)
- HDU2594:Simpsons’ Hidden Talents
- hdu2594-Simpsons’ Hidden Talents
- hdu2594 Simpsons’ Hidden Talents
- hdu2594 Simpsons’ Hidden Talents
- HDU2594 Simpsons’ Hidden Talents
- HDU2594 Simpsons’ Hidden Talents
- HDU2594-Simpsons’ Hidden Talents
- NSPredicate匹配中文正
- 创建索引视图
- 使用eclipse的todo标签管理任务
- 各种api大全,帮助文档,需要的迅速秒下
- 局部单例模式以及C++实现
- 【KMP】 hdu2594 Simpsons’ Hidden Talents
- unity3d开发之二:开发环境的搭建及c#注意事项
- MyEclipse 快捷键
- javaEE之路
- Service如何利用RegisterDeviceNotification监控Volume的装载和卸载 .
- 个人总结NDIS中NDIS_PACKET,NDIS_BUFFER的关系
- 北大ACM poj1016
- 操作系统引导Boot的初步实现
- 一个男人关心的东西决定了他的层次