Simpsons’ Hidden Talents

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

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
题意:给两个字符串str1和str2, 求出是str1的前缀且是str2的后缀的最长的字符串
/*
ID: LinKArftc
PROG: 2594.cpp
LANG: C++
*/ #include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <utility>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define eps 1e-8
#define randin srand((unsigned int)time(NULL))
#define input freopen("input.txt","r",stdin)
#define debug(s) cout << "s = " << s << endl;
#define outstars cout << "*************" << endl;
const double PI = acos(-1.0);
const double e = exp(1.0);
const int inf = 0x3f3f3f3f;
const int INF = 0x7fffffff;
typedef long long ll; const int maxn = ; char str[maxn], str1[maxn];
int Next[maxn]; void getNext(char *p) {
int len = strlen(p);
int i = , j = -;
Next[i] = j;
while (i < len) {
if (j == - || p[i] == p[j]) {
i ++; j ++;
Next[i] = j;
} else j = Next[j];
}
} int main() {
while (~scanf("%s %s", str, str1)) {
int len1 = strlen(str);
int len2 = strlen(str1);
strcat(str, str1);
getNext(str);
int len = strlen(str);
int cnt = Next[len];
while (cnt > len1 || cnt > len2) {
cnt = Next[cnt];
}
if (cnt == ) printf("0\n");
else {
for (int i = ; i < cnt; i ++) printf("%c", str[i]);
printf(" %d\n", cnt);
}
} return ;
}

HDU2594(简单KMP)的更多相关文章

  1. hdu2594 简单KMP

    题意:      给你两个串,问你s1的前缀和s2的后缀最长公共部分是多少. 思路:      根据KMP的匹配形式,我们求出s1的next,然后用s1去匹配s2,输出当匹配到s2的最后一个的时候的匹 ...

  2. 简单kmp算法(poj3461)

    题目简述: 给你两个字符串p和s,求出p在s中出现的次数. 思路简述: 在介绍看BF算法时,终于了解到了大名鼎鼎的KMP算法,结果属于KMP从入门到放弃系列,后来看了几位大神的博客,似乎有点懂了.此题 ...

  3. HDU 2087 剪花布条 (简单KMP或者暴力)

    剪花布条 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  4. POJ2406简单KMP

    题意:      给一个字符串,求最大的前缀循环周期,就是最小的循环节对应的最大的那个周期. 思路:      KMP的简单应用,求完next数组后有这样的应用:next[i] :是最大循环节的第几位 ...

  5. HDU 1358 简单kmp

    题目大意: 找到所有的可组成连续字符串相连的位置,和循环字符串的个数 #include <cstdio> #include <cstring> #include <alg ...

  6. 剪花布条 - HDU 2087(简单KMP | 暴力)

    分析:基础的练习............... ============================================================================ ...

  7. CSU 1598 最长公共前缀 (简单KMP或者暴力)

    Submit Page    Summary    Time Limit: 1 Sec     Memory Limit: 128 Mb     Submitted: 226     Solved: ...

  8. POJ 2406 Power Strings 简单KMP模板 strcmp

    http://poj.org/problem?id=2406 只是模板,但是有趣的是一个strcmp的字符串比较函数,学习到了... https://baike.baidu.com/item/strc ...

  9. HDU2594 【KMP】

    题意: 给两个字符串s1,s2,求最长的s1前缀匹配s2后缀的字符串,以及长度 思路: 利用KMP看下最终匹配到了哪个位置:一个是利用常规匹配,另一个是利用next数组的跳转. #include< ...

随机推荐

  1. 梳理 Opengl ES 3.0 (一)宏观着眼

    Opengl ES 可以理解为是在嵌入式设备上工作的一层用于处理图形显示的软件,是Opengl 的缩水版本. 下图是它的工作流程示意图: 注意图中手机左边的EGL Layer Opengl ES是跨平 ...

  2. 阿里的100TB Sort Benchmark排序比雅虎快了一倍还多,我的看法

    如果我的判断正确,它们使用的软件和算法应该是HADOOP,MAP/REDUCE,或者类似的技术方案.如果这些条件一样,影响计算结果的还有三个因素: 1.CPU的数量和CPU的处理能力     CPU的 ...

  3. 解决:Unable to execute dex: GC overhead limit exceeded

    转自http://blog.sina.com.cn/s/blog_6e334dc70101hnug.html Android打包时下面的错误: Unable to execute dex: GC ov ...

  4. MySQL linux错误处理

    https://cloud.tencent.com/developer/article/1023732 mysql5.7 ERROR 1045 (28000): Access denied for u ...

  5. 基于eth快速发行自己的数字货币

    我们总在寻觅,也不断迷失. 像一颗飘摇的韭菜,彷徨而又无奈,无奈又彷徨. 如果你问我未来,我不知去向何方 我只知道生长,恣意野蛮. 我们不断在追寻一款爆款的项目,一个百倍币千倍币,却没有想到,实际上做 ...

  6. Chrome 与 Firefox-Dev 的 DevTools

    不管是做爬虫还是写 Web App,Chrome 和 Firefox 的 DevTools 都是超常用的,但是经常发现别人的截图有什么字段我找不到,别人的什么功能我的 Chrome 没有,仔细一搜索才 ...

  7. 【Python】- 如何使用Visual Studio 2013编写python?

    安装Visual Studio 2013 1.VS2013下载安装略 安装python2.7 1.从官网下载python2.7,下载地址:https://www.python.org/getit/  ...

  8. 域名解析的DNS缓存如何清理

    域名解析(DNS)缓存是什么? 域名解析缓存又名DNS缓存,常见表现名称是TTL:(TimeToLive)生存时间,就是域名解析记录在DNS服务器中的存留有效时间. 当各地的DNS服务器接受到解析请求 ...

  9. 配合JAVA的AJAX使用

    概要 Ajax是“Asynchronous JavaScript and XML”的简称,即异步的JavaScript和XML. readyState属性用来返回当前的请求状态,有五个可选值.分别是0 ...

  10. 【题解】APIO2013机器人

    其实这题前前后后的思考时间加起来应该有两天之久了,dp状态,转移方式等等都还是比较好想,然而左看右看觉得spfa复杂度未免太爆炸……然后选择看了一篇题解,发现在多重优化之下,其实是可以过的…… 首先建 ...