Hard 大文本找两个单词最短距离 @CareerCup

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值