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.

题意:求两个字符串的最长公共子串

扩展KMP裸题

 #include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; const int maxn=5e4+; char s[maxn],t[maxn];
int nxt[maxn],ext[maxn]; void EKMP(char s[],char t[],int lens,int lent){
int i,j,p,l,k;
nxt[]=lent;j=;
while(j+<lent&&t[j]==t[j+])j++;
nxt[]=j;
k=;
for(i=;i<lent;i++){
p=nxt[k]+k-;
l=nxt[i-k];
if(i+l<p+)nxt[i]=l;
else{
j=max(,p-i+);
while(i+j<lent&&t[i+j]==t[j])j++;
nxt[i]=j;
k=i;
}
} j=;
while(j<lens&&j<lent&&s[j]==t[j])j++;
ext[]=j;k=;
for(i=;i<lens;i++){
p=ext[k]+k-;
l=nxt[i-k];
if(l+i<p+)ext[i]=l;
else{
j=max(,p-i+);
while(i+j<lens&&j<lent&&s[i+j]==t[j])j++;
ext[i]=j;
k=i;
}
}
} int main(){
while(scanf("%s%s",s,t)!=EOF){
EKMP(t,s,strlen(t),strlen(s));
int l=strlen(t);
int i;
for(i=;i<l;++i){
if(ext[i]==l-i){
printf("%s %d\n",t+i,l-i);
break;
}
}
if(i==l)printf("0\n");
}
return ;
}

hdu2594 Simpsons’ Hidden Talents LCS--扩展KMP的更多相关文章

  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 【KMP】

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

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

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

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

  6. HDU2594——Simpsons’ Hidden Talents

    Problem Description Homer: Marge, I just figured out a way to discover some of the talents we weren’ ...

  7. hdu2594 Simpsons’ Hidden Talents

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

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

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

  9. hdu 2594 Simpsons’ Hidden Talents KMP

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

随机推荐

  1. Vue笔记:使用 vuex 管理应用状态

    如果你在使用 vue.js , 那么我想你可能会对 vue 组件之间的通信感到崩溃 . 我在使用基于 vue.js 2.0 的UI框架 ElementUI 开发网站的时候 , 就遇到了这种问题 : 一 ...

  2. Neo4J 教程

    好文转载: W3C: https://www.w3cschool.cn/neo4j/neo4j_cypher_api_example.html neo4j图数据库入门: http://blog.csd ...

  3. post请求返回 读取 HTML 表单 URL 编码的数据流时出错

    <httpRuntime maxRequestLength="1048576" executionTimeout="3600" /> 网站配置 限制 ...

  4. 1.3用socketserver创建服务器

    socket服务器代码 # -*- coding: utf-8 -*-import socketserver,time myHost = '' myPort = 50007 def now(): #返 ...

  5. Centos7扩展存储空间

    1.在关机状态下,在Vm里面设置系统大小到需要的数值 :2.使用root权限登陆linux,df -h查看系统情况: [root@cratedb-hk-01 ~]# df -h 文件系统 容量 已用 ...

  6. [PyImageSearch] Ubuntu16.04 使用深度学习和OpenCV实现物体检测

    上一篇博文中讲到如何用OpenCV实现物体分类,但是接下来这篇博文将会告诉你图片中物体的位置具体在哪里. 我们将会知道如何使用OpenCV‘s的dnn模块去加载一个预训练的物体检测网络,它能使得我们将 ...

  7. Docker常用命令学习

    sudo service docker startsudo docker run hello-worlddocker stats --helpdocker run -d -P training/web ...

  8. tomcat启动失败,提示信息:Unable to ping server at localhost:1099

    jdk1.7+maven9.0.0开启服务器时,提示Unable to ping server at localhost:1099 然后换成tomcat8.5.1就成功开启服务器

  9. C++中数组定义及初始化

    一.一维数组 静态 int array[100]; 定义了数组array,并未对数组进行初始化 静态 int array[100] = {1,2}: 定义并初始化了数组array 动态 int* ar ...

  10. Python 基础list 增删改查 解析

    一 增加     1 append 增加到最后一个列表      li = [ 1,3,4 ,'wang',]li.append('wangyan')print(li)[1, 3, 4, 'wang' ...