-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearchEngine.java
79 lines (67 loc) · 1.68 KB
/
SearchEngine.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
76
77
78
79
/**
* https://www.hackerearth.com/practice/data-structures/advanced-data-structures/trie-keyword-tree/practice-problems/algorithm/search-engine/
* #string #trie
*/
import java.util.Scanner;
class SearchEngine {
static final int MAX = 26;
static class Node {
Node[] children;
int countLeaf;
int maxWeight;
Node() {
countLeaf = 0;
maxWeight = 0;
children = new Node[MAX];
}
}
public static void addWord(Node root, String word, int weight) {
int level;
int length = word.length();
int idx;
Node tmp = root;
for (level = 0; level < length; level++) {
idx = word.charAt(level) - 'a';
if (tmp.children[idx] == null) {
tmp.children[idx] = new Node();
}
tmp = tmp.children[idx];
tmp.maxWeight = Math.max(tmp.maxWeight, weight);
}
tmp.countLeaf = weight;
}
public static int findWord(Node root, String word) {
int level;
int length = word.length();
int idx;
Node tmp = root;
for (level = 0; level < length; level++) {
idx = word.charAt(level) - 'a';
if (tmp.children[idx] == null) {
return -1;
}
tmp = tmp.children[idx];
}
if (tmp == null) {
return -1;
}
return tmp.maxWeight;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int q = sc.nextInt();
Node root = new Node();
String word;
int weight;
for (int i = 0; i < n; i++) {
word = sc.next();
weight = sc.nextInt();
addWord(root, word, weight);
}
for (int i = 0; i < q; i++) {
word = sc.next();
System.out.println(findWord(root, word));
}
}
}