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. 虚拟现实-VR-UE4-LEAP-Motion手势识别

    点击打开链接今天到手一个新东西,LEAP手势识别仪. 关于LEAP Leap是一家面向PC以及Mac的体感控制器制造公司. 具体信息百度百科http://baike.baidu.com/link?ur ...

  2. jmeter完成CAS登录,并获取token(原创)

    思路: 1.系统完成CAS登录需要验证用户名/密码,以及动态授权参数 2.先通过指定url用正则提取出动态授权参数 3.完成登录需要cookie,需用正则提取出对应的cookie,已完成参数化的自动登 ...

  3. java 读取配置文件 与更新

    笔记 public class Config { private static Properties props = new Properties(); static File configFile ...

  4. TensorFlow 同时调用多个预训练好的模型

    在某些任务中,我们需要针对不同的情况训练多个不同的神经网络模型,这时候,在测试阶段,我们就需要调用多个预训练好的模型分别来进行预测. 调用单个预训练好的模型请点击此处 弄明白了如何调用单个模型,其实调 ...

  5. hibernate和mybatis的之CRUD封装差别

    hibernate和mybatis的之CRUD封装差别 以下讲的是基于MVC三层架构. 由于设计架构的差别,hibernate在实际编程中可以把基础的CRUD封装,比如BaseDao类.其它类只要去继 ...

  6. 前端JQuery中获取一个div下的多个id值

    获取所有的Id值,方法是通过div.class获取全局的值,然后再提取具体的Id值 方法一:用for循环,因为$("div.class")获取的是一个数组,通过循环读取出数组中的每 ...

  7. 201621123033 《Java程序设计》第13周学习总结

    1. 本周学习总结 以你喜欢的方式(思维导图.OneNote或其他)归纳总结多网络相关内容. 2. 为你的系统增加网络功能(购物车.图书馆管理.斗地主等)-分组完成 为了让你的系统可以被多个用户通过网 ...

  8. SSH答疑解惑系列(二)——java.lang.reflect.InvocationTargetException异常

    在项目中遇到了invocationTargetException的问题,在这里跟大家分享一下. 报错信息如下: 使用反射时,比如执行invoke方法,如果被反射执行的方法体抛出了Exception,这 ...

  9. vue2.0 vue-cli项目中路由之间的参数传递

    1.首先配置路由, import Vue from 'vue' import Router from 'vue-router' Vue.use(Router) export default new R ...

  10. 算法(3)Rotate Array

    题目:将一个n个元素的数组右移k位,比如n=7,k=3,对数组[1,2,3,4,5,6,7]作如下旋转[5,6,7,1,2,3,4] 思路:[5,6,7,1,2,3,4],不知大家看出来了没有呢,两次 ...