洛谷 2957 [USACO09OCT]谷仓里的回声Barn Echoes
题目描述
The cows enjoy mooing at the barn because their moos echo back, although sometimes not completely. Bessie, ever the excellent
secretary, has been recording the exact wording of the moo as it goes out and returns. She is curious as to just how much overlap there is.
Given two lines of input (letters from the set a..z, total length in the range 1..80), each of which has the wording of a moo on it, determine the greatest number of characters of overlap between one string and the other. A string is an overlap between two other strings if it is a prefix of one string and a suffix of the other string.
By way of example, consider two moos:
moyooyoxyzooo
yzoooqyasdfljkamo
The last part of the first string overlaps 'yzooo' with the first part of the second string. The last part of the second string
overlaps 'mo' with the first part of the first string. The largest overlap is 'yzooo' whose length is 5.
POINTS: 50
奶牛们非常享受在牛栏中哞叫,因为她们可以听到她们哞声的回音。虽然有时候并不能完全听到完整的回音。Bessie曾经是一个出色的秘书,所以她精确地纪录了所有的哞叫声及其回声。她很好奇到底两个声音的重复部份有多长。
输入两个字符串(长度为1到80个字母),表示两个哞叫声。你要确定最长的重复部份的长度。两个字符串的重复部份指的是同时是一个字符串的前缀和另一个字符串的后缀的字符串。
我们通过一个例子来理解题目。考虑下面的两个哞声:
moyooyoxyzooo
yzoooqyasdfljkamo
第一个串的最后的部份"yzooo"跟第二个串的第一部份重复。第二个串的最后的部份"mo"跟第一个串的第一部份重复。所以"yzooo"跟"mo"都是这2个串的重复部份。其中,"yzooo"比较长,所以最长的重复部份的长度就是5。
输入输出格式
输入格式:
- Lines 1..2: Each line has the text of a moo or its echo
输出格式:
- Line 1: A single line with a single integer that is the length of the longest overlap between the front of one string and end of the other.
输入输出样例
abcxxxxabcxabcd
abcdxabcxxxxabcx
11
说明
'abcxxxxabcx' is a prefix of the first string and a suffix of the second string.
字符串哈希板子
#include<cstdio>
#include<cstring>
#include<algorithm>
#define mod 1777777777
using namespace std;
typedef long long LL;
char s1[],s2[];
int len1,len2;
LL g[];
LL f1[],f2[];
void pre()
{
g[]=;
for(int i=;i<max(len1,len2);i++) g[i]=g[i-]*%mod;
f1[]=s1[]-'a';
for(int i=;i<len1;i++) f1[i]=(f1[i-]*+s1[i]-'a')%mod;
f2[]=s2[]-'a';
for(int i=;i<len2;i++) f2[i]=(f2[i-]*+s2[i]-'a')%mod;
}
LL gethash(LL *f,int l,int r)
{
if(!l) return f[r];
return (f[r]-f[l-]*g[r-l+]%mod+mod)%mod;
}
int main()
{
scanf("%s%s",s1,s2);
len1=strlen(s1);
len2=strlen(s2);
pre();
for(int i=min(len1,len2);i;i--)
{
if(gethash(f1,,i-)==gethash(f2,len2-i,len2-) ) { printf("%d",i); return ; }
if(gethash(f2,,i-)==gethash(f1,len1-i,len1-) ) { printf("%d",i); return ; }
}
printf("");
}
洛谷 2957 [USACO09OCT]谷仓里的回声Barn Echoes的更多相关文章
- 洛谷——P2957 [USACO09OCT]谷仓里的回声Barn Echoes
https://www.luogu.org/problem/show?pid=2957 题目描述 The cows enjoy mooing at the barn because their moo ...
- [luoguP2957] [USACO09OCT]谷仓里的回声Barn Echoes(Hash)
传送门 团队里的hash水题,数据小的不用hash都能过.. 也就是前缀hash,后缀hash,再比较一下就行. ——代码 #include <cstdio> #include <c ...
- [USACO09OCT]谷仓里的回声Barn Echoes(hush、STL)
https://www.luogu.org/problem/P2957 题目描述 The cows enjoy mooing at the barn because their moos echo b ...
- 洛谷——P2958 [USACO09OCT]木瓜的丛林Papaya Jungle
P2958 [USACO09OCT]木瓜的丛林Papaya Jungle 题目描述 Bessie has wandered off the farm into the adjoining farmer ...
- 洛谷 P2958 [USACO09OCT]木瓜的丛林Papaya Jungle
P2958 [USACO09OCT]木瓜的丛林Papaya Jungle 题目描述 Bessie has wandered off the farm into the adjoining farmer ...
- 洛谷 P2376 [USACO09OCT]津贴Allowance 解题报告
P2376 [USACO09OCT]津贴Allowance 题目描述 作为创造产奶纪录的回报,\(Farmer\) \(John\)决定开始每个星期给\(Bessie\)一点零花钱. \(FJ\)有一 ...
- 洛谷—— P1339 [USACO09OCT]热浪Heat Wave
P1339 [USACO09OCT]热浪Heat Wave 题目描述 The good folks in Texas are having a heatwave this summer. Their ...
- 洛谷 P2959 [USACO09OCT]悠闲漫步The Leisurely Stroll
P2959 [USACO09OCT]悠闲漫步The Leisurely Stroll 题目描述 Bessie looks out the barn door at the beautiful spri ...
- 洛谷 P2960 [USACO09OCT]Milkweed的入侵Invasion of the Milkweed
P2960 [USACO09OCT]Milkweed的入侵Invasion of the Milkweed 题目描述 Farmer John has always done his best to k ...
随机推荐
- 【树莓派 Raspberry-Pi 】系统安装及一些必要的配置
上周六刚收到我的小电脑,被无线设置卡住了,文章并非原创,参考了几个朋友的折腾经历,自己整理下备忘,也希望能帮到和我一样在树莓派方面小白的人,也希望可以和更多有这方面兴趣的朋友共同交流 0. 操作系统下 ...
- 基于spec评论“欢迎来怼”团队Alpha版作品
“欢迎来怼”团队的作品是手机版博客园 1.获取此博客园app的方式——二维码 通过扫描二维码的方式下载app,这是当今比较流行的方式,适合广大手机的使用者——青少年的使用习惯. 2.点击图标,进入该a ...
- <Android>日期,时间选择对话框
a) 调用Activity的onCreateDialog()方法创建对话框 b) 分别在OnDateSetListener的onDateSet()方法和OnTimeSet ...
- web前端之路 - 开篇
一 web发展历程 了解事物的历史有助于我们渐进式的从发展的思路清楚了解事物的来龙去脉. 这里有一篇网文写得比较清晰和完整:https://www.tianmaying.com/tutorial/we ...
- 发生dev_queue_xmit的时候,全部都是从ip_finish_output中来的吗
也就是说啊,内核中的收发包的路径,很可能是经理driver_recv --> tcp -->driver_send这样一个过程,是个很长的路径呢...... 从dev_queue_xmit ...
- Redis 学习之集群
该文使用centos6.5 64位 redis3.2.8 一. redis-cluster架构图 集群通信:所有redis节点之间通过PING-PONG机制彼此互联,内部使用二进制鞋子优化传输速度 ...
- SQL SERVER 存储过程中SELECT 返回值如何赋值给变量
今天在处理一个问题时,使用到一个存储过程,是用于更新并获取最新ID的.在使用过程中,需要获取到这个ID并赋值给变量,结果用EXEC @ID = 存储过程的方式获取失败了.具体情况如下: 为了还原整个情 ...
- Delphi处理事件函数中的Sender: TObject代表什么?
下面这个按钮点击事件中,Sender代表谁? procedure Tsomain.ToolButton1Click(Sender: TObject); 是代表事件的拥有者吗? procedure TF ...
- 【Python】ORM框架SQLAlchemy的使用
ORM和SQLAlchemy简介 对象关系映射(Object Relational Mapping,简称ORM),简单的来说,ORM是将数据库中的表与面向对象语言中的类建立了一种对应的关系.然后我们操 ...
- 实用图像处理入门 - 1 - opencv VS2012 环境搭建
标签中的部分 font-family: 华文细黑; font-size: 26px; font-weight: bold; color: #611427; margin-top:40px; } h2 ...