HDU 2594 Simpsons’ Hidden Talents (字符串-KMP)

Simpsons’ Hidden Talents


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

Source
HDU 2010-05 Programming Contest


题目大意:

给你字符串s1和s2,问你s1的前缀和s2的后缀最长相同的串多长?


解题思路:

牢记KMP Next数组的含义,将s1和s2拼接在一起,next[len]也就是最终答案。


解题代码:

#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <string>
#include <sstream>
#include <cmath>
#include <algorithm>
using namespace std;const int maxn=51000;
int next[maxn*2];
char word[maxn*2],s1[maxn],s2[maxn];void kmp(){int n=strlen(word);for(int i=0;i<=n;i++) next[i]=0;for(int i=1;i<n;i++){int j=i;while(j>0){j=next[j];if(word[j]==word[i]){next[i+1]=j+1;break;}}}
}int main(){while(scanf("%s%s",s1,s2)!=EOF){stringstream ss;ss<<s1<<s2;ss>>word;kmp();int len=strlen(word),ans=min(strlen(s1),strlen(s2));if(next[len]<=strlen(s1) && next[len]<=strlen(s2) ){ans=next[len];}if(ans==0) printf("0\n");else{for(int i=0;i<ans;i++) printf("%c",s1[i]);printf(" %d\n",ans);}}return 0;
}




转载于:https://www.cnblogs.com/toyking/p/3893163.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/407473.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

微信小程序几种常用弹窗提示

第一种&#xff1a;弹出提示框&#xff0c;可以选择确定或者取消。 代码&#xff1a;wx.showModal({title: 提示,content: 这是一个模态弹窗,success: function (res) {if (res.confirm) {//这里是点击了确定以后console.log(用户点击确定)} else {//这里是点击了取消以后conso…

[bash]删除文件中含特定字符串的行

]删除文件中含特定字符串的行[bash]: sed -e /abc/d a.txt // 删除a.txt中含"abc"的行&#xff0c;但不改变a.txt文件本身&#xff0c;操作之后的结果在终端显示 sed -e /abc/d a.txt > a.log // 删除a.txt中含"abc"的行&#xff0c;将操作之后的…

CSS之文档视图(DocumentView)和元素视图(ElementView)方法

1.elementFromPoint() 返回给定坐标处的元素。是个在目前而言&#xff0c;兼容性不错的方法 将给定位置处(100,100)的元素的字体颜色设置为红色 2.getBoundingClientRect() 返回的是返回的是一个对象&#xff0c;包含 top, left, right, 和 bottom四个属性值&#xff0c;大小…

谈CRM产品设计的指导思想

客户关系管理&#xff08;CRM&#xff09;其本意强调的是对客户“关系”进行有效管理&#xff0c;从而达到维持较高的客户占有率&#xff08;customershare&#xff09;的目的。所谓关系&#xff0c;是指两个事物之间其中的一方对另一方的行为方式以及感受状态。所以&#xff0…

try catch finally

<!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8" /><title>try catch finally</title><script>// 异常最大的特征,就是一旦出现异常,后面的代码将不会再执行// 那为了保证后面的代码在出现异常之后…

Java笔记-IO流的运用

--如果朋友您想转载本文章请注明转载地址"http://www.cnblogs.com/XHJT/p/3877386.html "谢谢-- 1.InputStream和System.in&#xff08;Scanner&#xff09; InputStream 输出流以字节为单位来获取数据&#xff0c;且需要复杂的判断并创建字节数组作为缓冲 另外字节转…

[Bash]kill指定的进程名

通过bash来kill指定的进程名&#xff0c;bash文件名为&#xff1a;/home/zcm/bin/d.sh&#xff0c;内容如下&#xff1a; #!/bin/shif [ "$1" "" ]; thenecho "Usage: sh $0 <processname>"exit 0fi#s1ps -ef|grep $1|grep -v grep|awk …

CSS之元素视图属性

1.clientLeft和clientTop 返回的是内容区域的左上角相对于整个元素左上角的位置&#xff08;包括边框&#xff09; 2.clientWidth和clientHeight 表示内容区域的高度和宽度&#xff0c;包括padding大小&#xff0c;但是不包括边框和滚动条 3.offsetLeft和offsetTop offsetLe…

阿里巴巴指东打西,PC之后卖盒饭?

这两天&#xff0c;看到了数篇关于阿里巴巴的报道&#xff0c;核心内容都是阿里巴巴联手英特尔&#xff0c;发起主题为“助力中国”的电子商务推动计划&#xff0c;并将推出面向中小企业电子商务专用电脑的消息。从报道看&#xff0c;阿里巴巴将同英特尔合作&#xff0c;推出贴…

SQL-MSSQL-CODE大全

SqlServer数据库语句大全 /*********************************************************/ 目录清单CONTEXT LIST /*********************************************************/ 1.数据库DataBase 1.1数据库建立/删除create/drop database 1.2数据库备份与恢复backup/restore d…

JS中的bind()方法

Function.prototype.bind()方法 bind()方法主要就是将函数绑定到某个对象&#xff0c;bind()会创建一个函数&#xff0c;函数体内的this对象的值会被绑定到传入bind()第一个参数的值&#xff0c;例如&#xff0c;f.bind(obj)&#xff0c;实际上可以理解为obj.f()&#xff0c;这…

Vue之Vue.set动态新增对象属性

当我们给一个比如props中&#xff0c;或者data中被观测的对象添加一个新的属性的时候&#xff0c;不能直接添加&#xff0c;必须使用Vue.set方法 Vue.set方法用来新增对象的属性。如果要增加属性的对象是响应式的&#xff0c;那该方法可以确保属性被创建后也是响应式的&#x…

[精华] VI高级命令集锦

鉴于大家在使用VI 的时候有一定的陌生&#xff0c;在这里借花献佛&#xff0c;从兄弟版[SHELL]中整理出以下关于VI编程的高级技巧&#xff0c;希望对大家学习UNIX有所帮助&#xff0c;并希望大家能结合使用HPUX的心得体会对这些经验进行进一步的扩展。1.交换两个字符位置xp2.上…

C语言程序设计 文件操作函数

文件操作函数 C语言 (FILE fputc fgetc fputs fgets fscanf fprintf)在ANSI C中&#xff0c;对文件的操作分为两种方式&#xff0c;即流式文件操作和I/O文件操作&#xff0c;下面就分别介绍之。 一、流式文件操作 这种方式的文件操作有一个重要的结构FILE&#xff0c;FILE在std…

Vue之过渡动画

v-enter: 定义过渡开始时元素的状态。在元素被插入时生效&#xff0c;在下一个帧(下一帧也就是v-enter-active)移除(实际就是过渡将要开始之前&#xff0c;元素的状态) v-enter-active: 定义”进入过渡”的结束状态。在元素被插入时生效&#xff0c;在 transition/animation 完…

HTTP Live Streaming直播(iOS直播)技术分析与实现(转)

HTTP Live Streaming直播(iOS直播)技术分析与实现 转载于:https://www.cnblogs.com/chen-jt/p/3885223.html