华为OD机试真题 - 中文分词模拟器(Python/JS/C/C++ 2024 D卷 100分)

2024-09-03 08:52

本文主要是介绍华为OD机试真题 - 中文分词模拟器(Python/JS/C/C++ 2024 D卷 100分),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在这里插入图片描述

华为OD机试 2024E卷题库疯狂收录中,刷题点这里

专栏导读

本专栏收录于《华为OD机试真题(Python/JS/C/C++)》。

刷的越多,抽中的概率越大,私信哪吒,备注华为OD,加入华为OD刷题交流群,每一题都有详细的答题思路、详细的代码注释、3个测试用例、为什么这道题采用XX算法、XX算法的适用场景,发现新题目,随时更新,全天CSDN在线答疑。

一、题目描述

给定一个连续不包含空格字符的字符串,该字符串仅包含英文小写字母及英文标点符号(逗号、句号、分号),同时给定词库,对该字符串进行精确分词。

说明:

  • 精确分词:字符串分词后,不会出现重叠。例如 “ilovechina”,不同切分后可得到 “i”, “love”, “china”。
  • 标点符号不分词,仅用于断句。
  • 词库:根据常识及词库统计出来的常用词汇。例如:dictionary={“i”,“love”,“china”,“ilovechina”,“lovechina”}。
  • 分词原则:采用分词顺序优先且最长匹配原则。“ilovechina”,假设分词结果[i,ilove,lo,love,ch,china,lovechina] 则输出 [ilove,china]
    • 错误输出:[i, lovechina],原因:“ilove” > 优先于 “lovechina” 成词。
    • 错误输出:[i, love, china],原因:“ilove” > “i”,遵循最长匹配原则。

二、输入描述

  1. 字符串长度限制:0 < length < 256
  2. 词库长度限制:0 < length < 100000
  3. 第一行输入待分词语句 “ilovechina”
  4. 第二行输入中文词库 “i, love, china, ch, na, ve, lo, this, is, the, word”

三、输出描述

按顺序输出分词结果 “i, love, china”

1、输入

ilovechina
i,love,china,ch,na,ve,lo,this,is,the,word

2、输出

i,love,china

3、说明

输入的字符串被按最长匹配原则分为 “i”, “love”, “china”。

四、测试用例

1、输入

ilovech
i,love,china,ch,na,ve,lo,this,is,the,word

2、输出

i,love,ch

3、说明

输入的字符串被按最长匹配原则分为 “i”, “love”, “ch”。

五、解题思路

  1. 解析输入:
    • 读取待分词的字符串。
    • 读取词库,并将其转换为一个集合(Set),以便于快速查找。
  2. 处理标点符号:
    • 标点符号仅用于断句,不参与分词。可以使用正则表达式将字符串按标点符号分割。
  3. 分词处理:
    • 对每个子字符串进行分词,遵循最长匹配原则。
    • 从字符串的第一个字符开始,尝试匹配最长的单词,如果匹配成功,将该单词加入结果集,继续处理剩下的部分。
  4. 输出结果:
    • 将所有子字符串的分词结果组合起来,并按要求格式输出。

六、Python算法源码

import redef segment(sentence, dictionary):result = []length = len(sentence)start = 0while start < length:longest_word = None# 从当前起始位置向后查找for end in range(start + 1, length + 1):word = sentence[start:end]if word in dictionary:if longest_word is None or len(word) > len(longest_word):longest_word = wordif longest_word is not None:result.append(longest_word)start += len(longest_word)else:start += 1return resultdef main():# 读取待分词语句input_string = input("请输入待分词语句:")# 读取词库dictionary_input = input("请输入词库:")# 将词库字符串解析成集合,方便后续查找dictionary = set(dictionary_input.split(","))# 使用正则表达式按标点符号将输入字符串分割成多个子字符串sentences = re.split(r'[,.]', input_string)# 存储分词结果result = []# 对每个子字符串进行分词处理for sentence in sentences:result.extend(segment(sentence.strip(), dictionary))# 按要求格式输出分词结果print(", ".join(result))if __name__ == "__main__":main()

七、JavaScript算法源码

