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.

InputInput consists of two lines. The first line contains s1 and the second line contains s2. You may assume all letters are in lowercase.OutputOutput 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
思路:连接连个字符串求next数组。注意next数组的值不能超过len1或者len2
#include <iostream>
#include<string.h>
using namespace std;
const int maxn = ,maxn2=*maxn;
char a[maxn],b[maxn],c[maxn2];
int ne[maxn2];
int main()
{
while(scanf("%s",a+)!=EOF)
{
scanf("%s",b+);
int len1=strlen(a+),len2=strlen(b+);
int num=;
for(int i=;i<=len1;i++)
{
c[num]=a[i];
num++;
}
for(int i=;i<=len2;i++)
{
c[num]=b[i];
num++;
}
num--;
for(int i=,j=;i<=num;i++)
{
while(j&&c[i]!=c[j+]) j=ne[j];
if(c[i]==c[j+]) j++;
ne[i]=j;
}
if(ne[num]==)
printf("0\n");
else
{
int ans=ne[num];
if(ans>len1) ans=len1;
if(ans>len2) ans=len2;
for(int i=;i<=ans;i++)
printf("%c",a[i]);
printf(" %d\n",ans);
}
}
return ;
}

Simpsons’ Hidden Talents的更多相关文章

  1. hdu 2594 Simpsons’ Hidden Talents KMP

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

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

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

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

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

  4. hduoj------2594 Simpsons’ Hidden Talents

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

  5. hdu2594 Simpsons’ Hidden Talents kmp

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

  6. hdu 2594 Simpsons’ Hidden Talents KMP应用

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

  7. hdoj 2594 Simpsons’ Hidden Talents 【KMP】【求串的最长公共前缀后缀】

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

  8. hdu2594 Simpsons' Hidden Talents【next数组应用】

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

  9. HDU2594 Simpsons’ Hidden Talents 【KMP】

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

  10. (KMP)Simpsons’ Hidden Talents -- hdu -- 2594

    http://acm.hdu.edu.cn/showproblem.php?pid=2594 Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Ja ...

随机推荐

  1. jsp三种注释方法

    HTML注释(输出注释):指在客户端查看源代码时能看见注释.例如, <!-- this is an html comment.it will show up int the response. ...

  2. 对0.5px的边的研究--------------引用

    什么是像素? 像素是屏幕显示最小的单位,在一个1080p的屏幕上,它的像素数量是1920 1080,即横边有1920个像素,而竖边为1080个.一个像素就是一个单位色块,是由rgba四个通道混合而成. ...

  3. fabric报错:Fatal error: run() received nonzero return code 1 while executing!

    今天在使用fabric远程安装rpm时,一直报:Fatal error: run() received nonzero return code 1 while executing! 这看起来也是没笔病 ...

  4. 解决JavaServer Faces 2.2 requires Dynamic Web Module 2.5 or newer问题

    ** 错误1: **在eclipse中新创建一个web项目的时候项目下的JSP文件中会爆出错误:The superclass “javax.servlet.http.HttpServlet” was ...

  5. Sql Server2008中自定义函数调用存储过程解决方案

    1.开启sql server 2008远程连接 打开sql server配置管理器 配置SSCM,选中左侧的“SQL Server服务”,确保右侧的“SQL Server”以及“SQL Server ...

  6. 好题Islands

    Orz yjc 吊打候选队 不好的思路是枚举森林的m块,这样DP显然会涉及n当做某一维,最多只能卷积优化一下 生成函数什么的n太大不用想 考虑m,k比较小,能不能把n变成一个系数,而不是维度 所以就是 ...

  7. (转)js中then方法说明

    javascript中的then方法说明: then()方法是异步执行.  意思是:就是当.then()前的方法执行完后再执行then()内部的程序,这样就避免了,数据没获取到等的问题.  语法:pr ...

  8. List of Mozilla-Based Applications

    List of Mozilla-Based Applications The following is a list of all known active applications that are ...

  9. jQuery file upload callback options

    autoUpload By default, files added to the widget are uploaded as soon as the user clicks on the star ...

  10. 基于EasyHook实现监控explorer资源管理器文件复制、删除、剪切等操作

    一.前言 最近自己在研究一个项目,需要实现对explorer资源管理器文件操作的监控功能,网上找到一些通过C++实现Hook explorer文件操作的方法,由于本人习惯用.NET开发程序,加之C/C ...