/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chart_3;
import java.util.ArrayList;
import java.util.List;
/**
*
* 3.5 最短摘要的生成
*/
public class ExtractSummary {
public static void main(String[] args) {
String[] w = {"aa", "bb", "q0", "cc", "q1", "dd", "ee", "q2", "q0", "q1", "hh"};
String[] q = {"q0", "q1", "q2"};
//解法一
String str = extract_summary1(w, q);
System.out.println(str);
}
private static String extract_summary1(String[] w, String[] q) {
int w_begin = 0;
int q_begin = 0;
int w_length = w.length;
int q_length = q.length;
List<String> list = new ArrayList();
while (w_begin < w_length) {
String str = "";
int[] tmp = new int[q.length];
for (int i = w_begin; i < w_length; i++) {
str += w[i];
boolean flag = false;
for (int j = 0; j < q.length; j++) {
if (w[i].equals(q[j]) && tmp[j] == 0) {
tmp[j] = 1;
flag = true;
break;
}
}
if (flag) {
q_begin++;
}
if (q_begin == q_length) {
list.add(str);
break;
}
}
q_begin = 0;
w_begin++;
}
int num = 0;
int flag = 0;
if (!list.isEmpty()) {
num = list.get(0).length();
for (int i = 1; i < list.size(); i++) {
if (list.get(i).length() < num) {
num = list.get(i).length();
flag = i;
}
}
}
return list.get(flag);
}
}
编程之美3.5最短摘要的生成Java版
最新推荐文章于 2022-04-01 19:04:17 发布