http://acm.hdu.edu.cn/showproblem.php?pid=2594

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

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

题意:求出第一个串的最长前缀同时是后一个串的最长后缀

而kmp算法中next数组其实就是查找某串中每一位前面的子串的前后缀有多少位匹配

所以可以将两个串合并,求出next[L(L=l1+l2)],最后保证L<=l2&&L<=l1

 #include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio> using namespace std; const int N(+);
char s1[N],s2[N],s3[N<<];
int p[N<<],l1,l2,l3; inline void Get_next()
{
for(int i=,j=;i<=l1+l2;i++)
{
for(;j>&&s3[j+]!=s3[i];) j=p[j];
if(s3[j+]==s3[i]) j++;
p[i]=j;
}
} int main()
{
for(;cin>>s1+>>s2+;)
{
l1=strlen(s1+); l2=strlen(s2+);
for(int i=;i<=l1;i++) s3[i]=s1[i];
for(int i=;i<=l2;i++) s3[i+l1]=s2[i];
Get_next(); int l=l1+l2;
for(;l>l1||l>l2;) l=p[l];
for(int i=;i<=l;i++) printf("%c",s1[i]);
if(l) printf(" %d\n",l);
else puts("");
}
return ;
}

cin很灵性、、、

HDU——T 2594 Simpsons’ Hidden Talents的更多相关文章

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

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

  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 KMP

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

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

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

  5. hdu 2594 Simpsons’ Hidden Talents KMP应用

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

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

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

  7. 【HDU 2594 Simpsons' Hidden Talents】

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

  8. hdu 2594 Simpsons’ Hidden Talents

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2594 思路:将两个串连起来求一遍Next数组就行长度为两者之和,遍历时注意长度应该小于两个串中的最小值 ...

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

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

随机推荐

  1. 操作系统——第五章 输入输出(I/O)管理

    这就是SDT表和DCT表

  2. [Codeforces 816A]Karen and Morning

    题目大意:给你一个时间(hh:mm),求最少经过多少分钟才能使这个时间变成回文. 解题思路:模拟,先判断0的情况,然后每过1分钟判断一次即可. C++ Code: #include<cstdio ...

  3. Linux 中常用的基础命令一

    1.目录相关命令的使用  pwd(printing working directory) 显示当前工作目录    pwd命令相关的环境变量:     PWD  保存了当前工作目录路径     OLDP ...

  4. 如何在Google Play上通过电脑下载apk

    操作步骤: 1.首先打开翻 墙软件. 2.键入网址:http://apps.evozi.com/apk-downloader/ 3.将Google Play里apk的网址,复制到“Package na ...

  5. hdoj-1164-Eddy&#39;s research I【分解质因数】

    Eddy's research I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...

  6. Simditor用法

    一不小心接触到Simditor,瞬间被它优美极简的界面所吸引.Simditor是Tower开源的所见即所得的在线富文本编辑器. Simditor的理念是保持简单,避免过度的功能,每个特性都追求极致的用 ...

  7. BZOJ 1146 二分+链剖+线段树+treap

    思路: 恶心的数据结构题-- 首先 我们 链剖 把树 变成序列 再 套一个 区间 第K大就好了-- 复杂度(n*log^4n) //By SiriusRen #include <cstdio&g ...

  8. powerdesigner导出sql

    http://jingyan.baidu.com/article/7082dc1c48960ee40a89bd38.html 生成注释 http://wangjingyi.iteye.com/blog ...

  9. Aspose.Cells相应操作及下载

    Aspose.Cells相应操作 1,上传 1.1 Workbook Workbook workBook = new Workbook(); 属性: 名称 值类型 说明 Colors Color[] ...

  10. Smart Pointer Guidelines

    For Developers‎ > ‎ Smart Pointer Guidelines What are smart pointers? Smart pointers are a specif ...