-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontacts.js
65 lines (56 loc) · 1.17 KB
/
contacts.js
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
process.stdin.resume()
process.stdin.setEncoding('ascii')
let input_stdin = ''
let input_stdin_array = ''
let input_currentline = 0
process.stdin.on('data', function (data) {
input_stdin += data
})
process.stdin.on('end', function () {
input_stdin_array = input_stdin.split('\n')
main()
})
function readLine () {
return input_stdin_array[input_currentline++]
}
/// //////////// ignore above this line ////////////////////
function main () {
const trie = new Node()
const n = parseInt(readLine())
for (let a0 = 0; a0 < n; a0++) {
const op_temp = readLine().split(' ')
const op = op_temp[0]
const contact = op_temp[1].split('')
if (op === 'add') {
trie.add(contact)
} else {
console.log(trie.find(contact))
}
}
}
class Node {
constructor () {
this.size = 0
}
add (s) {
this.size++
if (s.length === 0) {
return
}
const current = s.shift()
if (!this[current]) {
this[current] = new Node()
}
this[current].add(s)
}
find (s) {
if (s.length === 0) {
return this.size
}
const child = this[s.shift()]
if (!child) {
return 0
}
return child.find(s)
}
}