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

时间:2019-03-22 20:12:05

标签: c arrays compare

假设我们有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个索引时的匹配数,但不计算每个可能的移位的匹配数,并返回导致最大匹配元素数的移位。

2 个答案:

答案 0 :(得分:1)

中的比较
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) ...的原因,它对于所使用的字符串永远是不正确的,因此 arr1 arr2 可以是 argv [1] argv [2] ,而不是硬编码
  • 如果您希望所有匹配项都删除所有与 ln1 ln2
  • 有关的代码

答案 1 :(得分:1)

这是C#解决方案。回家后可以发布C版本,但逻辑应该相同。

int matches = 0;
int maxMatches = 0;
int maxIndex = 0;

String[] arr1 = { "A", "B", "D", "D", "D", "A", "B", "A", "C" };
String[] arr2 = { "D", "D", "D", "A", "B", "K", "D", "D", "J", "A", "B", "A", "J", "J" };


for (int i = 0; i <= Math.Abs(arr1.Length - arr2.Length); i++)
{
    for (int j = 0; j < Math.Min(arr1.Length, arr2.Length); j++)
    {
        if ((i+j) < Math.Min(arr1.Length, arr2.Length)  &&  arr1[i + j] == arr2[j])
        {
            matches++;
        }
    }
    if(matches > maxMatches)
    {
        maxMatches = matches;
        maxIndex = i;
    }

    matches = 0;
}
Console.WriteLine("\nMaximum Matches: " + maxMatches.ToString() + " at index: " + maxIndex.ToString());
Console.Read();