简体   繁体   English

为什么我的程序无法识别字符串中的相似词?

[英]why can't my program recognize similar words in a string?

I want to write a program that will take an input T .我想编写一个接受输入T的程序。 In the next T lines, each line will take a string as an input.在接下来的T行中,每行将以一个字符串作为输入。 The output would be how many ways the string can be reordered. output 将是字符串可以重新排序的方式。

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

int main() {
    int T, i, l, count = 1, test = 0, word = 0, ans;
    char line[200];

    scanf("%d", &T);

    for (i = 0; i < T; i++) {
        scanf(" %[^\n]", line);
        l = strlen(line);
        for (int q = 0; q < l; q++) {
            if (line[q] == ' ') {
                word++;
            }
        }
        ans = fact(word + 1);
        word = 0;
        for (int j = 0; j < l; j++) {
            for (int k = j + 1; k < l; k++) {
                if (line[k] == ' ' && line[k + 1] == line[j]) {
                    int m = j;
                    int n = k + 1;
                    for (;;) {
                        if (line[m] != line[n]) {
                            break;
                        } else
                        if (line[m] == ' ' && line[n] == ' ') {
                            test = 1;
                            break;
                        } else {
                            m++;
                            n++;
                        }
                    }
                    if (test == 1) {
                        count++;
                        ans = ans / fact(count);
                        count = 0;
                        test = 0;
                    }
                }
            }
        }
        printf("%d\n", ans);
    }
}

int fact(int n) {
    if (n == 1) {
        return 1;
    } else {
        return n * fact(n - 1);
    }
}

Now, in my program,现在,在我的程序中,

my output is like this:我的 output 是这样的:

2
no way no good
12
yes no yes yes no
120

if T = 2 and the 1st string is no way no good , it gives the right output that is 12 (4./2,).如果T = 2并且第 1 个字符串no way no good ,它给出正确的 output 即12 (4./2,)。 That means, it has identified that there are two similar words.也就是说,它识别出了两个相似的词。

But in the 2nd input, the string is yes no yes yes no .但是在第二个输入中,字符串是yes no yes yes no that means 3 yes and 2 no s.这意味着 3 yes和 2 no s。 So the and should be 5!/(3!2!) = 10 .所以 和 应该是 5!/(3!2!) = 10 But why is the answer 120 ?但为什么答案是120 and why can't it recognize the similar words?为什么它不能识别相似的词?

The main problem in your duplicate detector is you test the end of word with if (line[m] == ' ' && line[n] == ' ') but this test fails to identify a duplicate that occurs with the last word because line[n] is '\0' , not ' ' .重复检测器中的主要问题是您使用if (line[m] == ' ' && line[n] == ' ')测试单词的结尾,但此测试无法识别最后一个单词出现的重复,因为line[n]'\0' ,而不是' '

Note these further problems:请注意这些进一步的问题:

  • you do not handle words that occur more than twice correctly: you should perform ans = ans / fact(count);你没有正确处理出现两次以上的单词:你应该执行ans = ans / fact(count); only after the outer loop finishes.仅在外循环完成后。 For example, if a word is present 3 times, it will be detected as 3 pairs of duplicates, effectively causing ans to be divided by 2 3 = 8, instead of 3. = 6.例如,如果一个词出现 3 次,它将被检测为 3 对重复项,有效地导致ans除以 2 3 = 8,而不是 3. = 6。

  • you should protect against buffer overflow and detect invalid input with:您应该防止缓冲区溢出并检测无效输入:

     if (scanf(" %199[^\n]", line);= 1) break;
  • the range of type int for ans is too small for a moderately large number of words: 13, is 6227020800, larger than INT_MAX on most systems. ansint类型范围对于中等数量的单词来说太小:13,是 6227020800,在大多数系统上大于INT_MAX

The code is difficult to follow.代码很难遵循。 You should consider parsing the line into an array of words and using a more conventional way of counting duplicates.您应该考虑将该行解析为一个单词数组,并使用更传统的方法来计算重复项。

Here is a modified version using this approach:这是使用这种方法的修改版本:

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

static int cmpstr(const void *p1, const void *p2) {
    char * const *pp1 = p1;
    char * const *pp2 = p2;
    return strcmp(*pp1, *pp2);
}

unsigned long long factorial(int n) {
    unsigned long long res = 1;
    while (n > 1)
        res *= n--;
    return res;
}

int main() {
    int T, i, n, begin, count;
    unsigned long long ans;
    char line[200];
    char *words[100];

    if (!fgets(line, sizeof line, stdin) || sscanf(line, "%d", &T) != 1)
        return 1;

    while (T --> 0) {
        if (!fgets(line, sizeof line, stdin))
            break;
        n = 0;
        begin = 1;
        for (char *p = line; *p; p++) {
            if (isspace((unsigned char)*p)) {
                *p = '\0';
                begin = 1;
            } else {
                if (begin) {
                    words[n++] = p;
                    begin = 0;
                }
            }
        }
        qsort(words, n, sizeof(*words), cmpstr);
        ans = factorial(n);
        for (i = 0; i < n; i += count) {
            for (count = 1; i + count < n && !strcmp(words[i], words[i + count]); count++)
                continue;
            ans /= factorial(count);
        }
        printf("%llu\n", ans);
    }
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的程序无法识别 PORTbits.RCx == 0 条件? - Why doesn't my program recognize the PORTbits.RCx == 0 condition? GDB为什么不识别我的C程序的语言? - Why doesn't GDB recognize the language of my C program? 检查字符串中的单词是否为回文的代码。 无法识别短语末尾的单词 - Code that checks if a words from string is a palindrome. Can't recognize words at the end of the phrase 为什么我的程序无法计算字符串中的单词? - Why does my program not work for counting the words in a string? 为什么lex无法识别我的正则表达式定义 - Why can't lex recognize my regular expression definitions 为什么我的程序无法识别 function? - Why does my program not recognize the function? 为什么我的程序输出的字数不正确? - Why is my program not outputting the right count of words? Pocketsphinx 无法有效识别通过麦克风录制的单词(命令) - pocketsphinx can't efficiently recognize words (commands) recorded via mic Pthread 生产者/消费者作业问题。 你能告诉我为什么我的程序没有将文件中的所有单词都写入 output 吗? - Pthread producer/consumer homework problem. Can you tell me why my program isn't writing all the words from the file to output? 为什么我的程序无法识别另一个命令? - How come my program won't recognize another command?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM