Simpsons’ Hidden Talents

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4683    Accepted Submission(s): 1702

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
 
Source
 
题意:给定两个串S1和S2,你要找到S1的最长前缀,且这个前缀还要是S2的后缀.
题解:KMP计算串S2的后缀能匹配S1的前缀是多长

#include <cstdio>
#include <string>
#include <iostream> using namespace std; int maxl[],p[]; int main()
{string a,b;
while(cin>>b>>a)
{ b=" "+b;
a=" "+a;
int m=b.length();
int n=a.length();
n--;
m--;
memset(p,,sizeof(p));
memset(maxl,,sizeof(maxl));
p[]=p[]=;
int j=;
for(int i=;i<=m;i++)
{
while(j>&&b[j+]!=b[i])j=p[j];
if(b[j+]==b[i])j++;
p[i]=j;
}
j=;
for(int i=;i<=n;i++)
{
while(j>&&b[j+]!=a[i])j=p[j];
if(b[j+]==a[i])j++;
maxl[i]=j;
}
if(maxl[n]==)cout<<<<endl;
else {
for(int i=;i<=maxl[n];i++)
cout<<b[i];
cout<<" "<<maxl[n]<<endl;
} getchar();
}
return ;
}

代码

hdu 2594 Simpsons’ Hidden Talents KMP的更多相关文章

  1. hdu 2594 Simpsons’ Hidden Talents KMP应用

    Simpsons’ Hidden Talents Problem Description Write a program that, when given strings s1 and s2, fin ...

  2. hdu 2594 Simpsons’ Hidden Talents(KMP入门)

    Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...

  3. HDU 2594 Simpsons’ Hidden Talents(KMP求s1前缀和s2后缀相同部分)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2594 题目大意:给两串字符串s1,s2,,找到最长子串满足既是s1的前缀又是s2的后缀,输出子串,及相 ...

  4. HDU 2594 Simpsons’ Hidden Talents(辛普森一家的潜在天赋)

    HDU 2594 Simpsons’ Hidden Talents(辛普森一家的潜在天赋) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 3 ...

  5. HDU 2594 Simpsons’ Hidden Talents(KMP的Next数组应用)

    Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...

  6. HDU 2594 Simpsons’ Hidden Talents (KMP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2594 这题直接用KMP算法就能够做出来,只是我还尝试了用扩展的kmp,这题用扩展的KMP效率没那么高. ...

  7. hdu 2594 Simpsons’ Hidden Talents 【KMP】

    题目链接:http://acm.acmcoder.com/showproblem.php?pid=2594 题意:求最长的串 同一时候是s1的前缀又是s2的后缀.输出子串和长度. 思路:kmp 代码: ...

  8. hdu 2594 Simpsons’ Hidden Talents(扩展kmp)

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

  9. 【HDU 2594 Simpsons' Hidden Talents】

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

随机推荐

  1. LinkedBlockingQueue和ConcurrentLinkedQueue详细用法

    1.LinkedBlockingQueue<E>:java.util.concurrent API中的解释: public class LinkedBlockingQueue<E&g ...

  2. 大数据下Limit使用(MySQL)

    对于一直用Oracle的我,今天可是非常诧异,MySQL中同一个函数在不同数量级上的性能居然差距如此之大. 先看表ibmng(id,title,info)  唯一  id key 索引title 先看 ...

  3. HDOJ 2089 不要62

    不要62 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  4. Parencodings(imitate)

    Parencodings Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20679   Accepted: 12436 De ...

  5. cc表示Cocos核心,ccs代表CocoStudio,ccui代表CocoStudio的UI控件

    cc表示Cocos核心,ccs代表CocoStudio,ccui代表CocoStudio的UI控件

  6. [Effective JavaScript 笔记]第40条:避免继承标准类

    ECMAScript标准库里配备了许多重要的类,如Array,function,以及Date等.扩展这些类生成子类可以方便完成很多工作,但它们的定义具有很多特殊的行为,所以很难写出行为正确的类. Ar ...

  7. python学习之最简单的用户注册及登录验证小程序

    文章都是从我的个人博客上粘贴过来的哦,更多内容请点击 http://www.iwangzheng.com 正如很多同学所知道的,楼主开始学习python了,前进的道路曲曲折折,有荆棘也有陷阱,从最简单 ...

  8. OpenCV入门(一)

    参考:http://blog.csdn.net/poem_qianmo/article/details/20537737 这位同学挺牛的,才研一就出书了,实在是让人汗颜啊,不说了,多学习. 这一篇主要 ...

  9. tomcat配置文件之Server.xml

    Server.xml包含的元素有<Server>.<Service>.<Connector>.<Engine>.<Host>.<Con ...

  10. Verify Preorder Serialization of a Binary Tree

    One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, ...