簡體   English   中英

判斷一個數組是否是另一個數組的子序列

[英]finding if an array is a sub-sequence of another array

讓我們假設我有這兩個arraysint array[15] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}int s_array[15] = {1,2,3,4,5}

*請注意,我知道s_array[]設置為 15 但只有 5 個元素

在我的代碼的以下部分中,我想檢查s_array[]是否是array[] ] 的子序列,這意味着存儲在s_array[]中的任何值也存儲在array[]中,並且在同一個命令。 這意味着{3,5,4}不是子序列而{3,4,5}是。

這是代碼的一部分(我認為問題所在)。 js_array[]中的數量。 整個代碼將在下面顯示

            for( i = 0; i < 15; i++ ){
            if( s_array[0] == array[i] ){ /*if the first element of the s_array is not 
                                           available then it is not a sub-sequence and 
                                            there is no need to check the rest */ 
                for( Bcount = 1; Bcount < ( j - 1 ); Bcount++ ){
                    if( s_array[Bcount] == array[Bcount + i])
                        counter++;
                    else{
                        printf( "B is not a sub-sequence of A." );
                        break;
                    }
                }
                if( j == counter ){
                    printf( "B is a sub-sequence of A." );
                    break;
                }
            }
            else
                continue;
        }

感興趣的人的完整代碼或者它是否有助於更好地理解

#include <stdio.h>

int main(){
    int array[15], s_array[15], i = 0, j = 0, Bcount = 1, counter = 1;
    printf( "Please enter a sequence A: " );
    while(i < 15  && scanf("%d", &array[i]) == 1) {
          //input for array[], will always have 15 elements
        i++;
        if( i == 15){
            printf( "Please enter a sequence B: " );
            while(j < 15  && scanf("%d", &s_array[j]) == 1) {
                //input for s_array, will have somewhere between 1 and 15 elements
                j++;
            }
            for( i = 0; i < 15; i++ ){
                if( s_array[0] == array[i] ){
                    for( Bcount = 1; Bcount < ( j - 1 ); Bcount++ ){
                        if( s_array[Bcount] == array[Bcount + i])
                            counter++;
                        else{
                            printf( "B is not a sub-sequence of A." );
                            break;
                        }
                    }
                    if( j == counter ){
                        printf( "B is a sub-sequence of A." );
                        break;
                    }
                }
                else
                    continue;
            }
            return 5;
        }
    }
    return 0;
}

您可以遍歷array並為s_array設置索引計數器。 如果僅array[i] == s_array[j] ,則增加該計數器,否則重置。

如果滿足以下條件,則可以更早地結束循環:

  • 您已經找到子數組
  • 沒有足夠的元素在數組中進行比較

代碼示例:

#include <stdio.h>
#include<stdbool.h>

int main(void)
{
  int array[15] = {1, 2, 3, 4, 6, 7, 6, 9, 0, 1, 2, 3, 4, 5, 6};
  int s_array[5] = {2, 3, 4, 5, 6};

  const int size_array = sizeof(array) / sizeof(array[0]);
  const int size_s_array = sizeof(s_array) / sizeof(s_array[0]);

  bool isFound = false;

  if (size_s_array > size_array)
  {
    printf("Sub array is larger than the array\n");
  }
  else
  {
    int j = 0;
    for (int i = 0; (i < size_array) && (j != size_s_array) ; i++)
    {
      if (array[i] == s_array[j])
      {
        j++;
      }
      else
      {
        j = 0;
        if (i > size_array - size_s_array) // You can also put this in for loop's test condition, but I find this more readable.
        {
          break;
        }
      }
    }
    isFound = (j == size_s_array) ? true : false;
  }

  printf("%s\n", isFound ? "Found" : "Not found");

  return 0;
}

不同s_array的測試用例:

array[15] = {1, 2, 3, 4, 6, 7, 6, 9, 0, 1, 2, 3, 4, 5, 6};

int s_array[5] = {2, 3, 4, 5, 6};
Found

int s_array[1] = {8};
Not found

int s_array[2] = {1, 2};
Found

int s_array[15] = {1, 2, 3, 4, 6, 7, 6, 9, 0, 1, 2, 3, 4, 5, 6};
Found

int s_array[16] = {0, 1, 2, 3, 4, 6, 7, 6, 9, 0, 1, 2, 3, 4, 5, 6};
Sub array is larger than the array
Not found

int s_array[2] = {0, 2};
Not found

謝謝#vitruvius。 我實現了他的邏輯並修改了部分代碼。 這里是。

     #include<bits/stdc++.h>
using namespace std;
 
bool issub(int a[],int b[],int n,int m){
  
  int j=0;
  for(int i=0;i<n && j!=m;i++){
    if(a[i]==b[j]) 
    {
      j++;
    }
  }
 
  bool x=(j==m)?1:0;
  return x;
 
}
 
int main(){
   int n,m;
   cin>>n>>m;
  int a[n],b[m];
  for(int i=0;i<n;i++){
      cin>>a[i];
  }
  for(int i=0;i<m;i++){
      cin>>b[i];
  }
  if(issub(a,b,n,m)){
    cout<<"YES"<<endl;
  }
  else{
    cout<<"NO"<<endl;
  }
 
  return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM