题目描述

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("");
}

最新文章

  1. Cocopods不显示三方库的解决方法
  2. 一些bug总结
  3. iOS - Swift 异常处理
  4. Delphi DecodeDate和EncodeDate 拆分和聚合时间函数的用法
  5. JVM-ClassLoader(转)
  6. TCP/IP详解之:SNMP
  7. CSS中设置height:100%无效的解决方案
  8. 6_css选择器
  9. Linux安装JDK、MySQL和Tomcat
  10. ubuntu 16.04 安装 opencv +contrib (3.2.0) + python 3.5
  11. [工控安全]“祝融”—一种针对PLC控制系统的欺骗攻击病毒
  12. Oracle不常用SQL
  13. ArcGIS鼠标滚轮方向之代码篇
  14. wechat 网页版通信全过程
  15. Delphi XE 10.2.3使用CEF4Delphi取网页元素时碰到&amp;nbsp;变问号?的处理
  16. [A类会议] 国内论文检索
  17. python 输出格式化之后的时间格式
  18. 安装部署Apache Hadoop (本地模式和伪分布式)
  19. 【转】nginx中proxy_set_header Host $host的作用
  20. git杂记-分支简介

热门文章

  1. Fluent Python: @property
  2. WIN8/8.1/10进入BIOS方法图解
  3. PokeCats开发者日志(十)
  4. Win10修改编辑hosts文件无法保存怎么办
  5. vue-cli项目里npm安装使用elementUI
  6. 【bzoj1596】[Usaco2008 Jan]电话网络 树形dp
  7. JS详细图解作用域链与闭包
  8. 具体数学数论章-----致敬Kunth
  9. BZOJ5300:[CQOI2018]九连环——题解
  10. BZOJ Lydsy5月月赛 ADG题解