题目网址:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110060#problem/C

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的后缀的最大长度。
 
思路: 利用KMP算法,求出s1的next[]数组,用s1串和s2从s2[0]一直到s2串尾进行匹配,当匹配到s2串尾时的k值就是最大长度。
 

next数组的求解思路:

  最重要的就是如何根据待匹配的模版字符串求出对应每一位的最大相同前后缀的长度。

 1 void makeNext(const char P[],int next[])
2 {
3 int q,k;//q:模版字符串下标;k:最大前后缀长度
4 int m = strlen(P);//模版字符串长度
5 next[0] = 0;//模版字符串的第一个字符的最大前后缀长度为0
6 for (q = 1,k = 0; q < m; ++q)//for循环,从第二个字符开始,依次计算每一个字符对应的next值
7 {
8 while(k > 0 && P[q] != P[k])//递归的求出P[0]···P[q]的最大的相同的前后缀长度k
9 k = next[k-1]; //不理解没关系看下面的分析,这个while循环是整段代码的精髓所在,确实不好理解
10 if (P[q] == P[k])//如果相等,那么最大相同前后缀长度加1
11 {
12 k++;
13 }
14 next[q] = k;
15 }
16 } 

  while循环所做的工作:

  1.   已知前一步计算时最大相同的前后缀长度为k(k>0),即P[0]···P[k-1];
  2.   此时比较第k项P[k]与P[q],如图1所示
  3.   如果P[K]等于P[q],那么很简单跳出while循环;
  4.   如果不等呢?那么我们应该利用已经得到的next[0]···next[k-1]来求P[0]···P[k-1]这个子串中最大相同前后缀,可能有同学要问了——为什么要求P[0]···P[k-1]的       最大相同前后缀呢???是啊!为什么呢? 原因在于P[k]已经和P[q]失配了,而且P[q-k] ··· P[q-1]又与P[0] ···P[k-1]相同,看来P[0]···P[k-1]这么长的子串是用不        了了,那么我要找个同样也是P[0]打头、P[k-1]结尾的子串即P[0]···P[j-1](j==next[k-1]),看看它的下一项P[j]是否能和P[q]匹配。如图2所示
 
本题代码:
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
char s1[],s2[];
int nex[]; void nnext(int len)
{ ///求s1串的nex[]数组;
int k=;
nex[]=;
for(int i=;i<len;i++)
{ ///求出nex[]的每一个值;
///nex[i]表示以s1[i]结尾的字符串能与s1[]前缀匹配的最大长度;
while(k>&&s1[k]!=s1[i])
k=nex[k-];
if(s1[k]==s1[i])
k++;
nex[i]=k;
}
} int main()
{
int k,len1,len2;
while(scanf("%s%s",s1,s2)!=EOF)
{
k=;
len1=strlen(s1);
len2=strlen(s2);
nnext(len1);
for(int i=;i<len2;i++)
{
while(k>&&s1[k]!=s2[i])
k=nex[k-];
if(s1[k]==s2[i])
k++;
if(i==len2-) break;
if(k>len1-)
{
k=nex[k-];
}
}
if(k)
{
s1[k]='\0';
printf("%s %d\n",s1,k);
}
else
printf("0\n");
}
return ;
}

KMP--Simpsons’ Hidden Talents的更多相关文章

  1. HDU 2594 (简单KMP) Simpsons’ Hidden Talents

    题意: 有两个字符串,找一个最长子串是的该串既是第一个字的前缀,又是第二个串的后缀. 分析: 把两个串并起来然后在中间加一个无关字符,求next数组即可. #include <cstdio> ...

  2. hdu 2594 Simpsons’ Hidden Talents KMP

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

  3. HDU 2594 Simpsons’ Hidden Talents(KMP的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/Ot ...

  5. hdu 2594 Simpsons’ Hidden Talents KMP应用

    Simpsons’ Hidden Talents Problem Description Write a program that, when given strings s1 and s2, fin ...

  6. hdoj 2594 Simpsons’ Hidden Talents 【KMP】【求串的最长公共前缀后缀】

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

  7. HDU2594 Simpsons’ Hidden Talents 【KMP】

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

  8. (KMP)Simpsons’ Hidden Talents -- hdu -- 2594

    http://acm.hdu.edu.cn/showproblem.php?pid=2594 Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Ja ...

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

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

  10. hdu 2594 Simpsons’ Hidden Talents(KMP入门)

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

随机推荐

  1. Spring3系列7- 自动扫描组件或Bean

    Spring3系列7- 自动扫描组件或Bean 一.      Spring Auto Scanning Components —— 自动扫描组件 1.      Declares Component ...

  2. bash + script

    shell "" 保留$,`,\, 换行含义,‘’保留字面值 $(), ``用于命令替换 算术扩展如 $[1+1] for循环: for Host in host1, host2, ...

  3. node.js中log4js的使用

    以前用过forever进程守护的日志记录到指定文件,但是只能保存到一个文件中不能分片,这样到只日志文件越来越大, forever start -s -l ./forever.log app.js -l ...

  4. HDU 3999 The order of a Tree

    The order of a Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  5. 消息中间件Notify和MetaQ-阿里中间件

    3.1.Notify Notify是淘宝自主研发的一套消息服务引擎,是支撑双11最为核心的系统之一,在淘宝和支付宝的核心交易场景中都有大量使用.消息系统的核心作用就是三点:解耦,异步和并行.下面让我以 ...

  6. 后端码农谈前端(CSS篇)第一课:CSS概述

    一.从扮演浏览器开始 扮演浏览器是Head First图书中很有意义的一个环节.可作者忘记了告诉我们扮演浏览器的台本.我们从这里开始. 上图是webkit内核渲染html和css的流程图.从该图我们可 ...

  7. NPM使用详解(下)

    NPM使用详解(下) *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !impo ...

  8. 没有Google的日子很难过...But you can try...

    作为开发人员,经常会通过Google查询一些资料(别问我为什么不用百度,当你输入关键字,然后给你提示没有查询到结果,或者一页全是垃圾资料的时候你就知道了).但是,N个月前,Google不好使了,同时N ...

  9. ef 更新导航属性

    总之,要让所有的东西,都被DbContext管理状态 1.查出来,改了,再提交 2.new 出来,attach,再改,再提交 以上两种都较好理解 3.new出来,改了,再attach,在改状态,再提交 ...

  10. suricata抓包方式之一 AF_PACKET

    1.前言 linux提供了原始套接字RAW_SOCKET,可以抓取数据链路层的报文.这样可以对报文进行深入分析.今天介绍一下AF_PACKET的用法,分为两种方式.第一种方法是通过套接字,打开指定的网 ...