题目描述

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[81],s2[82];
int len1,len2;
LL g[82];
LL f1[82],f2[82];
void pre()
{g[0]=1;for(int i=1;i<max(len1,len2);i++) g[i]=g[i-1]*26%mod;f1[0]=s1[0]-'a';for(int i=1;i<len1;i++) f1[i]=(f1[i-1]*26+s1[i]-'a')%mod;f2[0]=s2[0]-'a';for(int i=1;i<len2;i++) f2[i]=(f2[i-1]*26+s2[i]-'a')%mod;
}
LL gethash(LL *f,int l,int r)
{if(!l) return f[r];  return (f[r]-f[l-1]*g[r-l+1]%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,0,i-1)==gethash(f2,len2-i,len2-1) ) { printf("%d",i); return 0; }if(gethash(f2,0,i-1)==gethash(f1,len1-i,len1-1) ) { printf("%d",i); return 0; }}printf("0");
}

转载于:https://www.cnblogs.com/TheRoadToTheGold/p/7358180.html

洛谷 2957 [USACO09OCT]谷仓里的回声Barn Echoes相关推荐

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

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

  2. 信息学奥赛一本通 1379:热浪(heatwv) | 洛谷 P1339 [USACO09OCT]Heat Wave G

    [题目链接] ybt 1379:热浪(heatwv) 洛谷 P1339 [USACO09OCT]Heat Wave G [题目考点] 1. 图论:最短路径 [解题思路] 首先抽象建模.城镇为顶点,道路 ...

  3. 59-硅谷课堂5-硅谷课堂-整合网关与订单 + 营销管理模块 + 公众号菜单管理

    59-硅谷课堂5-硅谷课堂-整合网关与订单和营销模块-- 笔记 笔记内容来源与尚硅谷教学视频 文章目录 59-硅谷课堂5-硅谷课堂-整合网关与订单和营销模块-- 笔记 笔记中涉及资源: 一.Sprin ...

  4. 60-硅谷课堂6-硅谷课堂-公众号消息和微信授权-- 笔记

    60-硅谷课堂6-硅谷课堂-公众号消息和微信授权-- 笔记 笔记内容来源与尚硅谷教学视频 文章目录 60-硅谷课堂6-硅谷课堂-公众号消息和微信授权-- 笔记 笔记中涉及资源: 一.公众号普通消息 ① ...

  5. 即时通讯源码-即时通讯集群服务免费-通讯百万并发技术-Openfire 的安装配置教程手册-哇谷即时通讯集群方案-哇谷云-哇谷即时通讯源码

    即时通讯源码-即时通讯集群服务免费-通讯百万并发技术-Openfire 的安装配置教程手册-哇谷即时通讯集群方案-哇谷云 1,openfire开发环境配置 很久没有写点东西了.最近很烦心,领导不给力. ...

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

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

  7. 【洛谷1339 [USACO09OCT]】热浪Heat Wave 图论+最短路

    AC代码 #include<bits/stdc++.h> using namespace std; const int MAXN=62000+10,INF=999999; struct E ...

  8. 洛谷 P2701 [USACO5.3]巨大的牛棚Big Barn

    题目背景 (USACO 5.3.4) 题目描述 农夫约翰想要在他的正方形农场上建造一座正方形大牛棚.他讨厌在他的农场中砍树,想找一个能够让他在空旷无树的地方修建牛棚的地方.我们假定,他的农场划分成 N ...

  9. python验证角谷_角谷猜想-随心随性无为而为-51CTO博客

    问题描述: 角谷猜想的内容为:任意给定一个自然数,若它为偶数则除以2,若它为奇术则乘3加1,得到一个新的自然数.按照这样的计算方法计算下去,若干次后得到的结果必然为1. 编写程序对角谷猜想的正确性加以 ...

  10. java角谷_JAVA 角谷定理 递归

    角谷定理.输入一个自然数,若为偶数,则把它除以2,若为奇数,则把它乘以3加1.经过如此有限次运算后,总可以得到自然数值1.求经过多少次可得到自然数1. 如:输入22, 输出 22 11 34 17 5 ...

最新文章

  1. ad用户和计算机的使用方法,AD技巧之指定用户登录和指定计算机登陆
  2. 【BZOJ2631】tree (LCT)
  3. 【转】【Android】使用BaseAdapter实现复杂的ListView
  4. poj 1979 Red and Black(BFS)
  5. 全国计算机网络自学考试,2008年1月全国自考“计算机网络基本原理”试题
  6. Appium+python自动化(十五)- Android 这些基础知识,你知多少???(超详解)...
  7. SQL2005备份集中的数据库备份与现有的数据库不同,错误号码:3154,解决方法...
  8. 如何使用JPA和Hibernate映射JSON集合
  9. C语言数组查找(线性查找 折半查找)
  10. 关于Android屏幕适配
  11. 计算机虚拟仪器技术与测试技术相关的概念,虚拟测试技术概念辨析.pdf
  12. keepalived java,keepalived+nginx+tomcat高可用负载均衡
  13. 2022年信息安全工程师考试知识点:Web安全
  14. 论程序员脱发是传言还是真的?
  15. 1+x云计算中级,第二天串讲,拓扑图
  16. 数学基础_若要使骰子(六个面)的每个数都出现至少一次,那么平均需要掷多少次骰子?
  17. “金山云感知城市”在重庆智博会发布 加速推动智慧城市发展
  18. C++中的clock计时不准?
  19. 输出图元---OpenGL显示窗口重定形函数
  20. 全球与中国射频识别零售管理市场深度研究分析报告

热门文章

  1. Python实战案例:tornado接口vue客户端的堂食点餐系统(中)
  2. android微信换行,微信终于支持换行输入了,只需要一个简单的操作-excel自动换行...
  3. 计算机文化基础项目化教程答案,第七版计算机文化基础实践教程 第3-4章习题答案及解析...
  4. Mac软件无响应怎么办?
  5. 数据库复习(四)Redis
  6. 2021年中国天眼对外开放,美媒感叹中国崛起的科技实力
  7. 神经同步预测儿童学习新单词的能力
  8. 什么是API接口平台?作用是什么?
  9. C语言 由拼音字串获取拼音数量
  10. 基于matlab的眼部疲劳检测