在C ++中检查一个数组是否是另一个数组的子数组
Prerequisite: std::equal() function
先決條件: std :: equal()函數(shù)
Problem statement:
問(wèn)題陳述:
Check if one array is subarray of another or not.
檢查一個(gè)數(shù)組是否是另一個(gè)數(shù)組的子數(shù)組。
Example:
例:
Input 1: Arr1= [3, 4, 5, 8] Arr2= [1, 3, 4, 5, 8, 9, 10]Output: True Arr1 is subarray of arr2Input 2: Arr1= [3, 4, 5, 8, 9 , 10, 12] Arr2= [1, 3, 4, 5, 8, 9, 10]Output: False None is subarray of one anotherInput 3: Arr1= [3, 4, 5, 8, 9] Arr2= [3, 4]Output: True Arr2 is subarray of arr1Solution:
解:
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.
我們已經(jīng)看到我們有一個(gè)STL函數(shù)equal() ,它檢查兩個(gè)范圍是否按順序具有相同的元素。 我們可以使用該函數(shù)來(lái)確定一個(gè)數(shù)組是否是另一個(gè)數(shù)組的子數(shù)組。 毫無(wú)疑問(wèn),如果兩個(gè)數(shù)組大小不相等,那么較小的數(shù)組就有可能成為較大數(shù)組的子數(shù)組。 如果它們的大小相等,則它們無(wú)論如何都不能成為彼此的子數(shù)組。
So there can be three cases:
因此可能有三種情況:
Both the sizes of the array are equal
數(shù)組的兩個(gè)大小相等
Simple return False
簡(jiǎn)單返回False
arr1 size is greater than arr2
arr1的大小大于arr2
Check for each range of
檢查每個(gè)范圍
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.
無(wú)論是否等于arr2, arr1的長(zhǎng)度都與arr2相同。 如果我們找到任何等于arr2的范圍,則arr2是arr1的子數(shù)組 。 以下是這種情況的算法。
For i=0 to length(arr1)-length(arr2)If equal(arr2.begin(),arr2.end(), arr1.begin()+i) is trueThen return true since we found a range starting from arr1[i] which is equal to arr2 Return false if no such range is found.arr2 size is greater than arr1
arr2的大小大于arr1
Check for each range of
檢查每個(gè)范圍
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.
無(wú)論是否等于arr1, arr2的長(zhǎng)度都與arr1相同。 如果我們找到任何等于arr1的范圍,則arr1是arr2的子數(shù)組 。 以下是這種情況的算法。
For i=0 to length(arr2)-length(arr1)If equal(arr1.begin(),arr1.end(), arr2.begin()+i) is trueThen 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 10True Array1 is subarray of Array2Output2: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 otherOutput3: 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
總結(jié)
以上是生活随笔為你收集整理的在C ++中检查一个数组是否是另一个数组的子数组的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: c构造函数和析构函数_C ++构造函数和
- 下一篇: 如果不这样用,Nacos也有安全问题!