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
 
 
分析:
两个字符串s1和s2, 求出是s1的前缀并且是s2的后缀的最长的字符串。
 
 
#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的更多相关文章

  1. HDU2594 Simpsons’ Hidden Talents —— KMP next数组

    题目链接:https://vjudge.net/problem/HDU-2594 Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Oth ...

  2. hdu2594 Simpsons’ Hidden Talents kmp

    Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...

  3. hdu2594 Simpsons' Hidden Talents【next数组应用】

    Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...

  4. HDU2594 Simpsons’ Hidden Talents 【KMP】

    Simpsons' Hidden Talents Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...

  5. 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 ...

  6. 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 ...

  7. HDU2594 Simpsons’ Hidden Talents 字符串哈希

    最近在学习字符串的知识,在字符串上我跟大一的时候是没什么区别的,所以恶补了很多基础的算法,今天补了一下字符串哈希,看的是大一新生的课件学的,以前觉得字符串哈希无非就是跟普通的哈希没什么区别,倒也没觉得 ...

  8. hdu2594 Simpsons’ Hidden Talents

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2594 思路: 其实就是求相同的最长前缀与最长后缀 KMP算法的简单应用: 假设输入的两个字符串分别是s ...

  9. hdu 2594 Simpsons’ Hidden Talents KMP

    Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...

随机推荐

  1. Floyd 无向图模板

    这是无向图的 void Floyd() { memset(v, 0x3f, sizeof v); ; i <= n; i++) ; j <= n; j++) v[i][j] = map[i ...

  2. hdu 2079 选课时间_母函数

    题意:需要学够n学分,有k个情况(x学分,y个相同学分的课) 解法:套母函数模板 #include <iostream> #include<cstdio> using name ...

  3. freemarker声明变量

    freemarker声明变量 1.使用assign创建和替换变量 (1)新建声明变量的ftl variable.ftl: <html> <head> <meta http ...

  4. SPOJ 130 - Rent your airplane and make money(dp+优化)

    题意:有n列预定航班,从st时刻开始出发,飞行时间为d,花费为p,且同一时刻不能有两个航班,求最大的花费 对航班的开始时间(或结束时间)按升序排序,从后往前找到对应结束时间所在的航班位置(如按结束时间 ...

  5. MHA自动切换流程

    MHA的全名叫做mysql-master-ha,配置后可以在10-30秒内完成master自动切换,切换过程如下: 1. 检测master的状态,方法是一秒一次“ SELECT 1 As Value” ...

  6. live555从RTSP服务器读取数据到使用接收到的数据流程分析

    本文在linux环境下编译live555工程,并用cgdb调试工具对live555工程中的testProgs目录下的openRTSP的执行过程进行了跟踪分析,直到将从socket端读取视频数据并保存为 ...

  7. SqlServer判断数据库、表、存储过程、函数是否存在

    假设场景是: 需要给一个脚本给客户更新, 这个对象可能存在或不存在 -- 更新存储过程 USE [数据库名] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ...

  8. ArcGIS添加鹰眼

    axMapControl1是主地图 axMapControl2是鹰眼地图 private void axMapControl1_OnExtentUpdated(object sender, IMapC ...

  9. ScriptManager需要用到的JS

    <script type="text/javascript"> Sys.Application.add_load(function() { var form = Sys ...

  10. 我的美国(北美)计算机CS实习面试经验分享

    过去的一年多里,参加了一些面试,虽然面过的公司不多,但都从头一直走到尾.毕竟自己也是花了大量的时间和精力在这一场场的面试里.所以,就絮叨下自己的一些经验,希望能给在美国找实习找工作的同学们提供一点点帮 ...