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. 使用Adivisor配置增强处理

    使用Adivisor配置增强处理 实现步骤: 1.通过MethodBeforeAdivice接口实现前置增强处理 public class ServiceBeforeAdvisor implement ...

  2. MyBatis 用户表记录数查询

    搭建MyBatis开发环境,实现用户表记录数查询 1.在MyEclipse中创建工程,导入MyBatis的jar包

  3. 高效使用git的一些命令

    1,添加文件到版本库        添加单个文件: git add filename        添加所有txt文件: git add *.txt        添加所有修改文件: git add ...

  4. vmware, failed to lock the file

    电脑死机,进不了桌面,实在不行就重启,但是在运行着虚拟机,重启后打开VMware虚拟机,提示failed to lock the file,进不去了,急阿,里面不少资料呢...问万能的Google,终 ...

  5. 淘宝(阿里百川)手机客户端开发日记第四篇 自定义ListView详解

    我们知道,如果采用官方的ListView,实现的功能在很多时候,并不能满足自己的业务需求,比如在设计到复杂的列表的时候,这一节,我们就开始动手自己实现自定义的ListView. 在上一节中,我们采用了 ...

  6. search in 2d matrix and serach minimum in rotated array

    import java.io.*; import java.lang.reflect.Array; import java.util.Arrays; import java.util.Collecti ...

  7. 《linux备份与恢复之二》3.19 dump(文件系统备份)

    <Linux指令从初学到精通>第3章文件管理,本章介绍了许多常用命令,如cp.ln.chmod.chown.diff.tar.mv等,因为这些都与文件管理相关,在日常的使用中经常用到,因此 ...

  8. linux ldconfig

    http://blog.csdn.net/dante_k7/article/details/7211868 ldconfig的主要用途: 默认搜寻/lilb和/usr/lib,以及配置文件/etc/l ...

  9. ubuntu tar 命令详细讲解

    Ubuntu--tar命令 tar zxvf ut6410-android2.1.tgz tar zcvf ut6410-android2.1.tgz ut6410-android2.1/ tar - ...

  10. maven的一些依赖

    maven的一些依赖: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://w ...