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 ...
随机推荐
- 谋哥:研究App排行榜浮出的神器
昨天发的<App排行榜的秘密>到头条网,阅读量到2万,踩的比顶的多几倍.原因是由于我使用360市场的数据来分析,而且这帮喷子根本不看你分析数据背后的意义,反正看到自己不喜欢的比方" ...
- Oracle SQL函数之数学函数
Oracle SQL函数之数学函数 ABS(x) [功能]返回x的绝对值 [参数]x,数字型表达式 [返回]数字 SQL> SELECT ABS(),ABS(-) FROM DUAL; ABS( ...
- 伪元素::before和::after
有时候我们的页面里面有不少其他网站的名字,而且还要求网站名后面还要有网站的链接,类似这样:百度(http://www.baidu.com).这个时候如果网站多的话写起来就很麻烦了 <a href ...
- SERVERPROPERTY方法说明
SERVERPROPERTY 返回有关服务器实例的属性信息. 语法 SERVERPROPERTY ( propertyname ) 参数 propertyname 是包含要返回的服务器属性信息的表达式 ...
- jeasyui制作计划-搭建php运行环境
1.参考链接: http://jingyan.baidu.com/article/154b46315242b328ca8f4101.html http://jingyan.baidu.com/arti ...
- 杭电OJ—— 1084 What Is Your Grade?
What Is Your Grade? Problem Description “Point, point, life of student!” This is a ballad(歌谣)well kn ...
- eclipse +cvs 的基本使用方法(一)
很多时候我们在做项目开发时,会用到cvs,现在我给大家介绍一下关于eclipse下怎么使用cvs管理功能,eclipse本身是自带cvs的,我们只要简单设置一下让它连接到cvs服务器上. 看下图 ...
- Plugin is too old, please update to a more recent version, or set ANDROID_DAILY_OVERRIDE environment variable to “*****”
Plugin is too old, please update to a more recent version, or set ANDROID_DAILY_OVERRIDE environment ...
- MySQL高效获取记录总数
通常mysql获取查询记录总数我们使用如下语句: SELECT COUNT(*) FROM users WHERE k='avs'; 或:SELECT id FROM goods WHERE k=' ...
- python核心编程-第三章-习题
1.这是python的语言特性,python先创建对象,在给变量赋值时,不需要定义变量的名称和类型,它实际是用变量引用对象.变量类型在给变量赋值时自动声明 2.原因类似变量无须声明类型 3.pytho ...