【问题标题】:Compare strings with a wildcard operator (*) and randomized order — Anagrams [closed]将字符串与通配符 (*) 和随机顺序进行比较 — Anagrams [关闭]
【发布时间】:2017-02-22 06:07:36
【问题描述】:

我有两个字符串,我想知道它们是否相等。然而,字符串的字符顺序是随机的。此外,某些字符可能已被通配符 (*) 替换。我正在使用它进行 Anagram 检测。


在这种情况下,我正在尝试获取字谜程序,我不得不说 ab** 是 abba 的字谜。现在,如果它实际上是像 abba 和 bbaa 这样的字谜,它可以判断它是否是字谜。现在我正在尝试弄清楚如何实现通配符 *,但我不知道从哪里开始,请帮忙!

到目前为止我有什么:

#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#define SIZE 5



bool areAnagram(char *str1, char *str2)
{

    int count[SIZE] = {0};
    int i = 0;

    for (i = 0; str1[i] && str2[i];  i++)
    {
        count[str1[i]]++;
        count[str2[i]]--;
    }

    if (str1[i] || str2[i])
    {
         return false;
    }



    for (i = 0; i < SIZE; i++)
    {
        if (count[i])
        {
            return false;
        }
    }

     return true;
}

int main()
{
    char str1[SIZE], str2[SIZE];

    FILE *finput;

    finput = fopen("input.txt", "r");

    fscanf(finput, "%s %s", str1, str2);

    printf("%s\n", str1);
    printf("%s\n", str2);

  if(areAnagram(str1, str2))
  {
        printf("THEY ARE ANAGRAMS\n");
  }
  else
  {
      printf("THEY AREN'T ANAGRAMS\n");
  }
}

【问题讨论】:

  • @TessellatingHeckler abba 和 bbaa 是字谜,这是 input.txt 设置的内容
  • 哦,现在我知道你的意思了。是的,这是一个损坏的代码,哎呀 brb
  • abaa baba - THEY ARE ANAGRAMS - 不,他们还没有?您正在初始化 count[SIZE] 一个 5 元素数组,索引为 0、1、2、3、4。然后你用count[str1[i]] 对其进行索引——str1 是一个字符数组,所以字母a 将是ASCII 字符号97,所以你正在做一个0-4 的数组的count[97]。我认为您正在咀嚼随机未分配的内存。然后你尝试在count[i]的五个地方寻找结果。如果您将计数设置得足够大并更改了计数器,这种方法可能会奏效,但现在它从根本上被打破了。

标签: c


【解决方案1】:

类似于选择排序算法的工作原理,但我不会检查整数值来排列,而是使用字符串比较来选择/删除并为 (*) 添加异常。遍历整个列表和中提琴!

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

int main() {

    char inputString[] = "AB**";//input string (with asterisks)
    char comparisonString[] = "AYYB";//comparison string (without asterisks)

    int inputString_length = strlen(inputString);
    int comparisonString_length = strlen(comparisonString);

    int anagram = 1;//boolean

    if (inputString_length != comparisonString_length) {
        anagram = 0;
    } else {

        int i = 0;
        while ((i < inputString_length) && (anagram == 1)) {

            char *letterToCheck = inputString[0];
            memmove(&inputString[0], &inputString[0 + 1], strlen(inputString) - 0);//remove first character

            int j = 0;
            int comparisonString_length_new = strlen(comparisonString);
            int matchFound = 0;//boolean
            while ((j < comparisonString_length_new) && (matchFound == 0)) {
                char *letterToCompare = comparisonString[j];

                if (letterToCheck == '*') {
                    matchFound = 1;
                }

                if (letterToCheck == letterToCompare) {
                    matchFound = 1;
                    memmove(&comparisonString[j], &comparisonString[j + 1], strlen(comparisonString) - j);//remove matched character
                }

                j++;
            }

            if (matchFound == 0) {
                anagram = 0;
            }


            i++;
        }

    }


    if (anagram == 0) {
        printf("Are NOT Anagrams");
    } else {
        printf("Are Anagrams");
    }

}

输入和比较字符串都可能包含*的另一种解决方案(注意:以下此解决方案由非现场用户添加,而不是OP,它也未经OP测试或验证)

#include <stdio.h>

#define CHAR_LEN 4
#define SPECIAL_CHARACTER '*'

int count_special_char(char *string) {
    int i = 0, count = 0;
    for(i = 0; i < CHAR_LEN; i++) {
        if(string[i] == SPECIAL_CHARACTER)
            count++;
    }
    return count;
}

int is_anagram(char *string_a, char *string_b) {
    int i, y;
    int found_count = 0;
    int a_special = count_special_char(string_a);
    int b_special = count_special_char(string_b);

    for(i = 0; i < CHAR_LEN; i++) {
        if(string_a[i] == SPECIAL_CHARACTER) //compare only non-asterisk char
            continue;

        for(y = 0; y < CHAR_LEN; y++) {
            if(string_a[i] == string_b[y]) {
                string_b[y] = '\0' //treat this char as found
                found_count++;
                break;
            }
        }
    }

    if((found_count + a_special + b_special) >= CHAR_LEN)
        return 1;
    else
        return 0;
}

int main() {
    char a[CHAR_LEN] = "**CD";
    char b[CHAR_LEN] = "AB**";

    if(is_anagram(a, b))
        printf("yes\n");
    else
        printf("no\n");

    return 0;
}

A*** 和 *XYZ 被假定为字谜,因为第一个字符串有 3 个 * 可以表示第二个字符串 XYZ。第二个字符串有1 * 可以代表第一个字符串A。如果有任何错误请指出并帮助。谢谢!

【讨论】:

  • 在第一个解决方案中,我在char *letterToCheck = str1[0]; 收到一个错误,说它是一个不兼容的整数到指针转换初始化'char *'
  • @Vcoss 是不是直接复制粘贴到新项目中测试了?我只是将它复制粘贴到在线 C IDE 中,它工作正常
【解决方案2】:

在 Linux 和 Posix 系统上,您可能会使用通配相关函数。查看glob(7) 并查看glob(3)fnmatch(3)wordexp(3)

也请查看regcomp(3)

如果您需要自己编写,请阅读finite state machinescontext free grammarsregular grammarsregular expressionsparsing

【讨论】:

  • '*' OP 表示单个字符。不是吗?在正则表达式中,'*' 可以代表任意数量的字符。
猜你喜欢
  • 2019-02-14
  • 1970-01-01
  • 2021-08-27
  • 2014-11-08
  • 1970-01-01
  • 2015-01-18
  • 1970-01-01
  • 2018-10-04
  • 1970-01-01
相关资源
最近更新 更多