在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

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是用 C 语言实现把数组所有奇数放在另一个数组的代码: ```c #include <stdio.h> void separate_odd(int arr[], int n, int odd_arr[], int *odd_n) { // 遍历原数组,把奇数存到另一个数组 for (int i = 0; i < n; i++) { if (arr[i] % 2 == 1) { odd_arr[*odd_n] = arr[i]; (*odd_n)++; } } } int main() { int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; int n = sizeof(arr) / sizeof(arr[0]); int odd_arr[n]; int odd_n = 0; separate_odd(arr, n, odd_arr, &odd_n); printf("Original array: "); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("\n"); printf("Odd array: "); for (int i = 0; i < odd_n; i++) { printf("%d ", odd_arr[i]); } printf("\n"); return 0; } ``` 函数 `separate_odd` 的作用是把原数组 `arr` 的所有奇数放到另一个数组 `odd_arr` ,`odd_n` 是 `odd_arr` 奇数的数量。 在 `main` 函数,我们先创建了一个原数组 `arr`,然后计算出它的长度 `n`。接着创建一个和原数组同样长度的另一个数组 `odd_arr`,并定义一个变量 `odd_n` 用来记录 `odd_arr` 奇数的数量。 调用函数 `separate_odd`,把原数组的奇数存到 `odd_arr` 。最后,我们打印出原数组和奇数数组的内容,以检查函数是否正常工作。 注意到我们在调用函数 `separate_odd` 时,传递了一个指向 `odd_n` 的指针 `&odd_n`,这是为了让函数能够修改 `odd_n` 的值,以记录 `odd_arr` 奇数的数量。 希望这个代码能够帮助到你!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值