博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
7-3 jmu-Java-05集合-4-倒排索引 (30分) Java语言编译
阅读量:4035 次
发布时间:2019-05-24

本文共 2446 字,大约阅读时间需要 8 分钟。

对若干行文字建立倒排索引(根据单词找到所在行号)。

然后根据关键字,在倒排索引查找进行查找,找到包含所有该关键字所在的行数并输出。
输入说明

若干行英文,以!!!!!为结束。输入一行查询关键字,以1个空格为分隔

输出说明

输出所创建倒排索引。索引的key按照字母升序,索引的value按照行号升序输出查询结果。如果找到,输出行集与行集内每一行的内容,如果没找到输出found 0 results

输入样例

where are you from are you ok

this is a test
that is an apple
there are lots of apples you eat it
who are you
!!!
you are
eat
you test
abc

输出样例

a=[2]

an=[3]
apple=[3]
apples=[4]
are=[1, 4, 5]
eat=[4]
from=[1]
is=[2, 3]
it=[4]
lots=[4]
of=[4]
ok=[1]
test=[2]
that=[3]
there=[4]
this=[2]
where=[1]
who=[5]
you=[1, 4, 5]
[1, 4, 5]
line 1:where are you from are you ok
line 4:there are lots of apples you eat it
line 5:who are you
[4]
line 4:there are lots of apples you eat it
found 0 results
found 0 results

import java.util.*;import java.util.Map.Entry;public class Main {
public static void main(String[] args) {
Scanner se = new Scanner(System.in); HashMap
> h = new HashMap
>(); ArrayList
original_article=new ArrayList
(); ArrayList
all_rows_number=new ArrayList
(); int NO_of_rows = 0; while (true) { String s = se.nextLine(); if (s.equals("!!!!!")) { break; }else if(s==null||s.length()==0||(s != null && s.equals(""))) { continue; }else{ original_article.add(s); NO_of_rows++; all_rows_number.add(NO_of_rows); String a[] = s.split("\\s+"); for (int i = 0; i < a.length; i++) { if (a[i] == null || a[i].length() == 0) continue; if (!h.containsKey(a[i])) { ArrayList
l = new ArrayList
(); l.add(NO_of_rows); h.put(a[i], l); } else { ArrayList
w=h.get(a[i]); if(!w.contains(NO_of_rows)) { w.add(NO_of_rows); } h.put(a[i], w); } } } } ArrayList
>> al = new ArrayList
>>( h.entrySet()); Collections.sort(al, Comparator.comparing(Map.Entry::getKey)); for (Map.Entry
> e : al) { System.out.println(e.getKey() + "=" + e.getValue()); } while(se.hasNext()) { String s=se.nextLine(); ArrayList
> q=new ArrayList
>(); String a[]=s.split("\\s+"); for(int i=0;i
d=returnIntersectionOfList(q); if(d.size()>0) { System.out.println(d); for(int r:d) { if(all_rows_number.contains(r)) { System.out.println("line "+r+":"+original_article.get(r-1)); } } }else { System.out.println("found 0 results"); } } } public static ArrayList
returnList(ArrayList
>> al ,String args){ for (Map.Entry
> e : al) { if(e.getKey().equals(args)){ return e.getValue(); } } return new ArrayList
(); } public static ArrayList
returnIntersectionOfList(ArrayList
> q){ ArrayList
d=q.get(0); for(int i=1;i

转载地址:http://embdi.baihongyu.com/

你可能感兴趣的文章
[LeetCode By Python]7 Reverse Integer
查看>>
[leetCode By Python] 14. Longest Common Prefix
查看>>
[leetCode By Python]111. Minimum Depth of Binary Tree
查看>>
[LeetCode By Python]118. Pascal's Triangle
查看>>
[LeetCode By Python]121. Best Time to Buy and Sell Stock
查看>>
[LeetCode By Python]122. Best Time to Buy and Sell Stock II
查看>>
[LeetCode By Python]125. Valid Palindrome
查看>>
[LeetCode By Python]136. Single Number
查看>>
[LeetCode By MYSQL] Combine Two Tables
查看>>
如何打开ipynb文件
查看>>
[Leetcode BY python ]190. Reverse Bits
查看>>
Android下调用收发短信邮件等(转载)
查看>>
Android中电池信息(Battery information)的取得
查看>>
SVN客户端命令详解
查看>>
Android/Linux 内存监视
查看>>
Linux系统信息查看
查看>>
用find命令查找最近修改过的文件
查看>>
Android2.1消息应用(Messaging)源码学习笔记
查看>>
Phone双模修改涉及文件列表
查看>>
android UI小知识点
查看>>