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: 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的更多相关文章
- 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 【KMP】
Simpsons' Hidden Talents Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
- hdu2594 Simpsons' Hidden Talents【next数组应用】
Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
- 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
Problem Description Homer: Marge, I just figured out a way to discover some of the talents we weren’ ...
- hdu2594 Simpsons’ Hidden Talents
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2594 思路: 其实就是求相同的最长前缀与最长后缀 KMP算法的简单应用: 假设输入的两个字符串分别是s ...
- HDU2594 Simpsons’ Hidden Talents 字符串哈希
最近在学习字符串的知识,在字符串上我跟大一的时候是没什么区别的,所以恶补了很多基础的算法,今天补了一下字符串哈希,看的是大一新生的课件学的,以前觉得字符串哈希无非就是跟普通的哈希没什么区别,倒也没觉得 ...
- hdu 2594 Simpsons’ Hidden Talents KMP
Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
随机推荐
- js 实现class作为选择器
<!doctype html> <html> <head> <meta charset="utf-8"> <meta name ...
- img2html实现将图片转换成网页
简单介绍img2html的用法,安装就不用说了pip.这个包现只支持python2,支持python的话需改下源码这几个部分: 加注释的是修改的地方 #!/usr/bin/env python # e ...
- 用老毛桃U盘安装:[3]Ghost版Win7系统
用老毛桃自动安装Ghost版Win7的步骤: 1,到网上先下载Ghost版Win7映像文件到硬盘,我放到的是U盘,盘符为Z,如果你愿意,可直接放到硬盘即可,放到硬盘安装速度会快一点. 2,把制作好的老 ...
- 跑caffe过程中的备忘
1*1卷积比如一张500*500且厚度depth为100的图片在20个filter上做1*1卷积,那么结果大小为500*500*20 只有池化改变图片的大小 一个大的全连接层可以理解为一个神经网络,这 ...
- 《Python》re模块补充、异常处理
一.re模块 1.match方法 import re # match 验证用户输入的内容 ret = re.match('\d+', 'hhoi2342ho12ioh11') print(ret) # ...
- :适配器模式:Adapter
#ifndef __ADAPTER_H__ #define __ADAPTER_H__ #include <iostream> using namespace std; class Duc ...
- Cracking The Coding Interview 4.1
//Implement a function to check if a tree is balanced. For the purposes of this question, a balanced ...
- 6.3 C++修改字符串
参考:http://www.weixueyuan.net/view/6392.html 总结: string字符串同样可以像字符串数组那样按照下标逐一访问字符串中的每一个字符,string字符串的起始 ...
- 第三节 java 数组(循环遍历、获取数组的最值(最大值和最小值)、选择排序、冒泡排序、练习控制台输出大写的A)
获取数组的最值(最大值和最小值) 思路: 1.获取最值需要进行比较,每一次比较都会有一个较大的值,因为该 值不确定,需要一个变量进行临储. 2.让数组中的每一个元素都和这个变量中的值进行比较,如果大于 ...
- 禁止textarea拉伸
添加css属性: style="resize:none" ;