[CareerCup] 18.5 Shortest Distance between Two Words 两单词间的最短距离
18.5 You have a large text file containing words. Given any two words, find the shortest distance (in terms of number of words) between them in the file. If the operation will be repeated many times for the same file (but different pairs of words), can you optimize your solution?
LeetCode上的原题,请参见我之前的博客Shortest Word Distance,Shortest Word Distance II和 Shortest Word Distance III。
解法一:
// Call One Time
int shortest_dist(vector<string> words, string word1, string word2) {
int p1 = -, p2 = -, res = INT_MAX;
for (int i = ; i < words.size(); ++i) {
if (words[i] == word1) p1 = i;
if (words[i] == word2) p2 = i;
if (p1 != - && p2 != -) res = min(res, abs(p1 - p2));
}
return res;
}
解法二:
// Call Many Times
int shortest_dist(vector<string> words, string word1, string word2) {
unordered_map<string, vector<int>> m;
int i = , j = , res = INT_MAX;
for (int i = ; i < words.size(); ++i) {
m[words[i]].push_back(i);
}
while (i < m[word1].size() && j < m[word2].size()) {
res = min(res, abs(m[word1][i] - m[word2][j]));
m[word1][i] < m[word2][j] ? ++i : ++j;
}
return res;
}
解法三:
// word1, word2 may be same
int shortest_dist(vector<string> words, string word1, string word2) {
int p1 = words.size(), p2 = -words.size(), res = INT_MAX;
for (int i = ; i < words.size(); ++i) {
if (words[i] == word1) p1 = word1 == word2 ? p2 : i;
if (words[i] == word2) p2 = i;
res = min(res, abs(p1 - p2));
}
return res;
}
[CareerCup] 18.5 Shortest Distance between Two Words 两单词间的最短距离的更多相关文章
- [Locked] Shortest Distance from All Buildings
Shortest Distance from All Buildings You want to build a house on an empty land which reaches all bu ...
- maximum shortest distance
maximum shortest distance Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- PAT1046: Shortest Distance
1046. Shortest Distance (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The ...
- [Swift]LeetCode821. 字符的最短距离 | Shortest Distance to a Character
Given a string S and a character C, return an array of integers representing the shortest distance f ...
- LeetCode 613. Shortest Distance in a Line
Table point holds the x coordinate of some points on x-axis in a plane, which are all integers. Writ ...
- [LeetCode] Shortest Distance to a Character 到字符的最短距离
Given a string S and a character C, return an array of integers representing the shortest distance f ...
- PAT A1046 Shortest Distance
PAT A1046 Shortest Distance 标签(空格分隔): PAT TIPS: 最后一个数据点可能会超时 #include <cstdio> #include <al ...
- [Solution] 821. Shortest Distance to a Character
Difficulty: Easy Problem Given a string S and a character C, return an array of integers representin ...
- A1046. Shortest Distance
The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed t ...
随机推荐
- C# 使用 NPOI 库读写 Excel 文件
NPOI 是开源的 POI 项目的.NET版,可以用来读写Excel,Word,PPT文件.在处理Excel文件上,NPOI 可以同时兼容 xls 和 xlsx.官网提供了一份 Examples,给出 ...
- Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树 矩阵面积并
D. Vika and Segments Vika has an infinite sheet of squared paper. Initially all squares are whit ...
- (转载)一个用于Gnome桌面的下拉式终端: Guake 0.7.0 发布
转自:https://linux.cn/article-5507-1.html Linux的命令行是最好.最强大的东西,它使新手着迷,并为老手和极客的提供极其强大的功能.那些在服务器和生产环境下工作的 ...
- ios透明代理抓包
之前接到一些ios测试的时候,一些应用往往由于这样那样的原因(比如自实现的发包函数)导致直接使用本地ios系统的代理很难将数据代理到主机的burp或findler中,本文提供了一种解决该问题的途径 原 ...
- jquery replace用法汇总
//只替换匹配到的第一个目标 var str="Visit Microsoft! Microsoft"document.write(str.replace(/Microsoft/, ...
- SQL ISNULL 函数
sql 中 NULL 值的处理:微软的 ISNULL() 函数用于规定如何处理 NULL 值.NVL(), IFNULL() 和 COALESCE() 函数也可以达到相同的结果.语法ISNULL ( ...
- 使用python实现栈和队列
1.使用python实现栈: class stack(): def __init__(self): self.stack = [] def empty(self): return self.stack ...
- zookeeper启动错误 transaction type: 2 error: KeeperErrorCode = NoNode for /hbase
hbase伪分布式,与zookeeper同一台机器的时候,运行一段时间,启动zookeeper的时候,日志中有如下错误,导致无法启动zookeeper java.io.IOException: Fai ...
- BZOJ3161 : 孤舟蓑笠翁
显然求出每个点到所有关键点的最短路和次短路即可,答案就是每个关键点的次短路. 设$f[i][j][0]$表示左手在$i$,右手在$j$的解,$f[i][j][1]$表示左手在$i$,右手在$j$,且左 ...
- BZOJ4546(原) : 三元组
设$f(x)=\sum_{x|d}p(d)$. 则$ans=\sum_{i=1}^n\sum_{j=1}^n\sum_{k=1}^n\mu(i)\mu(j)\mu(k)f(lcm(i,j))f(lcm ...