论坛首页 综合技术论坛

阿里实习一道题目:

浏览 14468 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (1)
作者 正文
   发表时间:2011-06-28   最后修改:2011-06-28

java 代码:

public class test1 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String s = "byte[] responseBody = getMethod get Response Body(); String res = new String (responseBody); response getWriter() print (res);";
        System.out.println(extractSummary(s,new String[]{"get","res","print"}));
    }
   
    //思路:通过对每个关键词循环,每次循环得到的数组下标与最大最小下标比较确定新的包含关键词的最大最小的下标,
    //     最后再通过循环得到的最大最小下标将单词组装成语句。
    private static String extractSummary(String description, String[] keyword) {
        String[] descriptions = description.split(" ");
        int minIndex = 0;
        int maxIndex = 0;
        for (int i = 0; i < keyword.length; i++) {
            for (int j=0; j<descriptions.length; j++) {
                if (descriptions[j].contains(keyword[i])) {
                    if (i == 0) { //第一次对起始和结束指针位置进行初始化
                        minIndex = j;
                        maxIndex = j;
                        break;
                    }
                    if (j < minIndex) { //如果匹配上的数组下标小于最小索引下标,则把当前下标作为最小指针下标
                        minIndex = j;
                    } else if (j > maxIndex) { //如果匹配上的数组下标大于于最大索引下标,则把当前下标作为最小指针下标
                        maxIndex = j;
                    }
                    break;
                }
            }
        }
        //组装字符串
        StringBuffer summary=new StringBuffer("");
        for (int i=minIndex; i <= maxIndex;i++) {
            if (i < maxIndex) {
                summary.append(descriptions[i]).append(" ");
            } else {
                summary.append(descriptions[i]);
            }
        }
        return summary.toString();
    }
}

0 请登录后投票
   发表时间:2011-08-11   最后修改:2011-08-11
楼上的这个不对吧?

试试:
摘要: "i love qinqin i love you forever";
关键字 {"love", "forever", "i"};
这是个奇迹,竟然输出了“i”,??????????
再改改。。。。。。。
看看我的肿么样:
0 请登录后投票
   发表时间:2011-08-12  
hxz_qlh 写道
楼上的这个不对吧?

试试:
摘要: "i love qinqin i love you forever";
关键字 {"love", "forever", "i"};
这是个奇迹,竟然输出了“i”,??????????
再改改。。。。。。。
看看我的肿么样:


有问题,试试"i love qinqin you forever i love"
0 请登录后投票
   发表时间:2011-12-30   最后修改:2011-12-30
算法思路是两个指针,开始都指向0,右指针增加到刚好满足所有关键字,左指针开始增加,直到再减少一个单词不能满足所有关键字为止,这就是一个匹配,记录最小长度,然后左指针再+1,继续循环直到右指针达到字符串尾部。

形象的理解为毛毛虫爬行,一张一缩。

public class Test {

	public static void main(String[] args) {
		extractSubString("show me the money what money is the money i have no money not quiet enough", new String[] { "no", "is", "i" });
	}

	public static void extractSubString(String source, String[] keyword) {
		String[] words = source.split(" ");
		Map<String, Integer> readWords = new HashMap<String, Integer>();
		Set<String> keywordSet = new HashSet<String>(Arrays.asList(keyword));
		int i = 0, j = 0, minSize = Integer.MAX_VALUE, start = -1, end = -1;
		for (j = 0; j < words.length && i < words.length; j++) {
			readWord(words[j], readWords, keywordSet);
			if (readWords.keySet().containsAll(keywordSet)) {
				while (i < words.length && tryDropWord(words[i], readWords, keywordSet)){
					i++;
				}
				if (j - i + 1 < minSize) {
					start = i;
					end = j;
					minSize = j - i + 1;
				}
				readWords.remove(words[i++]);
			}
		}

		if (start != -1) {
			System.out.println("startIndex=" + start);
			for (int k = start; k <= end; k++) {
				System.out.print(words[k] + ' ');
			}
		} else {
			System.out.print("无解");
		}
	}

	private static void readWord(String word, Map<String, Integer> readWords, Set<String> keyword) {
		if (keyword.contains(word)) {
			if (readWords.containsKey(word)) {
				readWords.put(word, readWords.get(word) + 1);
			} else {
				readWords.put(word, 1);
			}
		}
	}

	private static boolean tryDropWord(String word, Map<String, Integer> readWords, Set<String> keyword) {
		if (keyword.contains(word)) {
			if (readWords.get(word) > 1) {
				readWords.put(word, readWords.get(word) - 1);
				return true;
			} else {
				return false;
			}
		}
		return true;
	}
}

source="show me the money what money is the money i have no money not quiet enough"
keyword=["no", "is", "i"]
startIndex=6
is the money i have no
0 请登录后投票
论坛首页 综合技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics