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. 1.关于python 的hmac加密

    import base64 import hmac import urllib from hashlib import sha1 expires = b" # 过期时间戳 uuid = 'a ...

  2. pd.read_csv参数解析

    对pd.read_csv参数做如下解释: pandas.read_csv(filepath_or_buffer, sep=', ', delimiter=None, header='infer', n ...

  3. CUDA开发指南

    安装指南:https://blog.csdn.net/qilixuening/article/details/77503631 安装了anaconda不需要安装cuda和cudnn?:https:// ...

  4. Jenkins部署从节点

    由于jenkins上承载项目太多,需要专门的节点来执行需要构建的操作. 参考:https://www.cnblogs.com/lxs1314/p/7551309.html job仅使用绑定的slave ...

  5. java web程序上传文件,浏览器显示连接被重置

    上传文件时,到13%时浏览器显示连接被重置如图: 参考网上很多方法 比如设置server.xml 的相应大小.时间,然并没有解决问题 connectionTimeout="2000000&q ...

  6. Proxy-代理器(预计vue3.0实现双向绑定的方式)

    todo 常见的基于数据劫持的双向绑定有两种实现,一个是目前Vue在用的Object.defineProperty,另一个是ES2015中新增的Proxy,而Vue的作者宣称将在Vue3.0版本后加入 ...

  7. [LeetCode]-010-Regular_Expression_Matching

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  8. Ajax监测开始执行及结束执行

    特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...

  9. MongoDB通过JavaDriver执行shell命令,例如创建sharding collection

    Mongodb的java driver本身的接口 void createCollection(String collectionName, CreateCollectionOptions create ...

  10. webpack插件之htmlWebpackPlugin

    webpack插件之htmlWebpackPlugin webpack插件 自动化 htmlWebpackPlugin  由于webpack已经帮我们处理好js之间的依赖关系,现在我们可以忽略js的加 ...