洛谷: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;
		else
			a1 = -1;  //当他们不相等时,就结束计算,这里一定要结束计数
		if (get(len1 - i + 1, len1, e1) == get(1, i, e2))  //如果子串相等,此时i就是这个字串的长度
			b1 = i;
		else
			b1 = -1;   
		res = max(res, max(a1, b1));//求最大值
	}
	cout << res << endl;
	return 0;
}

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

详情请看字符串哈希

算法小白的刷题日记

相关推荐

  1. P2957 [USACO09OCT] Barn Echoes G

    2024-02-06 03:46:02       40 阅读
  2. P2895 [USACO08FEB] Meteor Shower S

    2024-02-06 03:46:02       15 阅读
  3. 【题解】 P9183 [USACO23OPEN] FEB B

    2024-02-06 03:46:02       39 阅读
  4. 题解】 P1696 [USACO18JAN] Blocked Billboard II B

    2024-02-06 03:46:02       13 阅读
  5. P8823

    2024-02-06 03:46:02       36 阅读
  6. P2863

    2024-02-06 03:46:02       18 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-06 03:46:02       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-06 03:46:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-06 03:46:02       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-06 03:46:02       20 阅读

热门阅读

  1. 设计模式分类

    2024-02-06 03:46:02       35 阅读
  2. 292.Nim游戏

    2024-02-06 03:46:02       35 阅读
  3. 【Django-ninja】使用schema

    2024-02-06 03:46:02       32 阅读
  4. docker容器stop流程

    2024-02-06 03:46:02       30 阅读
  5. Linux cp命令(cp指令)解析

    2024-02-06 03:46:02       32 阅读
  6. 每日一题 力扣1696跳跃游戏

    2024-02-06 03:46:02       39 阅读
  7. 【数学1】基础数学问题

    2024-02-06 03:46:02       39 阅读
  8. 【Android】代码混淆简单介绍

    2024-02-06 03:46:02       40 阅读
  9. 异或加密原理及简单应用(C语言版)

    2024-02-06 03:46:02       37 阅读