欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 能源 > 【2024最新华为OD-C/D卷试题汇总】[支持在线评测] 英文单词联想(100分) - 三语言AC题解(Python/Java/Cpp)

【2024最新华为OD-C/D卷试题汇总】[支持在线评测] 英文单词联想(100分) - 三语言AC题解(Python/Java/Cpp)

2024/10/25 15:27:38 来源:https://blog.csdn.net/Qmtdearu/article/details/140020544  浏览:    关键词:【2024最新华为OD-C/D卷试题汇总】[支持在线评测] 英文单词联想(100分) - 三语言AC题解(Python/Java/Cpp)

🍭 大家好这里是清隆学长 ,一枚热爱算法的程序员

✨ 本系列打算持续跟新华为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小姐是一家科技公司的产品经理,她最近负责开发一款英文输入法。其中一个重要功能是单词联想,即根据用户输入的单词前缀,从已输入的英文语句中联想出用户想输入的单词,并按字典序输出联想到的单词序列。如果无法联想到任何单词,则输出用户输入的单词前缀。

需要注意以下几点:

  1. 英文单词联想时,区分大小写。
  2. 缩略形式如"don’t",判定为两个单词,“don"和"t”。
  3. 输出的单词序列,不能有重复单词,且只能是英文单词,不能有标点符号。

输入格式

输入为两行:

  • 第一行输入一段由英文单词 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;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com