C-计算2个数组中相同元素的数量

用户名

假设我们有2个数组:

char arr1[1024]="ABDDDABAC";
char arr2[1024]="DDDABKDDJABAJJ";

并希望将arr2具有最大匹配元素数的位置确定为arr1。例如,如果将arr1 [0]与arr2 [2]进行比较,则将导致5个匹配项,如下代码所示。

int matches=0;
for (int i=0;i<strlen(arr2);i++){
    if (arr1[i+2]==arr2[i]){
        matches++;
    }
}
printf("matches: %d\n", matches);

上面的代码返回将arr1移位2个索引时的匹配数,但不计算每个可能的移位的匹配数,而是返回导致最大匹配元素数的移位。

布鲁诺

比较中

for (int i=0;i<strlen(arr2);i++){
    if (arr1[i+2]==arr2[i]){
        matches++;
    }
}

仅考虑(以昂贵的方式)arr2的长度,没有关于arr1的保护,您可以通过未定义的行为来退出它

如果要查找最大匹配数,只需迭代arr1中可能的偏移量并保存最佳情况,例如:

#include <stdio.h>
#include <string.h>

int main()
{
  const char * arr1 = "ABDDDABAC";
  const char * arr2 = "DDDABKDDJABAJJ";
  size_t max = 0;
  const char * pmax;
  size_t ln1 = strlen(arr1);
  size_t ln2 = strlen(arr2);

  for (const char * a1 = arr1; *a1 ; ++a1) {
    size_t mx = 0;
    const char * p1 = a1;
    const char * p2 = arr2;

    while (*p1 && *p2) {
      if (*p1++ == *p2++)
        mx += 1;
    }

    printf("%d matches at offset %d\n",mx,  a1 - arr1);

    if (mx > max) {
      max = mx;
      pmax = a1;
      if (mx == ln2)
        /* useless to continue, cannot be better */
        break;
    }

    if (--ln1 < max)
      /* useless to continue, cannot be better */
      break;
  }

  if (max == 0)
    puts("no match");
  else
    printf("max matches %d at offset %d\n", max, pmax - arr1);
}

编译与执行:

pi@raspberrypi:/tmp $ gcc -g -pedantic -Wextra -Wall m.c
pi@raspberrypi:/tmp $ ./a.out
1 matches at offset 0
2 matches at offset 1
5 matches at offset 2
2 matches at offset 3
2 matches at offset 4
max matches 5 at offset 2

valgrind下执行

pi@raspberrypi:/tmp $ valgrind ./a.out
==10912== Memcheck, a memory error detector
==10912== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==10912== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==10912== Command: ./a.out
==10912== 
1 matches at offset 0
2 matches at offset 1
5 matches at offset 2
2 matches at offset 3
2 matches at offset 4
max matches 5 at offset 2
==10912== 
==10912== HEAP SUMMARY:
==10912==     in use at exit: 0 bytes in 0 blocks
==10912==   total heap usage: 1 allocs, 1 frees, 1,024 bytes allocated
==10912== 
==10912== All heap blocks were freed -- no leaks are possible
==10912== 
==10912== For counts of detected and suppressed errors, rerun with: -v
==10912== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 3)

注意 :

  • 代码没有对数组的大小做任何假设,这就是为什么它包含的if (--ln1 < max) ...内容对于使用的字符串永远是不正确的,因此arr1arr2可以是argv [1]argv [2]而不是硬编码
  • 如果您想要所有匹配项,请删除所有与ln1ln2有关的代码

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用[[($ B元素的总和)-($ C元素的总和)]]等于最小数量(如果可能为0)的$ A元素制作2个数组($ B和$ C)?

在C中交换2个数组

C:仅在2个数组中打印不常见的元素

在C中数组相同的元素

在C prog中将2个数组复制到1个数组中

在C中的数组中打印2个数字并跳过2个数字

计算C中数组元素的数量时* array和array [0]之间的差异

在C中交换2个数组时遇到的困难

如何在C中的2D数组中显示两个相同列的各个元素

如何计算指向 C# 中另一个数组的数组?

制作一个函数来计算C语言中PriorityQueue中的元素数量

C ++计算2个数字的比率

将数组作为元素放置到C中的另一个数组

在 c 中返回一个数组来计算数字

如何在C++中重新定位一个数组中的元素

尝试从2个数组中查找最小的数组元素将返回0(不起作用),而其他方法起作用。(C lang)

C ++:2个数组之间的差异

获取C ++中数组中相同值的数量

如何在O(1)或O(log n)时间复杂度中检查2个c ++数组是否相同(所有元素都相同,所以顺序很重要)?

计算C#中数组元素的频率

第一个数组元素在循环 C++ 中丢失

从文件夹中的图像创建一个数组并随机呈现给定数量的图像(Objective-C)

C ++中的2D整数数组,每行中元素数量不均匀

C ++:我有两个数组,其中第一个元素和最后一个元素具有相同的内存地址

将 2 个数组的排序合并到 C 中的第三个数组中

计算 C 中未定义字符数组的索引数量

将所有数组元素复制到C中的另一个数组中

在C中创建N个相同的数组

C ++函数查找3个数组的最大元素