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处理

 #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 ;
}

最新文章

  1. My first win32 application program
  2. .htaccess应该放在哪里?
  3. 安卓Android面试题大全
  4. 转:怎么使用github(通俗易懂版)
  5. Window 8.1 开启Wifi共享
  6. Vim小知识
  7. JDK+Eclipse+MyEclipse+tomcat的安装与配置
  8. Mysql 计算时间间隔函数
  9. Android jni 编程3(对基本类型一维整型数组的操作)总结版
  10. PO BO VO DTO POJO DAO DO
  11. Android Studio之SVN打分支、切换分支及合并分支
  12. RXJS 实例操作符
  13. [原][openstack-pike][controller node][issue-3][horizon] dashboard show internal error 500 Cannot serve directory /var/www/html
  14. Myeclipse 2017 安装与破解
  15. wordpress有用的插件
  16. each的break
  17. 跨域(二)——WebSocket
  18. 机器视觉及图像处理系列之一(C++,VS2015)——搭建基本环境
  19. Jquery 选择器 详解 js 判断字符串是否包含另外一个字符串
  20. 关于 sql server 数据库权限乱七八糟的一些东西

热门文章

  1. NodeJS学习笔记 (11)网络UDP-dgram(ok)
  2. vSphere VCSA5.5加入AD域环境问题记录
  3. Python学习笔记(2)--基本数据类型
  4. 【Henu ACM Round#19 E】 Om Nom and Candies
  5. cacti1.1安装报错
  6. 洛谷——P1965 转圈游戏
  7. 三 概要模式 2) MR倒排索引、性能分析、搜索干扰词。
  8. HBase 1.1.2 优化插入 Region预分配
  9. ArcGIS api for javascript——图层-创建定制的切片图层类型的图层
  10. HDU Victor and World (最短路+状态压缩)