在C ++中检查一个数组是否是另一个数组的子数组

Prerequisite: std::equal() function

先决条件: std :: equal()函数

Problem statement:

问题陈述:

Check if one array is subarray of another or not.

检查一个数组是否是另一个数组的子数组。

Example:

例:

Input 1:
Arr1= [3, 4, 5, 8]
Arr2= [1, 3, 4, 5, 8, 9, 10]
Output:
True
Arr1 is subarray of arr2
Input 2:
Arr1= [3, 4, 5, 8, 9 , 10, 12]
Arr2= [1, 3, 4, 5, 8, 9, 10]
Output:
False
None is subarray of one another
Input 3:
Arr1= [3, 4, 5, 8, 9]
Arr2= [3, 4]
Output:
True
Arr2 is subarray of arr1

Solution:

解:

We already saw that we have an STL function equal() that checks whether two ranges have the same elements in order or not. We can use that function to figure out whether an array is subarray of the other one or not. No doubt, if two array sizes are not equal then the smaller size one may have the possibility of being subarray of the bigger array. If their sizes are equal then they can't be a subarray of one another anyway.

我们已经看到我们有一个STL函数equal() ,它检查两个范围是否按顺序具有相同的元素。 我们可以使用该函数来确定一个数组是否是另一个数组的子数组。 毫无疑问,如果两个数组大小不相等,那么较小的数组就有可能成为较大数组的子数组。 如果它们的大小相等,则它们无论如何都不能成为彼此的子数组。

So there can be three cases:

因此可能有三种情况:

  1. Both the sizes of the array are equal

    数组的两个大小相等

    Simple return False

    简单返回False

  2. arr1 size is greater than arr2

    arr1的大小大于arr2

    Check for each range of

    检查每个范围

    arr1 having length the same as arr2 whether equal to arr2 or not. If we can find any such range equal to arr2 then arr2 is subarray of arr1. Following is the algorithm for this case.

    无论是否等于arr2, arr1的长度都与arr2相同。 如果我们找到任何等于arr2的范围,则arr2arr1的数组 。 以下是这种情况的算法。

    For i=0 to length(arr1)-length(arr2)
    If equal(arr2.begin(),arr2.end(), arr1.begin()+i) is true
    Then return true since we found a range 
    starting from arr1[i] which is equal to arr2
    Return false if no such range is found.
    
    
  3. arr2 size is greater than arr1

    arr2的大小大于arr1

    Check for each range of

    检查每个范围

    arr2 having length the same as arr1 whether equal to arr1 or not. If we can find any such range equal to arr1 then arr1 is subarray of arr2. Following is the algorithm for this case.

    无论是否等于arr1, arr2的长度都与arr1相同。 如果我们找到任何等于arr1的范围,则arr1arr2的数组 。 以下是这种情况的算法。

    For i=0 to length(arr2)-length(arr1)
    If equal(arr1.begin(),arr1.end(), arr2.begin()+i) is true
    Then return true since we found a range 
    starting from arr2[i] which is equal to arr1
    Return false if no such range is found.
    
    

C++ implantation:

C ++植入:

#include <bits/stdc++.h>
using namespace std;
void isSubarray(vector<int> arr1, vector<int> arr2)
{
if (arr1.size() == arr2.size()) {
cout << "False\n";
cout << "None of the Arrays can be subarray one other\n";
return;
}
else if (arr1.size() > arr2.size()) {
for (int i = 0; i < arr1.size() - arr2.size(); i++) {
if (equal(arr2.begin(), arr2.end(), arr1.begin() + i)) {
cout << "True\n";
cout << "Array2 is subarray of Array1\n";
}
}
}
else { //arr2.size()>arr1.size()
for (int i = 0; i < arr2.size() - arr1.size(); i++) {
if (equal(arr1.begin(), arr1.end(), arr2.begin() + i)) {
cout << "True\n";
cout << "Array1 is subarray of Array2\n";
}
}
}
}
int main()
{
cout << "Enter number of elements for array1\n";
int n;
cin >> n;
vector<int> arr1(n);
cout << "Input the elements\n";
for (int i = 0; i < n; i++)
cin >> arr1[i];
cout << "Enter number of elements for array2\n";
int m;
cin >> m;
vector<int> arr2(m);
cout << "Input the elements\n";
for (int i = 0; i < m; i++)
cin >> arr2[i];
isSubarray(arr1, arr2);
return 0;
}

