对若干行文字建立倒排索引(根据单词找到所在行号)。
然后根据关键字,在倒排索引查找进行查找,找到包含所有该关键字所在的行数并输出。
输入说明
- 若干行英文,以
!!!!!
为结束。 - 输入一行查询关键字,以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.*;
class tianTian
{
String name;
ArrayList<Integer> jiangLi = new ArrayList<>();
public tianTian(String name,int cnt)
{
this.name=name;
jiangLi.add(cnt);
}
public String toString()
{
return name+"="+jiangLi.toString();
}
}
public class Main {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int line=1;
ArrayList<String> jieGe=new ArrayList<>();
ArrayList<tianTian> chengSS=new ArrayList<>();
while(true)
{
String str= in.nextLine();
if(str.equals("!!!!!"))
{
break;
}
jieGe.add(str);
String[] sss=str.split(" ");
for (String s : sss) {
boolean fare=true;
for (tianTian no : chengSS) {
if (no.name.equals(s)) {
boolean flag=true;
for(Integer cnt:no.jiangLi)
{
if(cnt==line)
{
flag=false;
break;
}
}
if(flag)
{
no.jiangLi.add(line);
}
fare=false;
break;
}
}
if(fare)
{
tianTian wenHT = new tianTian(s,line);
chengSS.add(wenHT);
}
}
line++;
}
Collections.sort(chengSS, new Comparator<tianTian>() {
@Override
public int compare(tianTian o1, tianTian o2) {
if(o1.name.compareTo(o2.name)>0)
{
return 1;
}
return -1;
}
});
for(tianTian jiangli:chengSS)
{
System.out.println(jiangli);
}
while (in.hasNextLine())
{
String str=in.nextLine();
String[] stt=str.split(" ");
ArrayList<Integer> momoda=new ArrayList<>();
for (int i = 0; i < jieGe.size(); i++) {
String omoxi = jieGe.get(i);
int ccTV = 0;
for (String gomogomo : stt) {
if (omoxi.contains(gomogomo)) {
ccTV++;
}
}
if (ccTV == stt.length) {
momoda.add(i+1);
}
}
if(!momoda.isEmpty())
{
System.out.println(momoda);
for(Integer i:momoda)
{
System.out.println("line "+i+":"+jieGe.get(i-1));
}
}
else
{
System.out.println("found 0 results");
}
}
}
}