洛谷——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.
输入输出样例
abcxxxxabcxabcd
abcdxabcxxxxabcx
11
说明
'abcxxxxabcx' is a prefix of the first string and a suffix of the second string.
hash处理
#include <algorithm>
#include <cstring>
#include <cstdio> using namespace std; #define ull unsigned long long
#define p 233
char s1[],s2[];
ull l1,l2,hs1[],hs2[]; inline void Get_hash1()
{
for(int i=;i<=l1;i++)
hs1[i]=hs1[i-]*p+s1[i]-'a';
}
inline void Get_hash2()
{
for(int i=;i<=l2;i++)
hs2[i]=hs2[i-]*p+s2[i]-'a';
}
inline ull Q_pow(ull a,ull b)
{
ull base=a,ret=;
for(;b;b>>=,base*=base)
if(b&) ret*=base;
return ret;
}
inline ull db1(ull x,ull y,ull P) { return hs1[y]-hs1[x-]*P; }
inline ull db2(ull x,ull y,ull P) { return hs2[y]-hs2[x-]*P; } int main()
{
scanf("%s%s",s1+,s2+);
l1=strlen(s1+); l2=strlen(s2+);
Get_hash1(); Get_hash2();
int ans=;
for(int i=;i<=l1;i++)
if(hs1[i]==db2(l2-i+,l2,Q_pow(p,i))) ans=max(ans,i);
for(int i=;i<=l2;i++)
if(hs2[i]==db1(l1-i+,l1,Q_pow(p,i))) ans=max(ans,i);
printf("%d",ans);
return ;
}
洛谷——P2957 [USACO09OCT]谷仓里的回声Barn Echoes的更多相关文章
- 洛谷 2957 [USACO09OCT]谷仓里的回声Barn Echoes
题目描述 The cows enjoy mooing at the barn because their moos echo back, although sometimes not complete ...
- [USACO09OCT]谷仓里的回声Barn Echoes(hush、STL)
https://www.luogu.org/problem/P2957 题目描述 The cows enjoy mooing at the barn because their moos echo b ...
- [luoguP2957] [USACO09OCT]谷仓里的回声Barn Echoes(Hash)
传送门 团队里的hash水题,数据小的不用hash都能过.. 也就是前缀hash,后缀hash,再比较一下就行. ——代码 #include <cstdio> #include <c ...
- 洛谷——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 ...
随机推荐
- [NOIP2013提高组]火柴排队
题目:洛谷P1966.Vijos P1842.codevs3286. 题目大意:有两排火柴,每根都有一个高度.设a.b分别表示两排火柴的高度,现在要令$\sum(a_i-b_i)^2$最小.现两排火柴 ...
- AWK的介绍学习
第一节.awk的工作流程和基本用法 1.awk介绍 awk是一种报表生成器,就是对文件进行格式化处理的,这里的格式化不是文件系统的格式化,而是对文件内容进行各种"排版",进而格式化 ...
- NetHogs---按进程或程序实时统计网络带宽使用率。
NetHogs是一个开源的命令行工具(类似于Linux的top命令),用来按进程或程序实时统计网络带宽使用率. 来自NetHogs项目网站: NetHogs是一个小型的net top工具,不像大多数工 ...
- uva 1292 树形dp
UVA 1292 - Strategic game 守卫城市,城市由n个点和n-1条边组成的树,要求在点上安排士兵,守卫与点相连的边.问最少要安排多少士兵. 典型的树形dp.每一个点有两个状态: dp ...
- HDU 5391-Zball in Tina Town(数论)
题目地址:pid=5391">HDU 5391 题意: Tina Town 是一个善良友好的地方,这里的每个人都互相关心.Tina有一个球,它的名字叫zball. zball非常奇妙, ...
- Android Volley 具体解释 Google公布的一套用于网络通信的工具库
下载地址:git clone https://android.googlesource.com/platform/frameworks/volley 或 : https://github.com/mc ...
- hadoop(八) - sqoop安装与使用
一. sqoop安装: 安装在一台节点上就能够了. 1. 使用winscp上传sqoop 2. 安装和配置 加入sqoop到环境变量 将数据库连接驱动mysql-connector-5.1.8.jar ...
- tp5中的配置机制
默认在application中, 一个config.php, 一个database.php, 还有一个extra文件夹,里面存放一些零散的配置. 如果在index.php初始化中调整配置路径, 那么e ...
- 6.控制器(ng-Controller)
转自:https://www.cnblogs.com/best/tag/Angular/ ngController指令给视图添加一个控制器,控制器之间可以嵌套,内层控制器可以使用外层控制器的对象,但反 ...
- PHP高手进阶-LAMPer技能树