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
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  2595 2596 2597 2598 2599 
 
 
题解:求最长的相同前缀和后缀...注意kmp函数中的要遍历第二条串的次数...
 
代码:
 #include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
#include <queue>
using namespace std;
#define INF 0x3f3f3f3f char p[],s[];
int nex[],ex[]; void get(char *p)
{
int plen=strlen(p);
nex[]=-;
int k=-,j=;
while(j < plen){
if(k==- || p[j] == p[k]){
++j;
++k;
if(p[j] != p[k])
nex[j]=k;
else
nex[j]=nex[k];
}
else{
k=nex[k];
}
}
} void kmp(char *p,char *s)
{
get(p);
ex[]=;
int i=,j=;
int slen=strlen(s);
int plen=strlen(p);
while(i < slen ){
if(j==- || s[i]==p[j]){
++i;
++j;
ex[i]=j;
}
else{
j=nex[j];
}
}
/*if(j == plen)
return i-j;
else
return -1;*/
} int main()
{
while(~scanf("%s%s",p,s)){
kmp(p,s);
int len=strlen(s);
if(ex[len]==)
printf("0\n");
else{
for(int i=; i<ex[len]; i++)
printf("%c",p[i]);
printf(" %d\n",ex[len]);
}
}
return ;
}

hdu Simpsons’Hidden Talents(kmp)的更多相关文章

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

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

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

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

  4. hdu2594 Simpsons’ Hidden Talents kmp

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

  5. HDU2594 Simpsons’ Hidden Talents —— KMP next数组

    题目链接:https://vjudge.net/problem/HDU-2594 Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Oth ...

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

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

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

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

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

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

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

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

随机推荐

  1. Windows Phone 8 - Runtime Location API - 2

    原文:Windows Phone 8 - Runtime Location API - 2 在<Windows Phone 8 - Runtime Location API - 1>介绍基 ...

  2. android studio 怎样正确导入jar

    近期又開始做android,使用android studio中遇到导入jar没有反应的问题,查了下资料实践攻克了,现特地写一下博客.希望对刚刚的使用的android studio的朋友有帮助. 1.先 ...

  3. CF 452A(Eevee-直接试)

    A. Eevee time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...

  4. [LeetCode101]Symmetric Tree

    题目: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). F ...

  5. Oracle性能优化顺序表名称来选择最有效的学习笔记

    选择最有效的顺序表名(只有有效的基于规则的优化)  ORACLE分析器按照订单处理从右到左FROM在FROM子句中的表名,故FROM写在最后的表(基础表 driving table)将被最先处理. 在 ...

  6. client多线程

    1.多线程对象 对象可以是多线程访问,线程可以在这里分为两类: 为完成内部业务逻辑的创建Thread对象,线程需要访问对象. 使用对象的线程外部对象. 进一步假设更精细的划分.业主外螺纹成线等线,. ...

  7. 完全背包(南阳oj311)(完全背包)

    全然背包 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描写叙述 直接说题意,全然背包定义有N种物品和一个容量为V的背包.每种物品都有无限件可用. 第i种物品的体积是c.价值 ...

  8. C日常语言实践中小(四)——勇者斗恶龙

    勇者斗恶龙 愿你的国有n龙的头,你想聘请骑士杀死它(全部的头). 村里有m个骑士能够雇佣,一个能力值为x的骑士能够砍掉恶龙一个致敬不超过x的头,且须要支付x个金币. 怎样雇佣骑士才干砍掉恶龙的全部头, ...

  9. 在Linux下,在网络没有配置好前,怎样查看网卡的MAC地址?

    在Linux下,在网络没有配置好前,怎样查看网卡的MAC地址? 使用 dmesg 与 grep 命令来实际,例如以下: [root@localhost ~]# dmesg | grep eth e10 ...

  10. 写hive sql和shell脚本时遇到几个蛋疼的问题!

    错误一: Hive的where后不能用字段的别名, 错误二: hive的groupby中不能用自己定义函数,否则报错(用嵌套select取代) 错误三: 运行:$ ./hive_game_operat ...