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
原创粉丝点击