🍭 大家好这里是清隆学长 ,一枚热爱算法的程序员
✨ 本系列打算持续跟新华为OD-C/D卷的三语言AC题解
💻 ACM银牌🥈| 多次AK大厂笔试 | 编程一对一辅导
👏 感谢大家的订阅➕ 和 喜欢💗
📎在线评测链接
https://app5938.acapp.acwing.com.cn/contest/2/problem/OD1083
🌍 评测功能需要 ⇒ 订阅专栏 ⇐ 后私信联系清隆解锁~
🍓OJ题目截图
文章目录
- 📎在线评测链接
- 🍓OJ题目截图
- 🍿 英文单词联想
- 问题描述
- 输入格式
- 输出格式
- 样例输入 1
- 样例输出 1
- 样例解释 1
- 样例输入 2
- 样例输出 2
- 样例解释 2
- 数据范围
- 题解
- 参考代码
🍿 英文单词联想
问题描述
K小姐是一家科技公司的产品经理,她最近负责开发一款英文输入法。其中一个重要功能是单词联想,即根据用户输入的单词前缀,从已输入的英文语句中联想出用户想输入的单词,并按字典序输出联想到的单词序列。如果无法联想到任何单词,则输出用户输入的单词前缀。
需要注意以下几点:
- 英文单词联想时,区分大小写。
- 缩略形式如"don’t",判定为两个单词,“don"和"t”。
- 输出的单词序列,不能有重复单词,且只能是英文单词,不能有标点符号。
输入格式
输入为两行:
- 第一行输入一段由英文单词 w o r d word word 和标点符号组成的语句 s t r str str。
- 第二行为一个英文单词前缀 p r e pre pre。
其中 0 < w o r d . l e n g t h ( ) ≤ 20 0 < word.length() \leq 20 0<word.length()≤20, 0 < s t r . l e n g t h ( ) ≤ 1000 0 < str.length() \leq 1000 0<str.length()≤1000, 0 < p r e . l e n g t h ( ) ≤ 20 0 < pre.length() \leq 20 0<pre.length()≤20。
输出格式
输出符合要求的单词序列或单词前缀,存在多个时,单词之间以单个空格分割。
样例输入 1
I love you
He
样例输出 1
He
样例解释 1
从用户已输入英文语句"I love you"中提炼出"I"、“love”、“you"三个单词,接下来用户输入"He”,从已输入信息中无法联想到任何符合要求的单词,因此输出用户输入的单词前缀。
样例输入 2
The furthest distance in the world, ls not between life and death, But when I stand in front of you, Yet you don't know that I love you.
f
样例输出 2
front furthest
样例解释 2
从用户已输入英文语句"The furthest distance in the world, Is not between life and death, But when I stand in front of you, Yet you don’t know that I love you"中提炼出的单词,符合"f"作为前缀的有"furthest"和"front",按字典序排序并在单词间添加空格后输出,结果为"front furthest"。
数据范围
- 0 < w o r d . l e n g t h ( ) ≤ 20 0 < word.length() \leq 20 0<word.length()≤20
- 0 < s t r . l e n g t h ( ) ≤ 1000 0 < str.length() \leq 1000 0<str.length()≤1000
- 0 < p r e . l e n g t h ( ) ≤ 20 0 < pre.length() \leq 20 0<pre.length()≤20
题解
本题的关键在于如何从给定的英文语句中提取出所有单词,并根据单词前缀进行过滤和排序。我们可以使用正则表达式或字符串分割的方法将语句拆分为单词列表,然后遍历单词列表,找出以给定前缀开头的单词,并将它们按字典序排列输出。
如果无法找到任何符合要求的单词,则直接输出给定的单词前缀。需要注意的是,在处理缩略形式时,应将其拆分为多个单词进行处理。
参考代码
- Python
import redef soln(sentence, prefix):# 使用正则表达式提取所有单词words = re.findall(r'\w+', sentence.lower())# 过滤并排序符合前缀的单词filtered_words = sorted([word for word in words if word.startswith(prefix.lower())])# 如果没有符合要求的单词,输出前缀if not filtered_words:return prefixelse:return ' '.join(filtered_words)# 读取输入
sentence = input()
prefix = input()# 调用函数并输出结果
print(soln(sentence, prefix))
- Java
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;public class Main {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);String sentence = scanner.nextLine();String prefix = scanner.nextLine();System.out.println(soln(sentence, prefix));}public static String soln(String sentence, String prefix) {// 使用正则表达式提取所有单词Pattern pattern = Pattern.compile("\\w+");Matcher matcher = pattern.matcher(sentence.toLowerCase());List<String> words = new ArrayList<>();while (matcher.find()) {words.add(matcher.group());}// 过滤并排序符合前缀的单词List<String> filteredWords = new ArrayList<>();for (String word : words) {if (word.startsWith(prefix.toLowerCase())) {filteredWords.add(word);}}Collections.sort(filteredWords);// 如果没有符合要求的单词,输出前缀if (filteredWords.isEmpty()) {return prefix;} else {return String.join(" ", filteredWords);}}
}
- Cpp
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <regex>using namespace std;string soln(string sentence, string prefix) {// 使用正则表达式提取所有单词regex pattern("\\w+");sregex_iterator iter(sentence.begin(), sentence.end(), pattern);sregex_iterator end;vector<string> words;while (iter != end) {words.push_back(iter->str());++iter;}// 过滤并排序符合前缀的单词vector<string> filteredWords;for (const string& word : words) {if (word.substr(0, prefix.length()) == prefix) {filteredWords.push_back(word);}}sort(filteredWords.begin(), filteredWords.end());// 如果没有符合要求的单词,输出前缀if (filteredWords.empty()) {return prefix;} else {string result;for (const string& word : filteredWords) {result += word + " ";}result.pop_back(); // 去掉最后一个空格return result;}
}int main() {string sentence, prefix;getline(cin, sentence);getline(cin, prefix);cout << soln(sentence, prefix) << endl;return 0;
}