Hard 大文本找两个单词最短距离 @CareerCup
2013-12-13 04:43
567 查看
如果只要找一次就用第一种O(n)解法
如果要找多次就多用一个Hashtable,把所有的组合都保存起来
Ref: http://tianrunhe.wordpress.com/2012/06/04/shortest-distances-between-two-words-in-a-file/
如果要找多次就多用一个Hashtable,把所有的组合都保存起来
package Hard; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import CtCILibrary.AssortedMethods; /** * 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. Can you make the searching operation in O(1) time? What about the space complexity for your solution? 译文: 有一个很大的文本文件,里面包含许多英文单词。给出两个单词,找到它们的最短距离 (以它们之间隔了多少个单词计数)。你能在O(1)的时间内返回任意两个单词间的最短距离吗? 你的解法空间复杂度是多少? * */ public class S18_5 { // O(n) public static int shortest(String[] words, String word1, String word2) { int min = Integer.MAX_VALUE; int lastPosWord1 = -1; int lastPosWord2 = -1; for (int i = 0; i < words.length; i++) { String currentWord = words[i]; if (currentWord.equals(word1)) { lastPosWord1 = i; // Comment following 3 lines if word order matters int distance = lastPosWord1 - lastPosWord2; if (lastPosWord2 >= 0 && min > distance) { min = distance; } } else if (currentWord.equals(word2)) { lastPosWord2 = i; int distance = lastPosWord2 - lastPosWord1; if (lastPosWord1 >= 0 && min > distance) { min = distance; } } } return min; } //=============================================================================== private static Map<HashSet<String>, Integer> distances = new HashMap<HashSet<String>, Integer>(); public static int query(String word1, String word2) { HashSet<String> pair = new HashSet<String>(); pair.add(word1); pair.add(word2); if(distances != null && distances.containsKey(pair)){ return distances.get(pair); } return Integer.MAX_VALUE; } public static void buildMap(String[] wordlist) { // build the mapping between pairs of words to // their shortest distances for (int i = 0; i < wordlist.length; ++i) { for (int j = i + 1; j < wordlist.length; ++j) { if (!wordlist[i].equals(wordlist[j])) { HashSet<String> pair = new HashSet<String>(); pair.add(wordlist[i]); pair.add(wordlist[j]); if (distances.keySet().contains(pair)) { int curr = distances.get(pair); if (j - i < curr) distances.put(pair, j - i); } else { distances.put(pair, j - i); } } } } } public static void main(String[] args) { String[] wordlist = AssortedMethods.getLongTextBlobAsStringList(); System.out.println(AssortedMethods.stringArrayToString(wordlist)); String[][] pairs = { { "Lara", "the" }, { "river", "life" }, { "path", "their" }, { "life", "a" } }; buildMap(wordlist); for (String[] pair : pairs) { String word1 = pair[0]; String word2 = pair[1]; int distance = shortest(wordlist, word1, word2); System.out.println("Distance between <" + word1 + "> and <" + word2 + ">: " + distance + ", " + query(word1, word2)); } } }
Ref: http://tianrunhe.wordpress.com/2012/06/04/shortest-distances-between-two-words-in-a-file/
相关文章推荐
- (算法)两个单词的最短距离
- Hard 找到由其它单词组成的最长单词 @CareerCup
- 两个单词之间的最短距离
- 程序员面试金典——解题总结: 9.18高难度题 18.5有个内含单词的超大文本文件,给定任意两个单词,找出在这个文件中这两个单词的最短距离
- Hard 单词变型成另一个单词 @CareerCup
- 给出两个单词,找到它们的最短距离
- 给出两个单词,找到它们的最短距离 (以它们之间隔了多少个单词计数)。
- Hard 随机选择subset @CareerCup
- Hard 动态查找中位数 @CareerCup
- 求两个时间点的最短距离
- Cracking the coding interview: 查找文中两个单词的距离
- [LeetCode] Shortest Word Distance 最短单词距离
- Stack_Queue 两个栈实现一个队列 @CareerCup
- 【数据结构与算法】二叉树给定两个节点的最短距离(C++实现)
- 在二维数组寻找两个定点的最短距离(递归)
- Hard 计算0到n之间2的个数 @CareerCup
- POJ 3862 Asteroids(两个三维凸包的重心到表面最短距离和)
- POJ 3862 Asteroids (三维凸包,求两个凸包重心到表面的最短距离)
- LeetCode 243. Shortest Word Distance (最短单词距离)$
- python的N个小功能(文本字段对应数值,经纬度计算距离,两个时间点计算时间间隔)