Output:

输出:

Output1:
Enter number of elements for array1
4
Input the elements
3 4 5 8
Enter number of elements for array2
7
Input the elements
1 3 4 5 8 9 10
True
Array1 is subarray of Array2
Output2:
Enter number of elements for array1
7
Input the elements
3 4 5 8 9 10 12
Enter number of elements for array2
7
Input the elements
1 3 4 5 8 9 10
False
None of the Arrays can be subarray one other
Output3:
Enter number of elements for array1
5
Input the elements
3 4 5 8 9
Enter number of elements for array2
2
Input the elements
3 4
True
Array2 is subarray of Array1

翻译自: https://www.includehelp.com/data-structure-tutorial/check-if-one-array-is-subarray-of-the-other-or-not-in-cpp.aspx

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/546040.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

第二章 认识计算机硬件

*(%)^*&!*第一讲 认识计算机主板一、主板的结构1、主板结构分类&#xff08;2&#xff09;AT、Baby-AT型&#xff08;2&#xff09;ATX型&#xff08;3&#xff09;Micro ATX板型&#xff08;4&#xff09;LPX、NLX、Flex ATX板型&#xff08;5&#xff09;EATX、WATX板型&…

IDEA 不为人知的 5 个骚技巧!真香!

工欲善其事&#xff0c;必先利其器&#xff0c;磊哥最近发现了几个特别棒的 IDEA“骚”技巧&#xff0c;已经迫不及待的想要分享给你了&#xff0c;快上车...1.快速补全行末分号使用快捷键 Shfit Ctrl Enter 轻松实现。2.自带的 HTTP 请求工具IDEA 自带了 HTTP 的测试工具&am…

教育编程语言(转)

这是wikipedia上的内容&#xff0c;转载保存&#xff0c;以便以后查阅&#xff0c;英文版见Educational programming language 主要是介绍了一些适合于教育的编程语言&#xff0c;分别适合于不同的个人需求。 详细内容如下&#xff1a; 许多教育性质的程序设计语言都提供建议…

JavaScript | 将十进制转换为十六进制,反之亦然

Sometimes we need to convert an integer value which is in decimal format to the hexadecimal string in JavaScript or need a decimal value from a given hexadecimal string. 有时&#xff0c;我们需要将十进制格式的整数值转换为JavaScript中的十六进制字符串&#xf…

漫画:Integer 竟然有 4 种比较方法?

代码测试public class IntegerTest {public static void main(String[] args) {Integer i1 127;Integer i2 127;System.out.println(i1 i2);Integer i3 128;Integer i4 128;System.out.println(i3 i4);} }以上代码的执行结果为&#xff1a;truefalse首先&#xff0c;当我…

第三章 组装个人计算机

*(%)^*&!*第一讲 选购个人计算机部件1、计算机配件选购的基本原则&#xff08;1&#xff09;组装电脑按需配置&#xff0c;明确电脑使用范围&#xff1b;&#xff08;2&#xff09;衡量装机预算&#xff1b;&#xff08;3&#xff09;衡量整机运行速度。2、电脑配件选购注意…

IP地址的分类——a,b,c 类是怎样划分的

如今的IP网络使用32位地址&#xff0c;以点分十进制表示&#xff0c;如172.16.0.0。地址格式为&#xff1a;IP地址网络地址&#xff0b;主机地址 或 IP地址主机地址&#xff0b;子网地址&#xff0b;主机地址。 IP地址类型 最初设计互联网络时&#xff0c;为了便于寻址以及层次…

《Introduction to Computing Systems: From bits and gates to C and beyond》

很好的一本计算机的入门书&#xff0c;被很多学校采纳作为教材&#xff0c;作者Yale N. Patt 是计算机界的泰斗。中文版名为《计算机系统概论》&#xff08;译者&#xff1a;梁阿磊 , 蒋兴昌, 林凌&#xff09; 书籍首页 (旧版首页 &#xff09; LC-3相关工具 LC-3Help 采…

在数组中查找第k个最大元素_查找数组中每个元素的最近最大邻居

在数组中查找第k个最大元素Problem statement: 问题陈述&#xff1a; Given an array of elements, find the nearest (on the right) greatest element ofeach element in the array. (Nearest greatest means the immediate greatest one on the right side). 给定一个元素数…

