819. Most Common Word
static int wing=[]()
{
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return ;
}(); class Solution
{
public:
string mostCommonWord(string paragraph, vector<string>& banned)
{
unordered_map<string,int> simap;
for(string s:banned)
simap[s]=-;
int beg=,flag=,max=;
paragraph.push_back(' ');
string res;
int sz=paragraph.length();
for(int i=;i<sz;i++)
{
if(isalpha(paragraph[i]))
paragraph[i]=tolower(paragraph[i]);
else if(paragraph[i]!=' ')
{
flag=;
continue;
}
else
{
string cur=paragraph.substr(beg,i-flag-beg);
simap[cur]++;
beg=i+;
flag=;
if(simap[cur]>max)
{
res=cur;
max=simap[cur];
}
}
}
return res;
}
};
以空格为标志,进行操作。
先把禁止序列扫进map,并将对应的字符串计数器置为-1000,由于段落最长就为1000,这样在扫描段落之后,禁止序列的字符串计数器就不可能为正数,就不会影响到非禁止序列高频单词的筛选。
观察段落序列可以发现,除了首个单词,每个单词前面都是空格,所以我们以空格为判定依据。
遇到字母时,操作只将字母转为小写
遇到非空格非字母,即遇到标点符号时,将标志位置为1,表示在下一个空格前有一个标点符号
遇到空格时,i 代表了空格的下标,beg代表了当前单词序列开始下标,即当前单词的第一个字母下标,用substr将单词提取出来,提取长度为 i-flag-1-beg+1=i-flag-beg
提取出来之后,将单词放入map,并增加计数器,然后比较计数器的值和最大值,进行相应操作。
操作之后,要将beg置为新单词的第一个字母,即i+1,将flag标志置为0。
这里还要注意一点,最后一个单词是以句号结尾的,末尾没有空格,循环不会操作最后一个单词,所以,为了让操作进行完整,在循环前,在段落后面补充了一个空格。
这个题用到了一点技巧性的东西,活用标志位,补充原字符串。
819. Most Common Word的更多相关文章
- 【Leetcode_easy】819. Most Common Word
problem 819. Most Common Word solution: class Solution { public: string mostCommonWord(string paragr ...
- LeetCode 819. Most Common Word (最常见的单词)
Given a paragraph and a list of banned words, return the most frequent word that is not in the list ...
- LeetCode 819. Most Common Word
原题链接在这里:https://leetcode.com/problems/most-common-word/description/ 题目: Given a paragraph and a list ...
- 819. Most Common Word 统计高频词(暂未被禁止)
[抄题]: Given a paragraph and a list of banned words, return the most frequent word that is not in the ...
- 【LeetCode】819. Most Common Word 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 正则+统计 日期 题目地址:https://leet ...
- leetcode Most Common Word——就是在考察自己实现split
819. Most Common Word Given a paragraph and a list of banned words, return the most frequent word th ...
- [Swift]LeetCode819. 最常见的单词 | Most Common Word
Given a paragraph and a list of banned words, return the most frequent word that is not in the list ...
- [LeetCode] Most Common Word 最常见的单词
Given a paragraph and a list of banned words, return the most frequent word that is not in the list ...
- 最常出现的字符串 Most Common Word
2018-10-26 00:32:05 问题描述: 问题求解: 方法一.Trie 最长出现的字符串,最容易想到的解法就是Trie树了,于是首先使用Trie树进行了实现,代码量有点大,当然了是可以A掉的 ...
随机推荐
- jdbc java远程连接mysql数据库服务器
首先,需要注意以下几点: 1.手机需要获得可以访问网络的权限: 2.导入的jdbc驱动的版本需要与mysql服务器的版本相近: 3.mysql默认的访客是只允许本机(localhost),不允许其他主 ...
- 什么是PCM?它和.wav文件是什么关系?
PCM(Pulse Code Modulation----脉码调制录音).所谓PCM录音就是将声音等模拟信号变成符号化的脉冲列,再予以记录.PCM信号是由[1].[0]等符号构成的数字信号,而未经过任 ...
- F - Restoring the Expression CodeForces - 898F
字符串hash: base设置为10 枚举'='可能出现的位置,从1/2处开始到大概1/3处结束,当然大概的1/3不用计算,直接到最后就行,因为本题必然有解,输出直接结束即可. 根据'='号位置,' ...
- 【Android端】代码打包成jar包/aar形式
Android端代码打包成jar包和aar形式: 首先,jar包的形式和aar形式有什么区别? 1.打包之后生成的文件地址: *.jar:库/build/intermediates/bundles/d ...
- TZOJ 3481 Highway Construction(树的直径+最短路)
描述 As head of the Accessible Commuting Movement (ACM), you've been lobbying the mayor to build a new ...
- window10安装黑苹果
一位大佬的分享笔记 启动盘https://github.com/Macrow/Hactonish clover的下载地址https://sourceforge.net/projects/clovere ...
- (二)创建ROS工作空间
ROS 教程中涉及的和编写的所有 ROS 代码需要有一个地方存放它,这个地方叫做工作空间.可以说,这个工作空间应该是一个大的集合.当我们要编译程序的时候,我们是对整个工作空间进行编译的. 这个工作空 ...
- WEB框架Django之ORM操作
一 ORM的简介 MVC或者MVC框架中包括的一个重要部分就是ORM,它实现了数据模型与数据库的解耦. 即数据模型的设计不需要依赖于特定的数据库,通过简单的配置可以轻松更换数据库,这可以大大减少开发人 ...
- webstorm 打包angular Module build failed: Error: No PostCSS Config found
angular创建项目后,在webstorm中启动时,报出如题错误,奇怪的是我从命令行启动(ng server)是没有问题的,多方寻求无果,在网上看到过说要加一个配置文件,我不信.我觉得是我配置哪里有 ...
- Android.Libraries
1. Android Dependencies, Referenced Libraries, Android Private Libraries Android Private Libraries - ...