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 ...
随机推荐
- Floyd 无向图模板
这是无向图的 void Floyd() { memset(v, 0x3f, sizeof v); ; i <= n; i++) ; j <= n; j++) v[i][j] = map[i ...
- hdu 2079 选课时间_母函数
题意:需要学够n学分,有k个情况(x学分,y个相同学分的课) 解法:套母函数模板 #include <iostream> #include<cstdio> using name ...
- freemarker声明变量
freemarker声明变量 1.使用assign创建和替换变量 (1)新建声明变量的ftl variable.ftl: <html> <head> <meta http ...
- SPOJ 130 - Rent your airplane and make money(dp+优化)
题意:有n列预定航班,从st时刻开始出发,飞行时间为d,花费为p,且同一时刻不能有两个航班,求最大的花费 对航班的开始时间(或结束时间)按升序排序,从后往前找到对应结束时间所在的航班位置(如按结束时间 ...
- MHA自动切换流程
MHA的全名叫做mysql-master-ha,配置后可以在10-30秒内完成master自动切换,切换过程如下: 1. 检测master的状态,方法是一秒一次“ SELECT 1 As Value” ...
- live555从RTSP服务器读取数据到使用接收到的数据流程分析
本文在linux环境下编译live555工程,并用cgdb调试工具对live555工程中的testProgs目录下的openRTSP的执行过程进行了跟踪分析,直到将从socket端读取视频数据并保存为 ...
- SqlServer判断数据库、表、存储过程、函数是否存在
假设场景是: 需要给一个脚本给客户更新, 这个对象可能存在或不存在 -- 更新存储过程 USE [数据库名] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ...
- ArcGIS添加鹰眼
axMapControl1是主地图 axMapControl2是鹰眼地图 private void axMapControl1_OnExtentUpdated(object sender, IMapC ...
- ScriptManager需要用到的JS
<script type="text/javascript"> Sys.Application.add_load(function() { var form = Sys ...
- 我的美国(北美)计算机CS实习面试经验分享
过去的一年多里,参加了一些面试,虽然面过的公司不多,但都从头一直走到尾.毕竟自己也是花了大量的时间和精力在这一场场的面试里.所以,就絮叨下自己的一些经验,希望能给在美国找实习找工作的同学们提供一点点帮 ...