比较C中两个字符串的排列

Comparing the permutations of two strings in C

我刚开始学习 C 基础知识并尝试解决这个问题,我们必须检查两个字符串是否相等(提供任何排列)。 你可以参考这个link:https://www.hackerearth.com/practice/basic-programming/input-output/basics-of-input-output/practice-problems/algorithm/two-strings-4/

我只是想获得一些关于如何改进我的代码的解决方案,它只提供输出 'NO':

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

int main()
{
    int i, j, k, m, n, o, p;
    char a[100000], b[100000], *c, *d;
    scanf("%d", &i);

    for (j = 0; j < i; j++)
    {
        scanf("%s %s", a, b);
    }
    for (k = 0; a[k] != '[=10=]'; k++)
    {
        n = rand() % k;
    }
    for (m = 0; b[m] != '[=10=]'; m++)
    {
        o = rand() % m;
    }
    for (p = 0; p < j; p++)
    {
        if (a[n] == b[o])
        {
            printf("YES");
        }
        else
        {
            printf("NO");
        }
    }
    return 0;
}

感谢您的帮助!

比较两个字符串使用 strcmp() :

int strcmp(const char *str1, const char *str2)

参数:

  1. str1 - 这是要比较的第一个字符串。
  2. str2 - 这是要比较的第二个字符串。

Return 值:

此函数return取值如下:

  • if Return value < 0 then it structures str1 is less than str2.
  • 如果Return value > 0 表示str2小于str1.
  • if Return value = 0 表示 str1 等于 str2.

示例:

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

int main () {
   char str1[15];
   char str2[15];
   int ret;


   strcpy(str1, "abcdef");
   strcpy(str2, "ABCDEF");

   ret = strcmp(str1, str2);

   if(ret < 0) {
      printf("str1 is less than str2");
   } else if(ret > 0) {
      printf("str2 is less than str1");
   } else {
      printf("str1 is equal to str2");
   }

   return(0);
}

因为您只需要一些关于改进代码的建议。

  1. 您可以使用 scanf("%20s", str1) 或类似的东西来改善答案的内存占用。您将需要使用循环来读取字符串。 %20s 要求 scanf 最多读取 20 个字符。您可以根据自己的需要定制号码。
  2. 您可以通过 string.h 中包含的 strlen 函数获取字符串的长度。
  3. 你只想查看每个字符出现的次数。在您的情况下,您可以使用长度为 26 或两个的整数数组,具体取决于您的算法。
  4. 使用更好的变量名。如果你在算法上做错了,这真的很有帮助。

This would be my solution just for one string comparision

用户代码的详细评估

for (j = 0; j < i; j++)
{
    scanf("%s %s", a, b);
}

此代码块读取所有行。 C 是按顺序计算的,因此您需要像

for (j = 0 ; j < i ; j++)
{
    scanf("%s %s", a, b);
    /* do comparision for each here */
}

正如我上面提到的,C 是按顺序求值的,因此接下来的 2 个 for 循环也会求值并从两个字符串中随机选择 2 个字符。我没有分析概率,但从我的感觉来看,我可以说大多数时候它不会击中同一个角色。与其祈祷RNG命中,不如循环一个字符串。

for (k = 0; a[k] != '[=12=]'; k++)
{
    n = rand() % k;
}
for (m = 0; b[m] != '[=12=]'; m++)
{
    o = rand() % m;
}

上面的代码将执行,并且每个 for 循环只产生 1 个输出,由于它的随机性,我无法判断它会导致哪个结果。

for (p = 0; p < j; p++)
{
    if (a[n] == b[o])
    {
    printf("YES");
    }
    else
    {
        printf("NO");
    }
}

这个 for 循环将执行 i 次,因为 j 的当前值将是 i 作为之前执行的第一个 for 循环。由于上述原因,这些循环中的每一个都会比较相同的 a[n]b[o]。所以结果将是 YESxiNOxi。不管字符串是什么。

希望这能解释你的代码有什么问题。

目前还不清楚您试图通过 rand() 函数实现什么,但您现在肯定需要找到不同的排列来实现这一目标。字符串 s1 的排列应等于字符串 s2,这意味着字符串 s1 中的所有字符都应出现在 s2 中,并且两个字符串中每个字符的计数应相同

这是一个工作版本:

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

 //CHECKING IF STRING TWO IS ANY PERMUTATION OF STRING ONE

 int main()
 {
     char str_one[]="abbcd";
     char str_two[]="bcab";
     int  arr[26]={0};
     int index=0;
     int len_one=strlen(str_one);
     int len_two=strlen(str_two);
     int val;

     if(len_one!=len_two)
     {
          printf("NO");
          exit(0);
     }

     while(index<len_one)
     {
         ++arr[str_one[index++]-'a'];
     }
     index=0;
     while(index<len_two)
     {
         --arr[str_two[index++]-'a'];
         if(arr[str_two[index]-'a']<0)
         {
             printf("NO");
             exit(0);
         }
     }
     index=0;

     while(index<26)
     {
         if(arr[index]!=0)
         {
           printf("NO");
           exit(0);
         }
         ++index;
     }

     printf("yes");

     return 0;
 }