Simpsons’ Hidden Talents

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

Total Submission(s): 4597    Accepted Submission(s): 1671

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
 

题意:给你两个字符串s1、s2。让你找出最长的字符串 使得它既是s1的前缀又是s2的后缀。

分析:f[ i ]表示字符串中第i个字符(字符串下标是从0開始的)前面的字符串的最长公共前缀后缀。 

思路:连接两个字符串。求失配函数f[]。

最后分类讨论f[len](len为连接后的新字符串长度),由于f[len]的值可能大于s1或者s2的长度。



AC代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char s1[100100], s2[50100];
char ss[50100];
int f[100100];//注意数组大小
void getfail(char *P)
{
int len = strlen(P);
f[0] = f[1] = 0;
for(int i = 1; i < len; i++)
{
int j = f[i];
while(j && P[i] != P[j])
j = f[j];
f[i+1] = P[i]==P[j] ? j+1 : 0;
}
}
int main()
{
while(scanf("%s%s", s1, s2) != EOF)
{
int l1 = strlen(s1);
int l2 = strlen(s2);
strcpy(ss, s1);
strcat(s1, s2);
getfail(s1);
int len = strlen(s1);
if(f[len])
{
if(f[len] > min(l1, l2))//特殊情况
{
if(l1 < l2)//选取长度较小的串
printf("%s %d\n", ss ,l1);
else
printf("%s %d\n", s2, l2);
}
else
{
for(int i = 0; i < f[len]; i++)
printf("%c", s1[i]);
printf(" %d\n", f[len]);
}
}
else
printf("0\n");
}
return 0;
}

hdoj 2594 Simpsons’ Hidden Talents 【KMP】【求串的最长公共前缀后缀】的更多相关文章

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

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

  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应用

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

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

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

  5. POJ 2774 求两个串的最长公共前缀 | 后缀数组

    #include<cstdio> #include<algorithm> #include<cstring> #define N 200005 using name ...

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

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

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

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

  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. hdu2594 Simpsons’ Hidden Talents kmp

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

随机推荐

  1. Could not get lock /var/lib/apt/lists/lock - open (11: Resource temporarily unavailable)

    今天在对 Ubuntu 进行更新源的时候,突然出现下列错误(为了省事,更新前直接切换了 root 用户) 上网查了一下,网上解释说应该是之前那个更新被强制取消的问题,进程仍然还在.用这个命令查看一下: ...

  2. sublime断点调试

    系统重装了之后以前装好的zendstudio的xDebug又不能用了 搞了一天,放弃了 看到sublime也能调试,我就用sublime了 首先要下sublime插件 链接:http://pan.ba ...

  3. ctsc2018

    day1: 8:20分还不知道考场在哪给ccf差评 8:30开始看题 第一题感觉是个模拟啊 很烦 瞄了一眼第二题第三题题意挺简单的啊感觉还不错 然后开始仔细看t1 然后我发现好像可以退狮子 应该是个数 ...

  4. 矩阵优化dp

    链接:https://www.luogu.org/problemnew/show/P1939 题解: 矩阵优化dp模板题 搞清楚矩阵是怎么乘的构造一下矩阵就很简单了 代码: #include < ...

  5. BZOJ1264 [AHOI2006]基因匹配Match 动态规划 树状数组

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1264 题意概括 给出两个长度为5*n的序列,每个序列中,有1~n各5个. 求其最长公共子序列长度. ...

  6. 【noip模拟赛10】奇怪的贸易 高精度

    描述 刚结束了CS战斗的小D又进入了EVE的游戏世界,在游戏中小D是一名商人,每天要做的事情就是在这里买东西,再运到那里去卖.这次小D来到了陌生的X星,X星上有n种货物,小D决定每种都买走一些,他用a ...

  7. 【Java】 剑指offer(6) 重建二叉树

    本文参考自<剑指offer>一书,代码采用Java语言.  更多:<剑指Offer>Java实现合集 题目 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的 ...

  8. OpenCV中cv2的用法

    一.读入图像 使用函数cv2.imread(filepath,flags)读入一副图片 filepath:要读入图片的完整路径 flags:读入图片的标志  cv2.IMREAD_COLOR:默认参数 ...

  9. Linux 之 AT&T汇编语言 mov、add、sub指令、数据段

    mov指令的几种形式: mov 寄存器. 数据 mov ax,8888 mov 寄存器. 寄存器 mov bx,ax mov 寄存器. 内存单元 mov ax,[0] mov 内存单元.寄存器 mov ...

  10. MAC OS X下配置PHP开发、调试环境

    操作系统:MAC OS X 工具:MAMP.PhpStorm.xdebug.chrome 1.下载MAMP 2.安装比较简单,安装完成后,应用程序中会增加如下4个应用 MacGDBp是PHP调试器,使 ...