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掉的 ...
随机推荐
- 关于RAID的概述
Raid 0:一块硬盘或者以上就可做raid0优势:数据读取写入最快,最大优势提高硬盘容量,比如3快80G的硬盘做raid0 可用总容量为240G.速度是一样.缺点:无冗余能力,一块硬盘损坏,数据全无 ...
- TZOJ 3665 方格取数(2)(最大点权独立集)
描述 给你一个m*n的格子的棋盘,每个格子里面有一个非负数. 从中取出若干个数,使得任意的两个数所在的格子没有公共边,就是说所取数所在的2个格子不能相邻,并且取出的数的和最大. 输入 包括多个测试实例 ...
- django的中间件:process_request|process_response|process_view|process_exception
MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware. ...
- python3之subprocess常见方法使用
一.常见subprocess方法 1.subprocess.getstatusoutput(cmd) 官方解释: Return (exitcode, output) of executing cmd ...
- Activity 的启动过程深入学习
手机应用也是一个app,每一个应用的icon都罗列在Launcher上,点击icon触发onItemClick事件. 我们要启动「淘宝」这个App,首先我们要在清单文件定义默认启动的Activity信 ...
- 11-web网页制作APP
如何将H5和WebApp 加壳成apk.ipa 问题:已经做好的纯H5的站点 想分别加两个壳子,变成apk和ipa ,要怎么实现? 要点: 1. app只是壳子,打开app直接跳转到H5的Ur ...
- 四元数运动学笔记(5)IMU驱动的运动误差方程
1.扩展卡尔曼滤波EKF1.1线性化卡尔曼滤波1.2偏差微分方程的推导1.3线性化卡尔曼滤波的流程1.4 离散EKF2.误差状态的运动方程2.1连续时间的IMU系统动态方程2.1.1相关变量2.1.2 ...
- c# sharpsvn 客户端开发总结
判断文件夹是否是 working copy 工作副本两个方法 if (!IsWorkingCopy(localPath))//查看某目录是否是受svn管理的状态, 即是否为工作副本 { notiny ...
- Attribute Syntax
Attribute Syntax This section describes the syntax with which __attribute__ may be used, and the con ...
- Liunx Mkdir
linux mkdir命令: 创建目录 介绍:该命令创建指定的目录名,要求创建目录的用户在当前目录中具有写权限,并且指定的目录名不能是当前目录中已有的目录1语法: mkdir [-m] [-p] 目录 ...