洛谷 P2957 [USACO09OCT]Barn Echoes G

该博客主要讲述了如何找出两个字符串之间的最大重叠部分。通过一个示例解释了问题,并提供了一个使用双层循环实现的AC代码来解决这个问题。输入是两个字符串,输出是它们的最大重叠长度。

题目描述

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.

原题:链接

我用的最基础的用双重循环遍历字符串的方法,这题也是各种做法都有,下面是AC代码

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
	
char s1[90],s2[90];
int len,cnt,maxlen;

int search(char s1[],char s2[])
{
	int m=strlen(s1);
	int n=strlen(s2);
	
	int k=0; 
	for(int i=0;i<m;i++)//遍历字符串s1 
    {
        for(int j=0;j<=i;j++)
        {
            if(s1[j]!=s2[n-1-i+j])//不等的时候停止 
            {
                k=1;
                break;
            }
        }
        if(k==0)
        {
            len=i+1;//记录长度 
            maxlen=max(maxlen,len);//比较并留下最大长度 
        }
    	k=0;
    }
    for(int i=0;i<n;i++)//遍历字符串s2 
    {
        for(int j=0;j<=i;j++)
        {
            if(s2[j]!=s1[m-1-i+j])
            {
                k=1;
                break;
            }
        }
        if(k==0)
        {
            len=i+1;
            maxlen=max(maxlen,len);
        }
    	k=0;
    }
    return maxlen;
}

int main()
{
	scanf("%s%s",&s1,&s2);
    printf("%d",search(s1,s2));
}

 

P2911 [USACO08OCT] Bovine Bones G 是【入门 4】数组中的一道题目。题目描述为 Bessie 喜欢棋盘游戏和角色扮演游戏,她说服 Farmer John 带她去一家爱好商店,在那里她购买了三个骰子用于投掷,这些骰子分别有 S1、S2 和 S3 个面[^1]。 在解题代码方面,有 Java 和 C++ 两种实现。Java 代码通过定义一个长度为 81 的数组统计不同和值出现的次数,通过嵌套循环模拟掷骰子的过程,将每个和值对应的数组元素加 1,同时更新最大出现次数及其对应的和值,最后输出最大出现次数对应的和值[^2]。示例代码如下: ```java import java.util.Scanner; public class P2911_1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int s1 = sc.nextInt(), s2 = sc.nextInt(), s3 = sc.nextInt(); int[] arr = new int[81]; int max = 0; int temp = 0; for (int i = 1; i <= s1; i++) { for (int j = 1; j <= s2; j++) { for (int k = 1; k <= s3; k++) { arr[i + j + k]++; if (temp < arr[i + j + k]) { max = i + j + k; temp = arr[i + j + k]; } } } } System.out.println(max); sc.close(); } } ``` C++ 代码同样使用数组统计和值出现的次数,先通过三重循环模拟掷骰子得到所有可能的和值并更新对应次数,再遍历所有可能的和值范围,找出出现次数最多且和值最小的结果并输出[^3]。示例代码如下: ```cpp #include<bits/stdc++.h> using namespace std; int s1, s2, s3, res; int cnt[20000]; int maxn = -1e9; int main() { scanf("%d%d%d", &s1, &s2, &s3); for (int i = 1; i <= s1; ++i) { for (int j = 1; j <= s2; ++j) { for (int k = 1; k <= s3; ++k) { cnt[i + j + k]++; } } } for (int i = 3; i <= s1 + s2 + s3; ++i) { if (cnt[i] > maxn) maxn = cnt[i], res = i; } printf("%d", res); return 0; } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值