Shortest summary generation

Shortest summary generation

Title description:
    Type the first 4 words of the blog name "The Method of Structure" in the Baidu or Google search box, and you can see the link to this blog in the first option. The search result is "The Method of Structure Algorithm- "there are some descriptive text under:" blog channel -CSDN.NET programmer interview, algorithm, programming, art, classic red-black tree four major highlights and summary of the original series: law algorithm July- structures ..., "we This text is called the summary of that search result, which is the shortest summary. Our question is, how is this shortest summary generated?
Analysis and ideas: The
    title is known, our goal is to find the string containing the shortest summary in an array containing keywords, there are a total of 4 steps :
        Step1, save all keywords in a map, used to view the position of the keyword in the paragraph
        step2, compare each keyword in turn, mark its appearing position, if the same keyword is found, update the following Mark
        step3, if all keywords are found (the number of saved indexes is equal to the number of keywords), calculate the length of the string, if it is less than min, replace the value of min, and place the string at the beginning of the original paragraph. Save the end position for the final output and compare step 4
        , repeat the above process until the original paragraph is completed. . .

import java.util.*;
import static java.util.Collections.sort;
public class main2 {
    
    
    public static void main(String[] args) {
    
    
        String keyword[] = {
    
     "微软", "计算机", "亚洲" ,"交流"};
        String description[] = {
    
    
                "微软", "亚洲", "研究院", "成立", "于", "1998", "年", ",", "我们", "的", "使命",
                "是", "使", "未来", "的", "计算机", "能够", "看", "、", "听", "、", "学", ",",
                "能", "用", "自然语言", "与", "人类", "进行", "交流", "。", "在", "此", "基础", "上",
                ",", "微软", "亚洲", "研究院", "还", "将", "促进", "计算机", "在", "亚太", "地区",
                "的", "普及", ",", "改善", "亚太", "用户", "的", "计算", "体验", "。", "”"};
        //方便查看运行结果
        for (String s : keyword) {
    
    
            System.out.print(s+" ");
        }
        System.out.println();
        minText(description,keyword);
    }
    static void minText(String[] per,String[]keys){
    
    
        Map<String,Integer> map=new HashMap();
        //index中存放关键字的下标
        ArrayList<Integer> index=new ArrayList();
        int min=Integer.MAX_VALUE;
        int start=-1;
        int end=-1;
        //初始化将每个关键字的值都设置为-1
        for (int i = 0; i < keys.length; i++) {
    
    
            map.put(keys[i],-1);
        }
        for (int i = 0; i < per.length; i++) {
    
    
            String word=per[i];
            //判断该字符串是否在map
            if(map.containsKey(word)){
    
    
                //如果包含该字符串,得到该字符串的下标
                int k = map.get(per[i]);
                //比较index看其中是否包含该下标,如果包含更新其下标,否则将该下标添加index中
                if (index.contains(k))
                    index.remove(index.indexOf(k));
                index.add(i);
                //更新字符串的下标
                map.put(per[i],i);
                //更新完成后判断是否所有关键字被囊括其中
                if(index.size()==keys.length){
    
    
                    //然后比较看是否是最短
                    int[]temp=getlength(index);
                    if(min>temp[0]){
    
    
                        min=temp[0];
                        //保存最短摘要的开始的下标,结束的下标
                        start=temp[1];
                        end=temp[2];
                    }
                    continue;
                }
            }
        }
        //输出找到的最短摘要
        for (int i = start; i <=end; i++) {
    
    
            System.out.print(per[i]);
        }
    }
    private static int[] getlength(ArrayList<Integer>index) {
    
    
        //四个关键字出现的下标顺序不一定是有序的
        sort(index);
        //得到开始的那个下标
        int start=index.get(0);
        //得到结束的那个下标
        int end=index.get(index.size()-1);
        return new int[]{
    
    end-start+1,start,end};
    }
}

The results of the program are as follows:
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_45731083/article/details/105318251