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

在C ++中检查一个数组是否是另一个数组的子数组相关推荐

  1. 977. 有序数组的平方|209. 长度最小的子数组|59. 螺旋矩阵 II

    977. 有序数组的平方 原理 准备:双指针.一个空数组.双指针指向的两个元素作比较,更大的数平方之后,放入空数组的尾部空位. 图解 其实这题的指针有两种方法: 从两边向中间靠拢,得到的是由大到小的值 ...

  2. 随想录一期 day2 [977.有序数组的平方|209. 长度最小的子数组|59.螺旋矩阵II(剥洋葱)]

    977.有序数组的平方 给你一个按 非递减顺序 排序的整数数组 nums,返回 每个数字的平方 组成的新数组,要求也按 非递减顺序 排序. 思路 递增数组,平方后最大值一定在最左侧或者最右侧,可想到– ...

  3. 算法------长度最小的子数组

    长度最小的子数组 给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组.如果不存在符合条件的连续子数组,返回 0.示例: 输入: s = 7, nu ...

  4. python【力扣LeetCode算法题库】1248- 统计「优美子数组」

    统计「优美子数组」 给你一个整数数组 nums 和一个整数 k. 如果某个 连续 子数组中恰好有 k 个奇数数字,我们就认为这个子数组是「优美子数组」. 请返回这个数组中「优美子数组」的数目. 示例 ...

  5. 209. Minimum Size Subarray Sum 长度最小的子数组

    Title 给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组,并返回其长度.如果不存在符合条件的连续子数组,返回 0. **示例: ** 输入: ...

  6. 13--长度最小的子数组

    文章目录 1.题目要求 2.解题代码 1.题目要求 给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组,并返回其长度.如果不存在符合条件的连续子数 ...

  7. Leetcode--209. 长度最小的子数组

    给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组.如果不存在符合条件的连续子数组,返回 0. 示例: 输入: s = 7, nums = [2, ...

  8. leetcode —— 209. 长度最小的子数组

    给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组,并返回其长度.如果不存在符合条件的连续子数组,返回 0. 示例: 输入: s = 7, num ...

  9. 子数组(Subarray)

    Subarray 一.前缀和 1800.最大升序子数组和 525.连续数组 523.连续的子数组和 974.和可被K整除的子数组 ★560.和为K的子数组 1524.和为奇数的子数组数目 1695.删 ...

  10. 牛客网:乘积为正数的最长连续子数组

    1.非环形 注意审题!! 这里是求连续最长乘积为正数的长度是多少,我们维护两个长度,一个pos一个neg,pos代表到当前这个数为止的最长正数乘积,neg代表到当前这个数为止的最长负数乘积,我们对每个 ...

最新文章

  1. HDU_1253 胜利大逃亡(BFS)
  2. OPENCV-7 学习笔记
  3. CAFFE怎样跑起来
  4. LeetCode 134. 加油站(贪心)
  5. DetNAS:首个搜索物体检测Backbone的方法 | NeurIPS 2019
  6. 使用PyTorch来进展不平衡数据集的图像分类
  7. Java基础整理(二)
  8. 最短路径(二维矩阵)
  9. java servlet配置_JavaWeb编程 Servlet的基本配置
  10. 思科首席技术官解析:统一计算及战略
  11. OpenCasCade网格的显示
  12. 股票估值法研究报告_论述股票的估值方法
  13. KVM切换器如何选购
  14. 快手-艳云脚本云控系统
  15. python turtle画彩虹的源代码_Python基础实例——绘制彩虹(turtle库的应用)
  16. 【DP】饥饿的WZK(hunger)
  17. 初中三年级数学可以用计算机吗,不到3分钟,这份初中数学攻略被家长和学生疯狂转发!太实用了!...
  18. 利用vue实现“显示更多”功能
  19. 适用于高级别自动驾驶的驾驶员可预见误用仿真测试
  20. 使用命令行配置启动windows服务

热门文章

  1. C语言case字句有什么作用,switch case 语句的使用规则
  2. nginx中的nginx.conf.default配置
  3. 你缺啥,你缺一个得力的办公软件
  4. 全新的membership框架Asp.net Identity(1)——.Net membership的历史
  5. Linux系统的基本法则
  6. maven 基本命令
  7. Oracle-物化视图
  8. android Fragments详解一:概述
  9. Linux下如何同时启动多个Tomcat服务器
  10. C程序设计语言上机13,《高级语言程序设计》北大上机试题(十三)