【问题标题】:two strings of integers. Check if the second one is substring两个整数字符串。检查第二个是否是子字符串
【发布时间】:2021-11-02 12:09:30
【问题描述】:
#include <stdio.h>
int main() {
  int x[100], y[100], i, j, dim1, dim2, subsir=0;
  scanf("%d", &dim1);
  for(i=0; i<dim1; i++)
  {
      scanf("%d", &x[i]);
  }
  scanf("%d", &dim2);
  for(j=0; j<dim2; j++)
  {
      scanf("%d", &y[j]);
  }
  if(dim1<dim2)
  {
      printf("no");
      return 0;
  }
  for(i=0; i<dim1; i++)
    for(j=0; j<dim2; j++)
        {
        if(x[i]==y[j])
            subsir=1;
        }


  if(subsir==1)
  {
      printf("yes");
  }
  else
  {
      printf("no");
  }

  return 0;
}

考虑两个整数字符串。检查第二个字符串是否是C中第一个字符串的子字符串。(子字符串由第一个字符串的连续元素组成)

【问题讨论】:

  • 您必须提出问题。这里没有问题。
  • 问题是“如何解决?” .我需要一个想法
  • 你要的函数是strstr in &lt;string.h&gt;
  • 欢迎来到 Stack Overflow!这个问题很笼统,而这里的问题通常更具体一些。你都尝试了些什么?你能给我们看一些代码吗?您可以解决什么更简单的问题?
  • @my_name 如果您需要实现一个执行您所描述的功能,您必须首先显示您的一些代码。然后,我们可以帮助您查找代码中的错误/错误,以防万一它不起作用。

标签: c string substring


【解决方案1】:

您可以通过蛮力方法实现您的目标,尝试从0dim1 - dim2 的每个位置。

这是一个带有错误检查的修改版本:

#include <stdio.h>

int main() {
    int x[100], y[100], i, j, dim1, dim2;

    if (scanf("%d", &dim1) != 1 || dim1 > 100)
        return 1;
    for (i = 0; i < dim1; i++) {
        if (scanf("%d", &x[i]) != 1)
            return 1;
    }
    if (scanf("%d", &dim2) != 1 || dim2 > 100)
        return 1;

    /* no need to read second array if larger */
    if (dim1 < dim2) {
        printf("no\n");
        return 0;
    }
    for (j = 0; j < dim2; j++) {
        if (scanf("%d", &y[j]) != 1)
            return 1;
    }
    /* trying every possible sub-array start in array `x` */
    for (i = 0; i <= dim1 - dim2; i++) {
        /* compare all entries */
        for (j = 0; j < dim2; j++) {
            if (x[i + j] != y[j])
                break;
        }
        /* if above loop completed, we have a match */
        if (j == dim2) {
           printf("yes\n");
           return 0;
        }
    }
    printf("no\n");
    return 0;
}

【讨论】:

    【解决方案2】:

    一个可能的实现,基于您的方法:

    // this function will return `1` in case `arr2` is contained inside `arr1`, otherwise `0`
    int is_subarr(int arr1[], size_t arr1_len, int arr2[], size_t arr2_len) {
      // as you already did, we first check that arr1's len is not smaller than arr2's
      if (arr1_len < arr2_len) {
        return 0;
      }
    
      // useful check: in many implementations, when `arr2` is empty, they return true
      if (arr2_len == 0) {
        return 1;
      }
    
      // first loop: we go through all elements of `arr1` (but we stop when we have less elements left than `arr2`'s len)
      for (size_t i = 0; i < arr1_len && i < arr2_len; i += 1) {
        // temporary flag, we just pretend that `arr2` is contained, and then we check for the opposite
        int eq = 1;
    
        // starting with the current position `i` inside `arr1`, we start checking if `arr2` is contained
        for (size_t j = 0; j < arr2_len; j += 1) {
          // if it's not contained, we break from this inner loop, and we keep searching through `arr1`
          if (arr1[i + j] != arr2[j]) {
              eq = 0;
              break;
          }
        }
    
        // in case `arr2` is effectively found, we return `1`
        if (eq == 1) {
          return 1;
        }
      }
    
      return 0;
    }
    

    注意:“整数字符串”在 C 中称为“整数数组”;相反,“字符串”是“字符数组”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-11
      • 2016-04-07
      • 2011-02-07
      • 2013-05-12
      • 2013-02-10
      相关资源
      最近更新 更多