-
Notifications
You must be signed in to change notification settings - Fork 0
/
SearchSystem.java
75 lines (75 loc) · 3.18 KB
/
SearchSystem.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import java.io.*;
import java.util.*;
import java.util.stream.Collectors;
public class SearchSystem {
public static void main(String[] args) throws IOException {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out))) {
int nDoc = Integer.parseInt(reader.readLine());
List<String> docs = new ArrayList<>();
for (int i = 0; i < nDoc; i++) {
docs.add(reader.readLine());
}
HashMap<String, HashMap<Integer, Integer>> preparedData = prepareData(docs);
int nRequest = Integer.parseInt(reader.readLine());
for (int i = 0; i < nRequest; i++) {
findRelevant(preparedData, reader.readLine(), nDoc, writer);
writer.newLine();
}
}
}
public static void findRelevant(HashMap<String, HashMap<Integer, Integer>> preparedData, String request, int n, BufferedWriter writer) throws IOException {
Map<Integer, Integer> result = new HashMap<>();
Set<String> requestSet = Arrays.stream(request.split(" ")).collect(Collectors.toSet());
for(String word: requestSet){
if(preparedData.containsKey(word)){
for(Map.Entry<Integer, Integer> doc: preparedData.get(word).entrySet()){
Integer score = 0;
if(result.containsKey(doc.getKey())){
score = result.get(doc.getKey());
}
score = score + doc.getValue();
result.put(doc.getKey(), score);
}
}
}
int count = Math.min(result.size(), 5);
for (int i = 0; i < count; i++) {
Integer maxKey = getMax(result);
writer.write(maxKey + " ");
result.remove(maxKey);
}
}
public static Integer getMax(Map<Integer, Integer> result){
Integer maxKey = 0;
Integer maxValue = 0;
for(Map.Entry<Integer, Integer> pair: result.entrySet()){
if(maxValue < pair.getValue()){
maxValue = pair.getValue();
maxKey = pair.getKey();
} else if(maxValue.equals(pair.getValue())){
if(maxKey > pair.getKey()){
maxKey = pair.getKey();
}
}
}
return maxKey;
}
public static HashMap<String, HashMap<Integer, Integer>> prepareData(List<String> docs){
HashMap<String, HashMap<Integer, Integer>> preparedData = new HashMap<>();
for (int i = 0; i < docs.size(); i++) {
for(String word: docs.get(i).split(" ")){
if(!preparedData.containsKey(word)){
HashMap<Integer, Integer> r = new HashMap<>();
preparedData.put(word, r);
}
if(!preparedData.get(word).containsKey(i+1)){
preparedData.get(word).put(i + 1, 1);
} else{
preparedData.get(word).put(i + 1, preparedData.get(word).get(i+1) + 1);
}
}
}
return preparedData;
}
}