[USACO09OCT]Barn Echoes G

1|0题目描述

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。

2|0输入格式

* Lines 1..2: Each line has the text of a moo or its echo

3|0输出格式

* 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.

4|0输入输出样例

输入 #1
abcxxxxabcxabcd abcdxabcxxxxabcx
输出 #1
11
复制代码
1 #include<bits/stdc++.h> 2 using namespace std; 3 string s1,s2; 4 int res,ans; 5 int main() 6 { 7 cin>>s1>>s2; 8 for(int i=0;i<s1.size();i++) 9 { 10 string tmp=s1.substr(0,i),tmp1; 11 if(i>=s2.size()) tmp1=s2; 12 else tmp1=s2.substr(s2.size()-i,i); 13 if(tmp==tmp1) res=max(res,i); 14 } 15 for(int i=0;i<s2.size();i++) 16 { 17 string tmp=s2.substr(0,i),tmp1; 18 if(i>=s1.size()) tmp1=s1; 19 else tmp1=s1.substr(s1.size()-i,i); 20 if(tmp==tmp1) ans=max(ans,i); 21 } 22 res=max(res,ans); 23 cout<<res; 24 return 0; 25 }
复制代码

 


__EOF__

本文作者Sakurajimamai
本文链接https://www.cnblogs.com/o-Sakurajimamai-o/p/17438797.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   o-Sakurajimamai-o  阅读(9)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
-- --
点击右上角即可分享
微信分享提示