c语言输入分为两部分。 第一部分是字典,每个单词占据一行,最后以仅包含'#'的一行表示结束。所有的单词都是不同的,字典中最多10000个单词。 输入的第二部分包含了所有待检测的单词,单词数目不超过50。每个单词占据一行,最后以仅包含'#'的一行表示结束。 字典中的单词和待检测的单词均由小写字母组成,并且单词最大长度为15。 输出 第一行输出二叉排序树字典的后序遍历,每一个单词后面跟一个空格。 然后按照检查次序每个单词输出一行,该行首先输出单词自身。如果单词在字典中出现,接着输出" is correct"。如果单词是错误的,那么接着输出':',如果字典中有建议修改单词,则按照字典中出现的先后次序输出所有的建议修改单词(每个前面都添加一个空格),如果无建议修改单词,在':'后直接换行
时间: 2024-03-17 07:47:44 浏览: 16
好的,以下是一个C语言的程序示例,可以实现上述功能:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_WORD_LEN 15
#define MAX_DICT_SIZE 10000
#define MAX_CHECK_WORDS 50
// 二叉排序树结点
typedef struct BSTNode {
char word[MAX_WORD_LEN + 1]; // 单词
struct BSTNode *left; // 左子树
struct BSTNode *right; // 右子树
} BSTNode;
// 将单词插入二叉排序树
void insert(BSTNode **root, char *word) {
if (*root == NULL) {
*root = (BSTNode*)malloc(sizeof(BSTNode));
strcpy((*root)->word, word);
(*root)->left = NULL;
(*root)->right = NULL;
} else if (strcmp(word, (*root)->word) < 0) {
insert(&(*root)->left, word);
} else if (strcmp(word, (*root)->word) > 0) {
insert(&(*root)->right, word);
}
}
// 后序遍历二叉排序树
void postorder(BSTNode *root) {
if (root == NULL) {
return;
}
postorder(root->left);
postorder(root->right);
printf("%s ", root->word);
}
// 检查单词是否在字典中出现
int check(BSTNode *root, char *word) {
if (root == NULL) {
return 0;
}
if (strcmp(word, root->word) == 0) {
return 1;
} else if (strcmp(word, root->word) < 0) {
return check(root->left, word);
} else {
return check(root->right, word);
}
}
// 比较两个单词的编辑距离
int edit_distance(char *word1, char *word2) {
int m = strlen(word1);
int n = strlen(word2);
int dp[m+1][n+1];
for (int i = 0; i <= m; i++) {
dp[i][0] = i;
}
for (int j = 0; j <= n; j++) {
dp[0][j] = j;
}
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (word1[i-1] == word2[j-1]) {
dp[i][j] = dp[i-1][j-1];
} else {
int replace = dp[i-1][j-1] + 1;
int insert = dp[i][j-1] + 1;
int delete = dp[i-1][j] + 1;
dp[i][j] = (replace < insert) ? replace : insert;
dp[i][j] = (dp[i][j] < delete) ? dp[i][j] : delete;
}
}
}
return dp[m][n];
}
// 查找建议修改单词
void suggest(BSTNode *root, char *word) {
if (root == NULL) {
return;
}
int dist = edit_distance(word, root->word);
if (dist == 1) {
printf(" %s", root->word);
}
suggest(root->left, word);
suggest(root->right, word);
}
int main() {
// 读入字典
BSTNode *root = NULL;
char dict_word[MAX_WORD_LEN + 1];
scanf("%s", dict_word);
while (strcmp(dict_word, "#") != 0) {
insert(&root, dict_word);
scanf("%s", dict_word);
}
// 读入待检测单词
char check_word[MAX_CHECK_WORDS][MAX_WORD_LEN + 1];
int check_word_num = 0;
scanf("%s", check_word[check_word_num]);
while (strcmp(check_word[check_word_num], "#") != 0) {
check_word_num++;
scanf("%s", check_word[check_word_num]);
}
// 输出字典的后序遍历
printf("Dictionary after sorting:\n");
postorder(root);
printf("\n");
// 检查单词并输出建议修改单词
printf("Check words:\n");
for (int i = 0; i < check_word_num; i++) {
printf("%s", check_word[i]);
if (check(root, check_word[i])) {
printf(" is correct\n");
} else {
printf(":");
suggest(root, check_word[i]);
printf("\n");
}
}
return 0;
}
```
这个程序会先读入字典,然后构建二叉排序树。然后再读入待检测的单词,依次检查每个单词是否在字典中出现,如果出现则输出"is correct",否则输出建议修改单词。最后输出字典的后序遍历。