C-查找数组是否是另一个数组的子序列



让我们假设我有这两个 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 arrayn");
  }
  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("%sn", 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;
}

相关内容

最新更新