题目描述

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.

输入输出样例

输入样例#1:

abcxxxxabcxabcd
abcdxabcxxxxabcx
输出样例#1:

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的更多相关文章

  1. 洛谷——P2957 [USACO09OCT]谷仓里的回声Barn Echoes

    https://www.luogu.org/problem/show?pid=2957 题目描述 The cows enjoy mooing at the barn because their moo ...

  2. [luoguP2957] [USACO09OCT]谷仓里的回声Barn Echoes(Hash)

    传送门 团队里的hash水题,数据小的不用hash都能过.. 也就是前缀hash,后缀hash,再比较一下就行. ——代码 #include <cstdio> #include <c ...

  3. [USACO09OCT]谷仓里的回声Barn Echoes(hush、STL)

    https://www.luogu.org/problem/P2957 题目描述 The cows enjoy mooing at the barn because their moos echo b ...

  4. 洛谷——P2958 [USACO09OCT]木瓜的丛林Papaya Jungle

    P2958 [USACO09OCT]木瓜的丛林Papaya Jungle 题目描述 Bessie has wandered off the farm into the adjoining farmer ...

  5. 洛谷 P2958 [USACO09OCT]木瓜的丛林Papaya Jungle

    P2958 [USACO09OCT]木瓜的丛林Papaya Jungle 题目描述 Bessie has wandered off the farm into the adjoining farmer ...

  6. 洛谷 P2376 [USACO09OCT]津贴Allowance 解题报告

    P2376 [USACO09OCT]津贴Allowance 题目描述 作为创造产奶纪录的回报,\(Farmer\) \(John\)决定开始每个星期给\(Bessie\)一点零花钱. \(FJ\)有一 ...

  7. 洛谷—— P1339 [USACO09OCT]热浪Heat Wave

    P1339 [USACO09OCT]热浪Heat Wave 题目描述 The good folks in Texas are having a heatwave this summer. Their ...

  8. 洛谷 P2959 [USACO09OCT]悠闲漫步The Leisurely Stroll

    P2959 [USACO09OCT]悠闲漫步The Leisurely Stroll 题目描述 Bessie looks out the barn door at the beautiful spri ...

  9. 洛谷 P2960 [USACO09OCT]Milkweed的入侵Invasion of the Milkweed

    P2960 [USACO09OCT]Milkweed的入侵Invasion of the Milkweed 题目描述 Farmer John has always done his best to k ...

随机推荐

  1. [C++] Copy Control (part 1)

    Copy, Assign, and Destroy When we define a class, we specify what happens when objects of the class ...

  2. node包管理相关

    切换npm数据源 镜像使用方法(三种办法任意一种都能解决问题,建议使用第三种,将配置写死,下次用的时候配置还在): 1.通过config命令 npm config set registry https ...

  3. httpd 2.2.15 添加流媒体模块

    项目中使用的一直都是 httpd  2.2.15  用于播放视频资源,近期有个新产品上线发现快进视频会出现卡顿情况,因此添加了流媒体模块.(怀疑是新产品中的播放器进行了更改) 原文:http://li ...

  4. 单源最短路——SPFA算法(Bellman-Ford算法队列优化)

    spfa的算法思想(动态逼近法):     设立一个先进先出的队列q用来保存待优化的结点,优化时每次取出队首结点u,并且用u点当前的最短路径估计值对离开u点所指向的结点v进行松弛操作,如果v点的最短路 ...

  5. Agri-Net(最小生成树)

    Description Farmer John has been elected mayor of his town! One of his campaign promises was to brin ...

  6. 欢迎来怼-Alpha周(2017年10月19)贡献分配规则和分配结果

    .从alpha周(2017年10月19日开始的2周)开始,提高贡献分比重. 贡献分 : 团队分 = 1 : 5 教师会在核算每位同学总分时按比例乘以系数. 每位同学带入团队贡献分10分,如果团队一共7 ...

  7. 《剑指offer》---字符串的全排列

    本文算法使用python3实现 1.问题一 1.1 题目描述:   输入一个字符串,按字典序打印出该字符串中字符的所有排列.例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc ...

  8. iOS- 利用UIImageView自己整了个不会说话的汤姆猫

    1.实现思路 先说说我实现它的主要思路,很简单,主要利用UIImageView连续动画播放,和按钮的点击事件,就可以完成了这么一个简单的不会说话的汤姆猫. 2.实现细节 2.1.加载本地字典里保存的本 ...

  9. 【题解】APIO2018 Duathlon 铁人两项

    首先对于给出的图建立圆方树,然后我们分类讨论每一个点作为中间的中转站出现的情况有多少种,累积到 \(ans\) 中. 对于圆点:在任意两个子树内分别选出一个节点都是合法的. 对于方点:连接向方点的点均 ...

  10. [HAOI2006]受欢迎的牛 tarjan缩点 + 拓扑排序

    ---题面--- 题解: 首先tarjan缩点应该还是容易想到的,因为喜爱具有传递性,所以一个强联通分量里面的点实际上是全部等效的,所以我们可以缩成一个方便判断, 缩完点之后整张图就变成了一个有向无环 ...