如果只要找一次就用第一种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 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/

本文介绍了一种O(1)时间复杂度的算法,用于查找文本文件中任意两个单词之间的最短距离。通过使用两次遍历和辅助数据结构,实现快速查询,同时考虑空间复杂度优化。
6836

被折叠的 条评论
为什么被折叠?



