#P3072. 猜字谜(100分)
-
1000ms
Tried: 177
Accepted: 41
Difficulty: 5
所属公司 :
华为od
-
算法标签>模拟
猜字谜(100分)
模拟
对于两个字符串变换顺序后顺序是一样的,和字母去重以后是一样的表示猜中谜底。 那么可以直接排序去重判断是不是相等的数据只有1000那么两两匹配即可,枚举谜面的单词,在嵌套一层循环判断是不是跟谜底单词相等,相等输出即可,如上述没有输出,输出一个"not found".详细请见代码
代码如下
cpp
#include <bits/stdc++.h>
using namespace std;
signed main() {
string puzzle_input, word_list_input;
cin >> puzzle_input >> word_list_input;
vector<string> puzzle_words;
vector<string> dictionary_words;
vector<string> original_words;
// 解析谜面单词
for (int i = 0; i < puzzle_input.size(); i++) {
string current_word = "";
int j = i;
current_word += puzzle_input[i];
// 提取每个单词,以逗号分隔
while (puzzle_input[j + 1] != ',' && j + 1 < puzzle_input.size()) {
current_word += puzzle_input[j + 1];
j++;
}
// 去重并排序
sort(current_word.begin(), current_word.end());
current_word.erase(unique(current_word.begin(), current_word.end()), current_word.end());
puzzle_words.push_back(current_word);
i = j + 1;
}
// 解析谜底库单词
for (int i = 0; i < word_list_input.size(); i++) {
string current_word = "";
current_word += word_list_input[i];
int j = i;
// 提取每个单词,以逗号分隔
while (word_list_input[j + 1] != ',' && j + 1 < word_list_input.size()) {
current_word += word_list_input[j + 1];
j++;
}
// 保存原始的谜底单词
original_words.push_back(current_word);
// 去重并排序
sort(current_word.begin(), current_word.end());
current_word.erase(unique(current_word.begin(), current_word.end()), current_word.end());
dictionary_words.push_back(current_word);
i = j + 1;
}
// 对每个谜面单词进行匹配
for (int i = 0; i < puzzle_words.size(); i++) {
bool found = false;
if (i != 0) {
cout << ","; // 输出单词间的逗号分隔符
}
// 尝试在谜底库中匹配
for (int j = 0; j < dictionary_words.size(); j++) {
if (puzzle_words[i] == dictionary_words[j]) {
found = true;
cout << original_words[j]; // 输出匹配到的谜底单词
}
}
// 如果没有找到匹配的谜底
if (!found) {
cout << "not found";
}
}
return 0;
}
python
def process_input(input_str):
words = input_str.split(",") # 根据逗号分割字符串
processed_words = []
for word in words:
# 去重并排序
processed_word = ''.join(sorted(set(word)))
processed_words.append(processed_word)
return processed_words
def main():
# 读取两行输入,分别是谜面和谜底单词列表
puzzle_input = input().strip()
word_list_input = input().strip()
# 解析谜面和谜底单词
puzzle_words = process_input(puzzle_input)
word_list = word_list_input.split(",") # 原始的谜底单词列表
# 对谜底进行去重和排序处理
dictionary_words = process_input(word_list_input)
result = []
# 匹配谜面单词和谜底单词
for i in range(len(puzzle_words)):
found = False
for j in range(len(dictionary_words)):
if puzzle_words[i] == dictionary_words[j]:
result.append(word_list[j]) # 如果匹配,保存原始谜底单词
found = True
break
if not found:
result.append("not found") # 如果没有找到匹配的,返回"not found"
# 输出结果,使用逗号分隔
print(",".join(result))
if __name__ == "__main__":
main()
java
import java.util.*;
public class Main {
// 处理输入字符串,去重并排序每个单词
public static List<String> processInput(String input) {
String[] words = input.split(","); // 以逗号分隔单词
List<String> processedWords = new ArrayList<>();
for (String word : words) {
// 将字符串转为字符数组,去重、排序
char[] chars = word.toCharArray();
Set<Character> charSet = new TreeSet<>(); // 使用 TreeSet 去重并排序
for (char c : chars) {
charSet.add(c);
}
// 将排序好的字符重新组合成字符串
StringBuilder sortedWord = new StringBuilder();
for (char c : charSet) {
sortedWord.append(c);
}
processedWords.add(sortedWord.toString());
}
return processedWords;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 读取两行输入
String puzzleInput = scanner.nextLine();
String wordListInput = scanner.nextLine();
// 处理谜面单词和谜底单词
List<String> puzzleWords = processInput(puzzleInput);
List<String> wordList = Arrays.asList(wordListInput.split(",")); // 原始谜底单词
List<String> dictionaryWords = processInput(wordListInput); // 处理后的谜底单词
List<String> result = new ArrayList<>();
// 对每个谜面单词进行匹配
for (int i = 0; i < puzzleWords.size(); i++) {
boolean found = false;
for (int j = 0; j < dictionaryWords.size(); j++) {
if (puzzleWords.get(i).equals(dictionaryWords.get(j))) {
result.add(wordList.get(j)); // 匹配到时,添加原始谜底单词
found = true;
break;
}
}
if (!found) {
result.add("not found"); // 未找到时,添加 "not found"
}
}
// 使用逗号分隔输出结果
System.out.println(String.join(",", result));
scanner.close();
}
}
题目内容
小王设计了一个简单的猜字谜游戏,游戏的谜面是一个错误的单词,比如nesw,玩家需要猜出谜底库中正确的单词。猜中的要求如下: 对于某个谜面和谜底单词,满足下面任一条件都表示猜中:
- 变换顺序以后一样的,比如通过变换w和e的顺序,“nwes”跟“news”是可以完全对应的;
- 字母去重以后是一样的,比如“woood”和“wood”是一样的,它们去重后都是“wod”
请你写一个程序帮忙在谜底库中找到正确的谜底。谜面是多个单词,都需要找到对应的谜底,如果找不到的话,返回"not found"
输入描述
- 谜面单词列表,以“,”分隔
- 谜底库单词列表,以","分隔
输出描述
- 匹配到的正确单词列表,以","分隔
- 如果找不到,返回"notfound"
备注
- 单词的数量N的范围:0<N<1000
- 词汇表的数量M的范围:0<M<1000
- 单词的长度P的范围:0<P<20
- 输入的字符只有小写英文字母,没有其他字符
样例1
输入
conection
connection,today
输出
connection
说明
样例2
输入
bdni,wooood
bind,wrong,wood
输出
bind,wood
说明