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:

  1. abcxxxxabcxabcd
  2. abcdxabcxxxxabcx
输出样例#1:

  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[],s2[];
  10. ull l1,l2,hs1[],hs2[];
  11.  
  12. inline void Get_hash1()
  13. {
  14. for(int i=;i<=l1;i++)
  15. hs1[i]=hs1[i-]*p+s1[i]-'a';
  16. }
  17. inline void Get_hash2()
  18. {
  19. for(int i=;i<=l2;i++)
  20. hs2[i]=hs2[i-]*p+s2[i]-'a';
  21. }
  22. inline ull Q_pow(ull a,ull b)
  23. {
  24. ull base=a,ret=;
  25. for(;b;b>>=,base*=base)
  26. if(b&) ret*=base;
  27. return ret;
  28. }
  29. inline ull db1(ull x,ull y,ull P) { return hs1[y]-hs1[x-]*P; }
  30. inline ull db2(ull x,ull y,ull P) { return hs2[y]-hs2[x-]*P; }
  31.  
  32. int main()
  33. {
  34. scanf("%s%s",s1+,s2+);
  35. l1=strlen(s1+); l2=strlen(s2+);
  36. Get_hash1(); Get_hash2();
  37. int ans=;
  38. for(int i=;i<=l1;i++)
  39. if(hs1[i]==db2(l2-i+,l2,Q_pow(p,i))) ans=max(ans,i);
  40. for(int i=;i<=l2;i++)
  41. if(hs2[i]==db1(l1-i+,l1,Q_pow(p,i))) ans=max(ans,i);
  42. printf("%d",ans);
  43. return ;
  44. }

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

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

    题目描述 The cows enjoy mooing at the barn because their moos echo back, although sometimes not complete ...

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

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

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

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

  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. [POI2012]HUR-Warehouse Store(贪心,堆)

    题意 n天.第i天上午会进货Ai件商品,中午的时候会有顾客需要购买Bi件商品,可以选择满足顾客的要求,或是无视掉他. 如果要满足顾客的需求,就必须要有足够的库存.问最多能够满足多少个顾客的需求. (n ...

  2. 用xmanager6启动Linux上的图形界面程序

    1.下载Xmanager6 并自行安装,这里不赘述了 2.打开Xmanager.启动Xstart 3.按提示输入:主机IP,协议,用户名,命令,完成后点击“保存”,接着点击“运行”,运行xmanage ...

  3. JDBC连接ORACLE无法登陆java.sql.SQLException: ORA-01017: invalid username/password; logon denied

    当用jdbc连接Oracle数据库的时候 private Connection getConnection() throws SQLException { OracleDataSource ods = ...

  4. C#调用C/C++动态库,封装各种复杂结构体

    C#调用C/C++动态库,封装各种复杂结构体. 标签: c++结构内存typedefc# 2014-07-05 12:10 6571人阅读 评论(1) 收藏 举报  分类: C(8)  C#(6)  ...

  5. Java基础学习总结(14)——Java对象的序列化和反序列化

    一.序列化和反序列化的概念 把对象转换为字节序列的过程称为对象的序列化. 把字节序列恢复为对象的过程称为对象的反序列化. 对象的序列化主要有两种用途: 1) 把对象的字节序列永久地保存到硬盘上,通常存 ...

  6. 利用NSProxy解决NSTimer内存泄漏问题

    之前写过一篇利用RunTime解决由NSTimer导致的内存泄漏的文章,最近和同事讨论觉得这样写有点复杂,然后发现有NSProxy这么好用的根类,根类,根类,没错NSProxy与NSObject一样是 ...

  7. uva 104 Arbitrage (DP + floyd)

    uva 104 Arbitrage Description Download as PDF Background The use of computers in the finance industr ...

  8. 【LeetCode-面试算法经典-Java实现】【032-Longest Valid Parentheses(最长有效括号)】

    [032-Longest Valid Parentheses(最长有效括号)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string contai ...

  9. setTimeOut函数传参数

    这样使用,后面的4000无效 setTimeout(removeGift(customer_id,gift_id),4000); function removeGift(customer_id,gif ...

  10. 分布式架构中shiro

    分布式架构中shiro 前言:前段时间在搭建公司游戏框架安全验证的时候,就想到之前web最火的shiro框架,虽然后面实践发现在netty中不太适用,最后自己模仿shiro写了一个缩减版的,但是中间花 ...