最短摘要问题之阿里笔试题

题目: 给定一段产品的英文描述,包含M个英文单词,每个单词以空格分隔,无其他标点,再给定N个英文单词关键字。请说明思路并编程实现方法  String extractSummary(String description,String [ ] Keywords):目标是找出此产品描述中包含N个关键词(每个关键词至少出现一次)的长度最短的子串,作为产品简介输出,编程语言不限。

实现方法:在确保所有关键字都包含的情况下,每次从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]

转载于:https://my.oschina.net/u/147640/blog/23789

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值