Hard 大文本找两个单词最短距离 @CareerCup
来源:互联网 发布:java api 1.8中文 编辑:程序博客网 时间:2024/06/06 03:05
如果只要找一次就用第一种O(n)解法
如果要找多次就多用一个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 mattersint 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/
0 0
- Hard 大文本找两个单词最短距离 @CareerCup
- 两个单词之间的最短距离
- Hard 单词变型成另一个单词 @CareerCup
- Hard 单词变型成另一个单词 @CareerCup
- 给出两个单词,找到它们的最短距离
- Hard 找到由其它单词组成的最长单词 @CareerCup
- Hard 找最小的k个数(Selection Rank) @CareerCup
- hdu 1007找最短距离的点
- Hard 不用+号实现两个数之和 @CareerCup
- uva 11836(两个四面体最短距离)
- LeetCode 243. Shortest Word Distance(单词最短距离)
- LeetCode-243:Shortest Word Distance (单词最短距离)
- 程序员面试金典——解题总结: 9.18高难度题 18.5有个内含单词的超大文本文件,给定任意两个单词,找出在这个文件中这两个单词的最短距离
- 最短距离
- 最短距离
- 最短距离
- 最短距离
- Moderate 查单词 @CareerCup
- 倒水问题
- Hard 计算0到n之间2的个数 @CareerCup
- {dede:channel type='son'}无子栏目时如何显示同级栏目
- JAVA的一些高级知识集合
- 《黑马程序员》多态———day07
- Hard 大文本找两个单词最短距离 @CareerCup
- Openning
- Hard 找最小的k个数(Selection Rank) @CareerCup
- 什么也没学上的毕业生是否还来得及
- Add Two Numbers
- Django admin进阶
- Remove Duplicates from Sorted List
- 我重写的Excel 净现值和内部回报率的公式
- windows下搭建爬虫框架scrapy