题目描述:

Given a paragraph and a list of banned words, return the most frequent word that is not in the list of banned words.  It is guaranteed there is at least one word that isn't banned, and that the answer is unique.

Words in the list of banned words are given in lowercase, and free of punctuation.  Words in the paragraph are not case sensitive.  The answer is in lowercase.

Example:
Input:
paragraph = "Bob hit a ball, the hit BALL flew far after it was hit."
banned = ["hit"]
Output: "ball"
Explanation:
"hit" occurs 3 times, but it is a banned word.
"ball" occurs twice (and no other word does), so it is the most frequent non-banned word in the paragraph.
Note that words in the paragraph are not case sensitive,
that punctuation is ignored (even if adjacent to words, such as "ball,"),
and that "hit" isn't the answer even though it occurs more because it is banned.

Note:

  • 1 <= paragraph.length <= 1000.
  • 1 <= banned.length <= 100.
  • 1 <= banned[i].length <= 10.
  • The answer is unique, and written in lowercase (even if its occurrences in paragraph may have uppercase symbols, and even if it is a proper noun.)
  • paragraph only consists of letters, spaces, or the punctuation symbols !?',;.
  • Different words in paragraph are always separated by a space.
  • There are no hyphens or hyphenated words.
  • Words only consist of letters, never apostrophes or other punctuation symbols.

要完成的函数:

string mostCommonWord(string paragraph, vector<string>& banned)

说明:

1、这道题目给定一个字符串,里面是一个句子,包含了字母(大小写都有)和空格、符号,还给了一个禁用词的vector(小写),要求我们对字符串里面的单词做词频分析,找到出现次数最多的单词,返回这个单词。

2、明白题意,这道题很容易做,也是一道工程类题目。

首先,对字符串中的字符逐个判断,如果是字母,转化为小写形式,记录位置为 i ,继续处理下一个,直到元素不是字母,记录位置 j ,把 i 到 j -1的子字符串放在vector中。

然后,对vector中的单词逐个判断,如果不是禁用词,那么累加次数。这里要使用set.count()来判断是不是禁用词,和map的数据结构来存储单词和对应的出现次数。

最后,遍历一遍map,不断更新出现的最大次数,顺便记录对应的元素,最终返回元素就可以了。

代码如下:

    string mostCommonWord(string paragraph, vector<string>& banned)
{
int s1=paragraph.size(),i=0,j;
vector<string>words;
while(i<s1)
{
if(isalpha(paragraph[i]))
{
j=i+1;
paragraph[i]=tolower(paragraph[i]);//转化为小写字母
while(isalpha(paragraph[j]))//j不断前进
{
paragraph[j]=tolower(paragraph[j]);
j++;
}
words.push_back(paragraph.substr(i,j-i));//提取子字符串,插入到vector中
i=(j+1);//更新i的值
}
else
i++;
}
set<string>banwords(banned.begin(),banned.end());//把禁用词vector转化为set,快速判断
map<string,int>wordnum;//定义一个map来存储单词和出现次数
for(auto word:words)//记录每个单词的出现次数
{
if(banwords.count(word)==0)
wordnum[word]++;
}
int max1=0;
string res;
for(map<string,int>::iterator iter=wordnum.begin();iter!=wordnum.end();iter++)
{
if(iter->second>max1)//不断更新max1和对应的单词
{
max1=iter->second;
res=iter->first;
}
}
return res;
}

上述代码实测7ms,因为服务器接收到的cpp submissions有限,所以没有打败的百分比。

最新文章

  1. Windows bat脚本学习(1)
  2. Mysql Sql语句令某字段值等于原值加上一个字符串
  3. [转]html js中name和id的区别和使用分析
  4. 查看maven项目的依赖关系 mvn dependency:tree
  5. JS的加载方式---同步和异步
  6. chrom浏览器避免弹出“确定要离开此面吗?”提示框
  7. 【 D3.js 选择集与数据详解 — 5 】 处理模板的应用
  8. SQLSERVER2008 18456错误
  9. SQL连接方式(内连接,外连接,交叉连接)
  10. 网易云课堂_程序设计入门-C语言_第四周:循环控制_2念整数
  11. Ajax前后台交互函数
  12. 深入理解.net - 3.类型Type
  13. .NET Core实战项目之CMS 第六章 入门篇-Vue的快速入门及其使用
  14. Linux基础入门-文件打包与解压缩
  15. MT【249】离心率两题
  16. 根据传入的文件名称动态从moglifs图片服务器拿到pdf文档并在线浏览
  17. vue线上项目,优化前后对比
  18. Angular环境搭建
  19. Struts2(五)数据校验
  20. js的事件学习笔记

热门文章

  1. Bash CookBook(一)--基础
  2. Opencv convertScaleAbs
  3. leetcode array解题思路
  4. Android中的Handler介绍
  5. [C++] How to prevent memory leaks
  6. array_column()
  7. vs2012安装qt5.5.1
  8. Excel分类汇总与数据有效性
  9. CodeForces 682A Alyona and Numbers (水题,数学)
  10. kalilinux-信息搜集