题目网址: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. iOS开发——项目实战总结&数据持久化分析

    数据持久化分析 plist文件(属性列表) preference(偏好设置) NSKeyedArchiver(归档) SQLite 3 CoreData 当存储大块数据时你会怎么做? 你有很多选择,比 ...

  2. Python中的模块与包

    标准库的安装路径 在import模块的时候,python是通过系统路径找到这些模块的,我们可以将这些路径打印出来: >>> pprint.pprint(sys.path) ['', ...

  3. server 2012 IIS 启用.NET 4.5

    server2012 自带.NET 4.5 不过IIS服务器只有2.0 和 4.0 所以我们需要手动配置 1.服务器-添加角色和功能 2.依次下一步,选择到服务器角色 -点开web服务器(IIS)-应 ...

  4. 编写并发程序 Inversion

    做完了 scala parallel 课程作业后,觉得 scala 写并发程序的便捷性是 java 永远都追不上的.scala 的Future 和 Promise,java 里 Future 和 Co ...

  5. [转]Entity Framework vs. LINQ to SQL

    Entity Framework和LINQ to SQL到底有什么区别?这是一个很常见的问题.下面的表中简要罗列了两种技术的主要区别. LINQ to SQL Entity Framework 复杂度 ...

  6. Android-NDK编译:cocos2d-x(三) eclipse 导入工程

    NDK 编译后,用eclipse导入cocos2d-x工程 菜单[File]-->[New]-->[Project] ,弹出New Project 对话框 窗口下方 选 [Android] ...

  7. [转]使用Cadence ADE + Spectre做Montel Carlo仿真

    1. 工艺模型的选择.以TSMC 180nm工艺为例,1.8V Normal devices 有TT,SS,FF,SF,FS共5种工艺Corner及Montel Carlo(MC)共6种可选用工艺角. ...

  8. uniGUI试用笔记(十三)调用WebService

    今天尝试用uniGUI做Web服务器,调用应用服务器的WebService,遇到些问题记录下来备忘. 1.对WebService的调用同一般App程序,只是注意如果WebService的执行时间较长, ...

  9. 在redhat上搭建redmine

    搞个项目管理的东西 找了下还是redmine比较合适,行动action: 1.ruby 额 是的你没有看错 需要先安装一个ruby的环境.话说这个安装起来很是纠结,本来想用yum 结果咩有成功,于是乎 ...

  10. SmartJS 第一期(0.1)发布 - AOP三剑客

    隔了好久才终于又发布了一点东西,SmartJS是最近才开始搞的一个开源js库,目的是做一些比较有特点的事情(smartjs暂时也是依赖于jquery). SmartJS的内容规划比较多,也无法在短时间 ...