当前位置: 首页 > news >正文

洛谷:P2957 [USACO09OCT] Barn Echoes G

题目描述

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.

学过哈希字符串就直接套模板就行了,虽然我因为一个小细节WA了无数次

#include<iostream>
#include<cstring>
using namespace std;
const int N = 1e5+3, P = 131;  //为什么是131,可以学一手字符串哈希
typedef unsigned long long UUL;
UUL e1[N], e2[N], p[N];
char str1[100], str2[100];
int a, b, len1, len2, res;
UUL get(int l,int r,UUL e[])
{return e[r] - e[l - 1] * p[r - l + 1];  //返回区间的字符串,前缀和
}
int main()
{cin >> str1 +1 >> str2+1; p[0] = 1;for (int i = 1; str1[i]; i++) {e1[i] = e1[i - 1] * P + str1[i];  //把字符串看成二进制求前缀和}for (int i = 1; str2[i]; i++){e2[i] = e2[i - 1] * P + str2[i];  //把字符串看成二进制求前缀和}for (int i = 1; str1[i] || str2[i]; i++){p[i] = p[i - 1] * P;     //计算进位}len1 = strlen(str1+1);len2 = strlen(str2+1);for(int i =1; str1[i] || str2[i];i++){int a1, b1;if (len1 < i || len2 < i) break;//判断边界if (get(1, i, e1) == get(len2 - i + 1, len2, e2)) //如果子串相等,此时i就是这个字串的长度a1 = i;elsea1 = -1;  //当他们不相等时,就结束计算,这里一定要结束计数if (get(len1 - i + 1, len1, e1) == get(1, i, e2))  //如果子串相等,此时i就是这个字串的长度b1 = i;elseb1 = -1;   res = max(res, max(a1, b1));//求最大值}cout << res << endl;return 0;
}

可以看另一篇博客字符串哈希

详情请看字符串哈希

算法小白的刷题日记

http://www.lryc.cn/news/294068.html

相关文章:

  • flinksqlbug : AggregateFunction udf Could not extract a data type from
  • Aigtek高压放大器用途是什么呢
  • c++ STL less 的视角
  • MQ面试题整理(持续更新)
  • 2401cmake,学习cmake2
  • 理解Jetpack Compose中的`remember`和`mutableStateOf`
  • 3D力导向树插件-3d-force-graph学习002
  • QXlsx Qt操作excel
  • Node.js 包管理工具
  • PyTorch 2.2 中文官方教程(十七)
  • Failed at the chromedriver@2.27.2 install script.
  • OpenResty 安装
  • 套路化编程 C# winform 自适应缩放布局
  • 源码梳理(3)MybatisPlus启动流程
  • 《学成在线》微服务实战项目实操笔记系列(P1~P49)【上】
  • 两种添加删除属性字段的方法
  • ObjectMapper之处理JSON序列化和反序列化
  • Sklearn、TensorFlow 与 Keras 机器学习实用指南第三版(八)
  • 【51单片机】直流电机实验和步进电机实验
  • django+flask网上购物商城系统的设计与实现python-vue
  • 公共用例库计划--个人版(六)典型Bug页面设计与开发
  • impala与kudu进行集成
  • 链表经典算法(+OJ刷题)
  • 网络原理TCP/IP(4)
  • 【C/C++ 11】贪吃蛇游戏
  • 【日常总结 - java】list 与 字符串(用逗号隔开)相互转换
  • 《幻兽帕鲁》好玩吗?幻兽帕鲁能在Mac上运行吗?
  • 【数据分享】1929-2023年全球站点的逐日平均能见度(Shp\Excel\免费获取)
  • 浅谈——开源软件的影响力
  • MySQL-事务(TRANSACTION)