[CareerCup] 17.9 Word Frequency in a Book 书中单词频率

17.9 Design a method to find the frequency of occurrences of any given word in a book.

这道题让我们找书中单词出现的频率,那么首先需要搞清楚的问题是,只需要统计一个单词,还是多个单词。如果是一个单词的话,那直接就遍历所有单词直接统计即可,如果是多个,就需要建立哈希表来建立每个单词和其出现次数之间的映射,然后再来查找即可,参见代码如下:

unordered_map<string, int> make_dictionary(vector<string> book) {
    unordered_map<string, int> res;
    for (auto word : book) {
        for (auto &a : word) a = tolower(a);
        ++res[word];
    }
    return res;
}

int get_frequency(unordered_map<string, int> m, string word) {
    if (m.empty() || word.empty()) return -1;
    for (auto &a : word) a = tolower(a);
    return m[word];
}

CareerCup All in One 题目汇总

时间: 2024-08-01 07:55:24

[CareerCup] 17.9 Word Frequency in a Book 书中单词频率的相关文章

Word frequency analysis

Write a program that reads a file, breaks each line into words, scripts whitespace and punctuation from the words, and converts them to lowercase. Modify the program to print the 20 most frequently-used words in the book. First I downloaded the e-boo

192. Word Frequency

192. Word Frequency QuestionEditorial Solution My Submissions Total Accepted: 5272 Total Submissions: 20228 Difficulty: Medium Write a bash script to calculate the frequency of each word in a text file words.txt. For simplicity sake, you may assume:

[Bash]LeetCode192..统计词频 | Word Frequency

Write a bash script to calculate the frequency of each word in a text file words.txt. For simplicity sake, you may assume: words.txt contains only lowercase characters and space ' ' characters. Each word must consist of lowercase characters only. Wor

使用word和pdf进行仿书编辑的经验

一.问题的提出:    一本书扫描好,要将书中的图片转换为文字版的word文档.二.问题的分析:    1.文字的提取    2.文字的编排三.问题的解决    1.如果用的是Adobe Acrobat 8 Professional      那么,扫描好的pdf文档,选定某一页,      文档→OCR文本识别→使用OCR识别文本      弹出识别文本对话框,注意要选择好识别的主要语言      在弹出的对话框中有个编辑按钮,点击“编辑”      弹出一个新的对话框窗体,将OCR识别的主要

教您如何在Word的mathtype加载项中修改章节号

在MathType数学公式编辑器中,公式编号共有五部分内容:分别是章编号(Chapter Number).节编号(Section Number).公式编号(Equation Number).括号(Enclosure). 分隔符(Separator).这五部分内容可以进行自由切换,以实现不同的需要.本文将以Microsoft office为例,教大家如何在word的mathtype加载项中修改章节号. 公式编号的设置方法: 1.单击word文档中的MathType加载项,点击第三列中插入编号的倒置

怎么才能将Word文档插入到CAD中?

怎么才能将Word文档插入到CAD中?在日常的编辑CAD图纸的过程中,建筑设计师们会遇到许许多多的问题,但是这些问题有必须去解决它,对于CAD小白来说这也是一项非常困难的工作,比如说怎么才能将Word文档插入到CAD中?具体要怎么操作才能实现了?下面小编就来教教大家在迅捷CAD编辑器专业版中怎么才能将Word文档插入到CAD中?想要了解的朋友就一起来看看吧! 第一步:打开任意一个浏览器,在浏览器的搜索框中搜索迅捷CAD编辑器专业版,然后进入到迅捷CAD的官网,进去之后点击下载安装最新版本的CAD

Egret入门学习日记 --- 第二十五篇(书中 9.16~9.17 节 内容)

第二十五篇(书中 9.16~9.17 节 内容) 对于昨天的关于 List 组件使用的问题,我打算到书中提到List之后,再回头补充. 还有就是 Scroller 的 TileLayout 布局方式,也要去研究一下. 好了,开始按照书中内容一步一步走. 开始 9.16节. 重点: 1.设定TabBar皮肤. 2.设置TabBar布局. 操作: 1.设定TabBar皮肤. 第一步,准备素材! 第二步,创建 exml 文件! 第三步,拖入组件!约束大小! 第四步,增加两个状态 down 和 up.

[CareerCup] 18.10 Word Transform 单词转换

18.10 Given two words of equal length that are in a dictionary, write a method to transform one word into another word by changing only one letter at a time. The new word you get in each step must be in the dictionary. 这道题让我们将一个单词转换成另一个单词,每次只能改变一个字母,

[CareerCup] 17.1 Swap Number In Place 互换位置

17.1 Write a function to swap a number in place (that is, without temporary variables). 这道题让我们交换两个数,但是不能用额外空间,那么我们可以先做差值,存入a中,然后再加上b,存入b中,那么此时的b即为原来的a,因为整个相当于做了一个a - b + b的过程,那么现在b是原来的a,而a中现在保存的是差值,,那么原来的b值就可以通过b-a来得到,保存到a中即可: 解法一: void swap(int a, i