题目描述
小王设计了一个简单的猜字谜游戏,游戏的谜面是一个错误的单词,比如nesw,玩家需要猜出谜底库中正确的单词。猜中的要求如下:
对于某个谜面和谜底单词,满足下面任一条件都表示猜中:
- 变换顺序以后一样的,比如通过变换w和e的顺序,“nwes”跟“news”是可以完全对应的;
- 字母去重以后是一样的,比如“woood”和“wood”是一样的,它们去重后都是“wod”
请你写一个程序帮忙在谜底库中找到正确的谜底。谜面是多个单词,都需要找到对应的谜底,如果找不到的话,返回”not found”
输入描述
- 谜面单词列表,以“,”分隔
- 谜底库单词列表,以","分隔
输出描述
- 匹配到的正确单词列表,以","分隔
- 如果找不到,返回"not found"
备注
- 单词的数量N的范围:0 < N < 1000
- 词汇表的数量M的范围:0 < M < 1000
- 单词的长度P的范围:0 < P < 20
- 输入的字符只有小写英文字母,没有其他字符
用例
输入 | conection connection,today |
输出 | connection |
说明 | 无 |
输入 | bdni,wooood bind,wrong,wood |
输出 | bind,wood |
说明 | 无 |
问题解析
题目描述了一个简单的猜单词游戏,玩家需要根据一个错误的单词从词库中猜出正确的单词。对于某个错误单词和词库中的单词,如果满足以下任一条件,则认为它们相匹配:
- 变换顺序后相同,例如“nwes”和“news”。
- 字母集合相同,例如“woood”和“wood”。
我们需要编写一个程序,输入错误单词列表和词库,输出所有匹配的正确单词。如果找不到匹配的正确单词,则输出“not found”。
输入输出
输入:
- 迷面单词列表,多个单词以逗号分隔。
- 迷底库单词列表,多个单词以逗号分隔。
输出:
- 匹配到的正确单词列表,以逗号分隔。
- 如果找不到,返回“not found”。
C语言实现
我们可以将每个单词按字母排序,这样“错误单词”和“正确单词”经过排序后如果相同,则认为它们匹配。可以使用哈希表来加速查找匹配的正确单词。
代码实现
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_WORD_LENGTH 100
#define MAX_WORDS 1000
void sort_string(char *str) {
int len = strlen(str);
for (int i = 0; i < len - 1; i++) {
for (int j = i + 1; j < len; j++) {
if (str[i] > str[j]) {
char temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
}
}
int main() {
char miswords[MAX_WORDS][MAX_WORD_LENGTH];
char dictionary[MAX_WORDS][MAX_WORD_LENGTH];
char *misword_list = NULL;
char *dictionary_list = NULL;
size_t len = 0;
printf("请输入迷面单词列表,以','分隔:\n");
getline(&misword_list, &len, stdin);
printf("请输入迷底库单词列表,以','分隔:\n");
getline(&dictionary_list, &len, stdin);
int misword_count = 0;
char *token = strtok(misword_list, ",");
while (token != NULL) {
strcpy(miswords[misword_count++], token);
token = strtok(NULL, ",");
}
int dictionary_count = 0;
token = strtok(dictionary_list, ",");
while (token != NULL) {
strcpy(dictionary[dictionary_count++], token);
token = strtok(NULL, ",");
}
char sorted_miswords[MAX_WORDS][MAX_WORD_LENGTH];
char sorted_dictionary[MAX_WORDS][MAX_WORD_LENGTH];
for (int i = 0; i < misword_count; i++) {
strcpy(sorted_miswords[i], miswords[i]);
sort_string(sorted_miswords[i]);
}
for (int i = 0; i < dictionary_count; i++) {
strcpy(sorted_dictionary[i], dictionary[i]);
sort_string(sorted_dictionary[i]);
}
int found = 0;
for (int i = 0; i < misword_count; i++) {
int match_found = 0;
for (int j = 0; j < dictionary_count; j++) {
if (strcmp(sorted_miswords[i], sorted_dictionary[j]) == 0) {
if (match_found == 0) {
printf("%s", dictionary[j]);
match_found = 1;
} else {
printf(",%s", dictionary[j]);
}
found = 1;
}
}
if (match_found == 0) {
if (found == 1) {
printf(",");
}
printf("not found");
}
if (i != misword_count - 1) {
printf(",");
}
}
printf("\n");
free(misword_list);
free(dictionary_list);
return 0;
}
代码说明:
- sort_string函数:对单词中的字符进行排序。
- main函数:
- 读取迷面单词列表和迷底库单词列表。
- 将每个单词按字母排序。
- 比较排序后的单词,找到匹配的正确单词。
- 输出匹配结果,如果没有匹配则输出“not found”。
Python 实现
def sort_string(s):
return ''.join(sorted(s))
def main():
# 读取输入
misword_list = input("请输入迷面单词列表,以','分隔:\n")
dictionary_list = input("请输入迷底库单词列表,以','分隔:\n")
miswords = misword_list.split(',')
dictionary = dictionary_list.split(',')
sorted_miswords = [sort_string(word) for word in miswords]
sorted_dictionary = [sort_string(word) for word in dictionary]
results = []
for i in range(len(miswords)):
match_found = False
matches = []
for j in range(len(dictionary)):
if sorted_miswords[i] == sorted_dictionary[j]:
matches.append(dictionary[j])
match_found = True
if match_found:
results.append(','.join(matches))
else:
results.append("not found")
# 输出结果
print(','.join(results))
if __name__ == "__main__":
main()
代码说明:
- sort_string函数:对单词中的字符进行排序。
- main函数:
- 读取迷面单词列表和迷底库单词列表。
- 将每个单词按字母排序。
- 比较排序后的单词,找到匹配的正确单词。
- 输出匹配结果,如果没有匹配则输出“not found”。
使用示例:
运行程序时,将按照下述步骤操作:
- 输入迷面单词列表,以逗号分隔。
- 输入迷底库单词列表,以逗号分隔。
- 程序将输出匹配的正确单词列表,如果没有匹配则输出“not found”。