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

时间:2017-11-29 19:46:02

标签: c arrays sequence

我们假设我有两个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;
}

1 个答案:

答案 0 :(得分:1)

您可以循环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