最短摘要問題之阿里筆試題

題目: 給定一段產品的英文描述,包含M個英文單詞,每一個單詞以空格分隔,無其餘標點,再給定N個英文單詞關鍵字。請說明思路並編程實現方法  String extractSummary(String description,String [ ] Keywords):目標是找出此產品描述中包含N個關鍵詞(每一個關鍵詞至少出現一次)的長度最短的子串,做爲產品簡介輸出,編程語言不限。java

實現方法:在確保全部關鍵字都包含的狀況下,每次從content尾向前挪動一個位置,都從content的頭部到尾遍歷一遍,碰上小的就付給result,直到徹底遍歷完編程

代碼:數組

package test;

import java.util.ArrayList;
import java.util.List;

/**
 * @author hy
 *  2011/6/13
 */
public class FindAbstract {
	static String content[] = { "a", "c", "d", "a", "c", "b", "d", "e", "a","a","b"};
	static String keyword[] = { "b", "c", "d" };
	static List<String> contentList = new ArrayList<String>();

	public static void main(String args[]) {
		List<String> result = new ArrayList<String>();
		int begin = 0;
		int end = content.length;
		// 將content內容從數組形式變換成List型
		for (int i = 0; i < end; i++)
			contentList.add(i, content[i]);
		// 輸出給定的content和keyword
		System.out.print("[content]:  ");
		for (int i = 0; i < content.length; i++)
			System.out.print(content[i] + " ");
		System.out.println();
		System.out.print("[keyword]:  ");
		for (int i = 0; i < keyword.length; i++)
			System.out.print(keyword[i] + " ");
		System.out.println();
		// 輸出最短摘要
		result = contentList;
		System.out.println("[AllMatch]:");
		for (end = content.length; end - begin >= keyword.length; end--) {
			for (begin = 0; end - begin >= keyword.length; begin++) {
				if (isAllHave(contentList.subList(begin, end), keyword)
						&& result.size() > contentList.subList(begin, end)
								.size()){
					result = contentList.subList(begin, end);
					System.out.println("     "+result);
				}
			}
			begin = 0;
		}
		System.out.println("[ShortestMatch]: "+result);

	}

	// 是否都包含全部關鍵字
	static boolean isAllHave(List<String> arr, String key[]) {
		boolean is = false;
		int temp = 0;
		for (int i = 0; i < key.length; i++)
			if (isKeywordIn(arr, key[i]))
				temp++;
		if (temp == key.length)
			is = true;
		return is;
	}

	// 是否包含單個關鍵字
	static boolean isKeywordIn(List<String> arr, String key) {
		int i;
		for (i = 0; i < arr.size(); i++)
			if (arr.get(i) == key)
				return true;
		return false;
	}

}

結果:編程語言

[content]:  a c d a c b d e a a b
[keyword]:  b c d
[AllMatch]:
     [c, d, a, c, b, d, e, a, a, b]
     [d, a, c, b, d, e, a, a, b]
     [a, c, b, d, e, a, a, b]
     [c, b, d, e, a, a, b]
     [c, b, d, e, a, a]
     [c, b, d, e, a]
     [c, b, d, e]
     [c, b, d]
[ShortestMatch]: [c, b, d]
spa

相關文章
相關標籤/搜索