这个题和kmp算法的共同点,也就是可以用kmp解的原因,在于当前缀所在串(kmp中的模式串)字符pj≠后缀所在串(kmp中文本串)字符tj时,应使前缀串(kmp中模式串)尽量往右移动最大位移,而暴力算法则是每次移动位移为1。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>using namespace std;#define maxn 50005char t[maxn], p[maxn];
int next[maxn];void getNext(int *next, char *p, int size){int j=0, k=next[0]=-1;while(j<size){if(k==-1 || p[j]==p[k]) next[++j]=++k;else k=next[k];}
}int kmp(char *t, int lt, char *p, int lp){int i=0, j=0;while(i<lt && j<lp){if(j==-1 || t[i]==p[j]) i++, j++;else j=next[j];}return j;           //没找到j=0,否则j=length of common substring
}int main(){while(scanf(" %s %s", p, t)==2){int lt = strlen(t),  lp = strlen(p);char *text = lt > lp ? lt-lp+t : t;lt = min(lt, lp);getNext(next, p, lp);int k = kmp(text, lt, p, lp);if(!k) printf("0\n");else{p[k]=0;printf("%s %d\n", p, k);}}return 0;
}

Simpsons’ Hidden Talents

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

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
HDU 2010-05 Programming Contest
Recommend
lcy   |   We have carefully selected several similar problems for you:  1711 1686 2595 2596 2597

转载于:https://www.cnblogs.com/ramanujan/p/3737739.html

hdu 2594 kmp相关推荐

  1. hdu 2594(kmp)

    1 /* 2 * KMP 3 * 思路:把两个串连接,然后求next[]数组值 4 * 注意:当两个串都是重复串且重复子串一样是,要特殊处理 5 */ 6 7 #include <cstdio& ...

  2. HDU 2594 - Simpsons’ Hidden Talents(KMP)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2594 题目描述: 给定两个字符串s1,s2,问是否存在一个最长的子串,使得这个子串既是s1的前缀又是 ...

  3. HDU 2594 Simpsons’ Hidden Talents (字符串-KMP)

    Simpsons' Hidden Talents Problem Description Homer: Marge, I just figured out a way to discover some ...

  4. 【 HDU - 2594 】Simpsons’ Hidden Talents(KMP应用,求最长前缀后缀公共子串)

    题干: Homer: Marge, I just figured out a way to discover some of the talents we weren't aware we had.  ...

  5. HDU - 2594 Simpsons’ Hidden Talents(KMP的next数组)

    题目链接:点击查看 题目大意:给出两个字符串s1和s2,求出s1的前缀和s2的后缀可以匹配的最大长度,并输出其匹配的子字符串 题目分析:因为涉及到了前后缀的关系,我想的是可以将s1和s2连接成一个字符 ...

  6. Simpsons’ Hidden Talents(辛普森一家的隐藏天赋 )(kmp经典模板题) HDU - 2594

    题目:Simpsons' Hidden Talents(辛普森一家的隐藏天赋 ) 中文大意 Homer: Marge, I just figured out a way to discover som ...

  7. Simpsons’ Hidden Talents HDU - 2594

    http://acm.hdu.edu.cn/showproblem.php?pid=2594 EXKMP详解https://blog.csdn.net/dyx404514/article/detail ...

  8. HDU 2087 (KMP不可重叠的匹配) 花布条

    题意: 用两个字符串分别表示布条和图案,问能从该布条上剪出多少这样的图案. 分析: 毫无疑问这也是用KMP匹配,关键是一次匹配完成后,模式串应该向后滑动多少. 和上一题 HDU 1686 不同,两个图 ...

  9. HDU 3336 KMP

    题意:求每一个前缀,跟前缀相同的每个子串. 此题:网上很多都是假程序,不过也AC了,的确我测试几个案例之后的的确确是存在这个问题. 分析:每一个前缀,可以考虑KMP,f失配指针,如何求得它出现了多少次 ...

最新文章

  1. AsyncTask工作机制简介
  2. python atm作业详解_Python ATM功能实现代码实例
  3. 代码中特殊的注释技术——TODO、FIXME和XXX的用处
  4. kafka 学习 非常详细的经典教程
  5. 打游戏提示计算机丢失,Windows7电脑运行某游戏提示“计算机丢失mxvcp120.dll”怎么办...
  6. linux系统怎么安装cas,CAS 在Linux中安装与配置
  7. java 泛型集合示例_Java 泛型(示例代码)
  8. 使用thead,tbody,tfoot来实现表格的分页打印
  9. 数据库水平切分的实现原理解析---分库,分表,主从,集群,负载均衡器...
  10. java 计算器 下载_那里可以下载到JAVA编的计算器程序??
  11. Project项目视图
  12. 使用CASEWHEN批量拼凑Update语句
  13. 原神3.0上半角色活动祈愿-2源码
  14. 一个免费识别验证码的接口
  15. 简单Java的商品模块功能
  16. Buffer.from()
  17. Android Studio 连接雷电模拟器
  18. laydate设置只选择年月
  19. 关于单边账的解释及解决
  20. RFID标签的基础知识(3)--了解芯片(之超高频标签芯片篇)

热门文章

  1. firebase 发生消息_如何在命令行提示符下显示当前的Firebase项目名称,以防止发生危险错误...
  2. 参加Python培训后能干嘛
  3. Unreal Engine 4 优化教程
  4. (二十一)数组的初始化
  5. String、StringBuffer、StringBuilder的理解
  6. Josephus Problem的详细算法及其Python, Java语言的实现
  7. 【AngularJS】—— 12 独立作用域
  8. 一篇文章读懂阿里云负载均衡性能保障型实例
  9. 使用 CAS 在 Tomcat 中实现单点登录
  10. 【原】YUI压缩与CSS media queries下的bug