6种快速统计代码执行时间的方法,真香!(史上最全)

我们在日常开发中经常需要测试一些代码的执行时间&#xff0c;但又不想使用向 JMH&#xff08;Java Microbenchmark Harness&#xff0c;Java 微基准测试套件&#xff09;这么重的测试框架&#xff0c;所以本文就汇总了一些 Java 中比较常用的执行时间统计方法&#xff0c;总共…

fltk 库

fltk是一个小型、开源、支持OpenGL 、跨平台&#xff08;windows,linux,mac OSX)的GUI库&#xff0c;它兼容xforms 图形库&#xff08;unix/linux下的一个C语言图形库)&#xff0c;所以可以用来开发模块化的程序&#xff0c;同时也可以使用面向对象开发程序&#xff0c;使用起来…

人工智能ai 学习_人工智能中强化学习的要点

人工智能ai 学习As discussed earlier, in Reinforcement Learning, the agent takes decisions in order to attain maximum rewards. These rewards are the reinforcements through which the agent learns in this type of agent. 如前所述&#xff0c;在“ 强化学习”中 &…

第四章 计算机软件安装与调试

*(%)^*&!*第一讲 系统BIOS和CMOS参数设置&#xff08;1&#xff09;一、BIOS、CMOS的基本概念1.BIOS的含义BIOS是只读存储器基本输入/输出系统的简写&#xff0c;是被雇花道计算机主板ROM芯片上的一组程序&#xff0c;为计算机提供最低级、最直接的硬件控制。2.CMOS的含义C…

连夜整理了几个开源项目,毕设/练手/私活一条龙!

一直以来&#xff0c;总有小伙伴问说&#xff1a;诶&#xff0c;有没有什么好的项目推荐啊&#xff0c;想参考使用。一般用途无非如下几种情况&#xff1a;自学练手&#xff1a;从书本和博客的理论学习&#xff0c;过渡到实践练手吸收项目经验&#xff0c;找工作写简历时能参考…

MPI编程简单介绍

第三章 MPI编程 3.1 MPI简单介绍 多线程是一种便捷的模型&#xff0c;当中每一个线程都能够訪问其他线程的存储空间。因此&#xff0c;这样的模型仅仅能在共享存储系统之间移植。一般来讲&#xff0c;并行机不一定在各处理器之间共享存储&#xff0c;当面向非共享存储系统开发…

英语电视节目网站

最近想练习一下英语听力&#xff0c;看到了一个网站&#xff0c;感觉好像还不错&#xff0c;播放比较流畅&#xff0c;语速相对来说比较慢&#xff0c;发音比较清晰。 链接&#xff1a;CSPAN 还有更多网站见&#xff1a;Broadband-Television BON CNC 英文在线广播&#x…

三位bcd加法计数器_两个8位BCD编号的加法| 8085微处理器

三位bcd加法计数器Problem statement: 问题陈述&#xff1a; To perform addition operation between two 8-bit BCD numbers using 8085 microprocessor. 使用8085微处理器在两个8位BCD编号之间执行加法运算。 Algorithm: 算法&#xff1a; Load the two numbers in HL pai…

第五章 计算机故障诊断与排除

*(%)^*&!*第一讲 计算机故障基础及电源类故障诊断和维护一、计算机故障的分类1.硬件故障硬件故障是指用户使用不当或由于电子元件故障而引起计算机硬件不能正常运行的故障。常见的硬件故障现象包括&#xff1a;&#xff08;1&#xff09;电源故障&#xff0c;导致没有供电或…

图灵奖演讲稿

刚刚读温伯格 的《理解专业程序员》&#xff0c;书中提到Floyd 图灵奖演讲中关于编程范式(programming paradigm )&#xff08;also see here )的演讲稿值得每个与编程有关的人一读&#xff0c;所以搜索了一些图灵奖相关的一些网络资源。 图灵奖主页 部分图灵奖演讲稿 其他资…

最简单的6种防止数据重复提交的方法!(干货)

有位朋友&#xff0c;某天突然问磊哥&#xff1a;在 Java 中&#xff0c;防止重复提交最简单的方案是什么&#xff1f;这句话中包含了两个关键信息&#xff0c;第一&#xff1a;防止重复提交&#xff1b;第二&#xff1a;最简单。于是磊哥问他&#xff0c;是单机环境还是分布式…