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

https://www.luogu.org/problem/show?pid=2957

题目描述

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.

hash处理

 1 #include <algorithm>
 2 #include <cstring>
 3 #include <cstdio>
 4
 5 using namespace std;
 6
 7 #define ull unsigned long long
 8 #define p 233
 9 char s1[88],s2[88];
10 ull l1,l2,hs1[88],hs2[88];
11
12 inline void Get_hash1()
13 {
14     for(int i=1;i<=l1;i++)
15         hs1[i]=hs1[i-1]*p+s1[i]-‘a‘;
16 }
17 inline void Get_hash2()
18 {
19     for(int i=1;i<=l2;i++)
20         hs2[i]=hs2[i-1]*p+s2[i]-‘a‘;
21 }
22 inline ull Q_pow(ull a,ull b)
23 {
24     ull base=a,ret=1;
25     for(;b;b>>=1,base*=base)
26         if(b&1) ret*=base;
27     return ret;
28 }
29 inline ull db1(ull x,ull y,ull P) { return hs1[y]-hs1[x-1]*P; }
30 inline ull db2(ull x,ull y,ull P) { return hs2[y]-hs2[x-1]*P; }
31
32 int main()
33 {
34     scanf("%s%s",s1+1,s2+1);
35     l1=strlen(s1+1); l2=strlen(s2+1);
36     Get_hash1(); Get_hash2();
37     int ans=0;
38     for(int i=1;i<=l1;i++)
39         if(hs1[i]==db2(l2-i+1,l2,Q_pow(p,i))) ans=max(ans,i);
40     for(int i=1;i<=l2;i++)
41         if(hs2[i]==db1(l1-i+1,l1,Q_pow(p,i))) ans=max(ans,i);
42     printf("%d",ans);
43     return 0;
44 }
时间: 2024-08-11 07:43:37

洛谷——P2957 [USACO09OCT]谷仓里的回声Barn Echoes的相关文章

洛谷——P2639 [USACO09OCT]Bessie的体重问题Bessie&#39;s We…

https://www.luogu.org/problem/show?pid=2639 题目描述 Bessie像她的诸多姊妹一样,因为从Farmer John的草地吃了太多美味的草而长出了太多的赘肉.所以FJ将她置于一个及其严格的节食计划之中.她每天不能吃多过H (5 <= H <= 45,000)公斤的干草. Bessie只能吃一整捆干草:当她开始吃一捆干草的之后就再也停不下来了.她有一个完整的N (1 <= N <= 500)捆可以给她当作晚餐的干草的清单.她自然想要尽量吃到更

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

P2958 [USACO09OCT]木瓜的丛林Papaya Jungle 题目描述 Bessie has wandered off the farm into the adjoining farmer's land. He raises delicious papaya fruit, which is a delicacy for cows. The papaya jungle is partitioned into a grid of squares with R rows and C col

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

P1339 [USACO09OCT]热浪Heat Wave 题目描述 The good folks in Texas are having a heatwave this summer. Their Texas Longhorn cows make for good eating but are not so adept at creating creamy delicious dairy products. Farmer John is leading the charge to delive

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

P2959 [USACO09OCT]悠闲漫步The Leisurely Stroll 题目描述 Bessie looks out the barn door at the beautiful spring day and thinks to herself, 'I'd really like to enjoy my walk out to the pastures for the tender spring grass.' She knows that once she leaves the b

洛谷 P1339 [USACO09OCT]热浪Heat Wave

题目描述 The good folks in Texas are having a heatwave this summer. Their Texas Longhorn cows make for good eating but are not so adept at creating creamy delicious dairy products. Farmer John is leading the charge to deliver plenty of ice cold nutritiou

洛谷P1339 [USACO09OCT]热浪Heat Wave

思路:裸SPFA过一遍(建议使用邻接链表存储),无向图,无向图,无向图,重要的事情要说三遍!!!蜜汁RE是什么鬼????第九个点数组开到20K,第十个点数组开到30K才AC.或许我代码写的有bug?(逃 CODE: 1 #include<iostream> 2 #include<queue> 3 using namespace std; 4 queue<int>q; 5 struct Edge{ 6 int pre,to,w; 7 }edge[30000]; 8 int

洛谷 P2701 [USACO5.3]巨大的牛棚Big Barn Label:二维数组前缀和 你够了 这次我用DP

题目背景 (USACO 5.3.4) 题目描述 农夫约翰想要在他的正方形农场上建造一座正方形大牛棚.他讨厌在他的农场中砍树,想找一个能够让他在空旷无树的地方修建牛棚的地方.我们假定,他的农场划分成 N x N 的方格.输入数据中包括有树的方格的列表.你的任务是计算并输出,在他的农场中,不需要砍树却能够修建的最大正方形牛棚.牛棚的边必须和水平轴或者垂直轴平行. EXAMPLE 考虑下面的方格,它表示农夫约翰的农场,‘.'表示没有树的方格,‘#'表示有树的方格 1 2 3 4 5 6 7 8 1 .

洛谷P1724 东风谷早苗

题目描述 在幻想乡,东风谷早苗是以高达控闻名的高中生宅巫女.某一天,早苗终于入手了最新款的钢达姆模型.作为最新的钢达姆,当然有了与以往不同的功能了,那就是它能够自动行走,厉害吧(好吧,我自重).早苗的新模型可以按照输入的命令进行移动,命令包含’E’.’S’.’W’.’N’四种,分别对应四个不同的方向,依次为东.南.西.北.执行某个命令时,它会向着对应方向移动一个单位.作为新型机器人,自然不会只单单执行一个命令,它可以执行命令串.对于输入的命令串,每一秒它会按照命令行动一次.而执行完命令串最后一个

洛谷——P1724 东风谷早苗

P1724 东风谷早苗 题目描述 在幻想乡,东风谷早苗是以高达控闻名的高中生宅巫女.某一天,早苗终于入手了最新款的钢达姆模型.作为最新的钢达姆,当然有了与以往不同的功能了,那就是它能够自动行走,厉害吧(好吧,我自重).早苗的新模型可以按照输入的命令进行移动,命令包含’E’.’S’.’W’.’N’四种,分别对应四个不同的方向,依次为东.南.西.北.执行某个命令时,它会向着对应方向移动一个单位.作为新型机器人,自然不会只单单执行一个命令,它可以执行命令串.对于输入的命令串,每一秒它会按照命令行动一次