HDU2594——Simpsons’ Hidden Talents
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.
The lengths of s1 and s2 will be at most 50000.
homer
riemann
marjorie
rie 3
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<ctype.h>
#include<algorithm>
#include<stack>
#include<queue>
#include<set>
#include<math.h>
#include<vector>
#include<map>
#include<deque>
#include<list>
using namespace std;
char a[50009],b[50009];
int next[50009];
int m,n;
int i,j;
void getnext()
{
int i=0;
int j=-1;
next[0]=-1;
while(i<n)
{
if(j==-1||b[i]==b[j])
{
i++;
j++;
if(b[i]!=b[j])
next[i]=j;
else
next[i]=next[j];
}
else
j=next[j];
}
}
int kmp()
{
int i=0;
int j=0;
while(i<m)
{
if(j==-1||a[i]==b[j])
{
i++;
j++;
}
else
{
j=next[j];
}
}
return j;
}
int main()
{
while(gets(b))
{
gets(a);
m=strlen(a);
n=strlen(b);
getnext();
int p=kmp();
for(int i=0; i<p; i++)
printf("%c",b[i]);
if(p)
printf(" ");
printf("%d\n",p);
}
return 0;
}
HDU2594——Simpsons’ Hidden Talents的更多相关文章
- HDU2594 Simpsons’ Hidden Talents —— KMP next数组
题目链接:https://vjudge.net/problem/HDU-2594 Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Oth ...
- hdu2594 Simpsons’ Hidden Talents kmp
Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- hdu2594 Simpsons' Hidden Talents【next数组应用】
Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
- HDU2594 Simpsons’ Hidden Talents 【KMP】
Simpsons' Hidden Talents Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
- hdu2594 Simpsons’ Hidden Talents LCS--扩展KMP
Homer: Marge, I just figured out a way to discover some of the talents we weren’t aware we had.Marge ...
- kuangbin专题十六 KMP&&扩展KMP HDU2594 Simpsons’ Hidden Talents
Homer: Marge, I just figured out a way to discover some of the talents we weren’t aware we had. Marg ...
- HDU2594 Simpsons’ Hidden Talents 字符串哈希
最近在学习字符串的知识,在字符串上我跟大一的时候是没什么区别的,所以恶补了很多基础的算法,今天补了一下字符串哈希,看的是大一新生的课件学的,以前觉得字符串哈希无非就是跟普通的哈希没什么区别,倒也没觉得 ...
- hdu2594 Simpsons’ Hidden Talents
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2594 思路: 其实就是求相同的最长前缀与最长后缀 KMP算法的简单应用: 假设输入的两个字符串分别是s ...
- hdu 2594 Simpsons’ Hidden Talents KMP
Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
随机推荐
- 【LeetCode练习题】Minimum Path Sum
Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to b ...
- DWR常用<init-param>参数
1 安全参数 allowGetForSafariButMakeForgeryEasier 开始版本:2.0 默认值:false 描述:设置成true使DWR工作在Safari 1.x , 会稍微降低安 ...
- [Spring boot] web应用返回jsp页面
同事创建了一个spring boot项目,上传到svn.需要我来写个页面.下载下来后,始终无法实现在Controller方法中配置直接返回jsp页面. 郁闷了一下午,终于搞定了问题.在此记录一下. 目 ...
- ubuntu下安装xlrd模块,Mysqldb模块
Python中安装xlrd模块 1.从 https://pypi.python.org/pypi/xlrd 下载压缩包 2.解压所下载的压缩包 3.CD到解压文件夹,运行 sudo python s ...
- Android实战技巧之十九:android studio导出jar包(Module)并获得手机信息
AS中并没有独立的Module 工程,可是能够在普通的Project中增加Module.所谓的Module就是我们通常所指的模块化的一个单元.并经常以jar包的形式存在.以下以一个获取手机信息的样例演 ...
- java学习笔记day02
1.如何定义一个功能,并通过函数(方法)体现出来呢? 1)明确改功能运算后的结果.确定函数的返回值类型. 2)明确在实现该功能的过程中是否有未知内容参与运算.确定参数列表. 2.函数重载:在同一个 ...
- Android服务端本地窗口FramebufferNativeWindow
Android窗口系统 我们知道Android系统采用OpenGL来绘制3D图形,OpenGL ES提供了本地窗口(NativeWindow)的概念,无论是在Android平台中还是其他平台中,只要实 ...
- Audio-支持多个音频文件格式
通过使用 audio 元素或对象支持多个音频格式,你可以将更多的听众从多个浏览器吸引到你的网页上. 使用源元素指定多个音频格式 在将 HTML5 audio 元素添加到代码时,可以指定一条在浏览器不支 ...
- /boot磁盘空间不足,没盘扩展,只好删除旧内核了
[xiejdm@localhost ~]$ uname -r -.el7.x86_64 [xiejdm@localhost ~]$ sudo rpm -qa | grep kernel kernel- ...
- UI事件之load
load事件属于CSS3规范中的UI事件,load事件处理程序在页面元素和资源(html/script/link/img等)全部加载完成后在window上触发,或在img元素加载完成后再img元素上触 ...