function segment(sentence, dictionary) {const result = [];const length = sentence.length;let start = 0;while (start < length) {let longestWord = null;// 从当前起始位置向后查找for (let end = start + 1; end <= length; end++) {const word = sentence.substring(start, end);if (dictionary.has(word)) {if (longestWord === null || word.length > longestWord.length) {longestWord = word;}}}if (longestWord !== null) {result.push(longestWord);start += longestWord.length;} else {start += 1;}}return result;
}function main() {// 读取待分词语句const input = prompt("请输入待分词语句:");// 读取词库const dictionaryInput = prompt("请输入词库:");// 将词库字符串解析成集合,方便后续查找const dictionary = new Set(dictionaryInput.split(","));// 使用正则表达式按标点符号将输入字符串分割成多个子字符串const sentences = input.split(/[,.;]/);// 存储分词结果const result = [];// 对每个子字符串进行分词处理for (const sentence of sentences) {result.push(...segment(sentence.trim(), dictionary));}// 按要求格式输出分词结果console.log(result.join(", "));
}// 调用主函数
main();

八、C算法源码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>#define MAX_WORD_LENGTH 100
#define MAX_SENTENCE_LENGTH 1000
#define MAX_DICTIONARY_SIZE 100// 字符串切割函数,类似于Python的split()
char** split(const char* str, const char* delim, int* count) {char* str_copy = strdup(str); // 复制字符串,以免修改原字符串char* token = strtok(str_copy, delim);char** result = malloc(MAX_SENTENCE_LENGTH * sizeof(char*));*count = 0;while (token != NULL) {result[(*count)++] = strdup(token);token = strtok(NULL, delim);}free(str_copy);return result;
}// 判断词库中是否包含某个单词
int is_in_dictionary(char* word, char dictionary[MAX_DICTIONARY_SIZE][MAX_WORD_LENGTH], int dict_size) {for (int i = 0; i < dict_size; i++) {if (strcmp(word, dictionary[i]) == 0) {return 1;}}return 0;
}// 分词函数,遵循最长匹配原则
void segment(char* sentence, char dictionary[MAX_DICTIONARY_SIZE][MAX_WORD_LENGTH], int dict_size, char result[MAX_SENTENCE_LENGTH][MAX_WORD_LENGTH], int* result_count) {int length = strlen(sentence);int start = 0;*result_count = 0;while (start < length) {char longest_word[MAX_WORD_LENGTH] = "";int longest_length = 0;for (int end = start + 1; end <= length; end++) {char word[MAX_WORD_LENGTH];strncpy(word, sentence + start, end - start);word[end - start] = '\0';if (is_in_dictionary(word, dictionary, dict_size) && strlen(word) > longest_length) {strcpy(longest_word, word);longest_length = strlen(word);}}if (longest_length > 0) {strcpy(result[*result_count], longest_word);(*result_count)++;start += longest_length;} else {start++;}}
}int main() {char input[MAX_SENTENCE_LENGTH];char dictionary_input[MAX_SENTENCE_LENGTH];char dictionary[MAX_DICTIONARY_SIZE][MAX_WORD_LENGTH];int dict_size = 0;// 读取待分词语句printf("请输入待分词语句:\n");fgets(input, MAX_SENTENCE_LENGTH, stdin);input[strcspn(input, "\n")] = '\0';  // 去除换行符// 读取词库printf("请输入词库:\n");fgets(dictionary_input, MAX_SENTENCE_LENGTH, stdin);dictionary_input[strcspn(dictionary_input, "\n")] = '\0';  // 去除换行符// 将词库字符串解析成二维数组int word_count;char** words = split(dictionary_input, ",", &word_count);for (int i = 0; i < word_count; i++) {strcpy(dictionary[dict_size++], words[i]);free(words[i]);}free(words);// 使用标点符号将输入字符串分割成多个子字符串int sentence_count;char** sentences = split(input, ",.;", &sentence_count);// 存储分词结果char result[MAX_SENTENCE_LENGTH][MAX_WORD_LENGTH];int result_count;// 对每个子字符串进行分词处理for (int i = 0; i < sentence_count; i++) {segment(sentences[i], dictionary, dict_size, result, &result_count);for (int j = 0; j < result_count; j++) {if (i > 0 || j > 0) {printf(", ");}printf("%s", result[j]);}free(sentences[i]);}free(sentences);printf("\n");return 0;
}

九、C++算法源码

#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <cstring>
#include <algorithm>using namespace std;// 字符串分割函数,类似于Python的split()
vector<string> split(const string &str, const string &delim) {vector<string> tokens;size_t prev = 0, pos = 0;do {pos = str.find_first_of(delim, prev);if (pos == string::npos) pos = str.length();string token = str.substr(prev, pos - prev);if (!token.empty()) tokens.push_back(token);prev = pos + 1;} while (pos < str.length() && prev < str.length());return tokens;
}// 判断词库中是否包含某个单词
bool is_in_dictionary(const string &word, const vector<string> &dictionary) {return find(dictionary.begin(), dictionary.end(), word) != dictionary.end();
}// 分词函数,遵循最长匹配原则
vector<string> segment(const string &sentence, const vector<string> &dictionary) {vector<string> result;size_t length = sentence.length();size_t start = 0;while (start < length) {string longest_word;size_t longest_length = 0;for (size_t end = start + 1; end <= length; ++end) {string word = sentence.substr(start, end - start);if (is_in_dictionary(word, dictionary) && word.length() > longest_length) {longest_word = word;longest_length = word.length();}}if (!longest_word.empty()) {result.push_back(longest_word);start += longest_length;} else {start++;}}return result;
}int main() {string input;string dictionary_input;// 读取待分词语句cout << "请输入待分词语句:" << endl;getline(cin, input);// 读取词库cout << "请输入词库:" << endl;getline(cin, dictionary_input);// 将词库字符串解析成集合,方便后续查找vector<string> dictionary = split(dictionary_input, ",");// 使用标点符号将输入字符串分割成多个子字符串vector<string> sentences = split(input, ",.;");// 存储分词结果vector<string> result;// 对每个子字符串进行分词处理for (const string &sentence : sentences) {vector<string> segmented = segment(sentence, dictionary);result.insert(result.end(), segmented.begin(), segmented.end());}// 按要求格式输出分词结果for (size_t i = 0; i < result.size(); ++i) {if (i > 0) {cout << ", ";}cout << result[i];}cout << endl;return 0;
}

🏆下一篇:华为OD机试真题 - 简易内存池(Python/JS/C/C++ 2024 E卷 200分)

🏆本文收录于,华为OD机试真题(Python/JS/C/C++)

刷的越多,抽中的概率越大,私信哪吒,备注华为OD,加入华为OD刷题交流群,每一题都有详细的答题思路、详细的代码注释、3个测试用例、为什么这道题采用XX算法、XX算法的适用场景,发现新题目,随时更新,全天CSDN在线答疑。

在这里插入图片描述

这篇关于华为OD机试真题 - 中文分词模拟器(Python/JS/C/C++ 2024 D卷 100分)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1132560

相关文章

使用Python创建一个功能完整的Windows风格计算器程序

《使用Python创建一个功能完整的Windows风格计算器程序》:本文主要介绍如何使用Python和Tkinter创建一个功能完整的Windows风格计算器程序,包括基本运算、高级科学计算(如三... 目录python实现Windows系统计算器程序(含高级功能)1. 使用Tkinter实现基础计算器2.

Python开发文字版随机事件游戏的项目实例

《Python开发文字版随机事件游戏的项目实例》随机事件游戏是一种通过生成不可预测的事件来增强游戏体验的类型,在这篇博文中,我们将使用Python开发一款文字版随机事件游戏,通过这个项目,读者不仅能够... 目录项目概述2.1 游戏概念2.2 游戏特色2.3 目标玩家群体技术选择与环境准备3.1 开发环境3

Python中模块graphviz使用入门

《Python中模块graphviz使用入门》graphviz是一个用于创建和操作图形的Python库,本文主要介绍了Python中模块graphviz使用入门,具有一定的参考价值,感兴趣的可以了解一... 目录1.安装2. 基本用法2.1 输出图像格式2.2 图像style设置2.3 属性2.4 子图和聚

Python使用Matplotlib绘制3D曲面图详解

《Python使用Matplotlib绘制3D曲面图详解》:本文主要介绍Python使用Matplotlib绘制3D曲面图,在Python中,使用Matplotlib库绘制3D曲面图可以通过mpl... 目录准备工作绘制简单的 3D 曲面图绘制 3D 曲面图添加线框和透明度控制图形视角Matplotlib

一文教你Python如何快速精准抓取网页数据

《一文教你Python如何快速精准抓取网页数据》这篇文章主要为大家详细介绍了如何利用Python实现快速精准抓取网页数据,文中的示例代码简洁易懂,具有一定的借鉴价值,有需要的小伙伴可以了解下... 目录1. 准备工作2. 基础爬虫实现3. 高级功能扩展3.1 抓取文章详情3.2 保存数据到文件4. 完整示例

使用Python实现IP地址和端口状态检测与监控

《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

基于Python打造一个智能单词管理神器

《基于Python打造一个智能单词管理神器》这篇文章主要为大家详细介绍了如何使用Python打造一个智能单词管理神器,从查询到导出的一站式解决,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 项目概述:为什么需要这个工具2. 环境搭建与快速入门2.1 环境要求2.2 首次运行配置3. 核心功能使用指

Python实现微信自动锁定工具

《Python实现微信自动锁定工具》在数字化办公时代,微信已成为职场沟通的重要工具,但临时离开时忘记锁屏可能导致敏感信息泄露,下面我们就来看看如何使用Python打造一个微信自动锁定工具吧... 目录引言:当微信隐私遇到自动化守护效果展示核心功能全景图技术亮点深度解析1. 无操作检测引擎2. 微信路径智能获

C#如何调用C++库

《C#如何调用C++库》:本文主要介绍C#如何调用C++库方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录方法一:使用P/Invoke1. 导出C++函数2. 定义P/Invoke签名3. 调用C++函数方法二:使用C++/CLI作为桥接1. 创建C++/CL

Python中pywin32 常用窗口操作的实现

《Python中pywin32常用窗口操作的实现》本文主要介绍了Python中pywin32常用窗口操作的实现,pywin32主要的作用是供Python开发者快速调用WindowsAPI的一个... 目录获取窗口句柄获取最前端窗口句柄获取指定坐标处的窗口根据窗口的完整标题匹配获取句柄根据窗口的类别匹配获取句