-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBOJ_5052.swift
69 lines (52 loc) · 1.17 KB
/
BOJ_5052.swift
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
// 백준 5052 전화번호 목록
// 21.05.25
import Foundation
class Trie {
private var isEnd = false
private var children = [Trie?](repeating: nil, count: 10)
public init() { }
public func insert(_ root: Trie, _ key: [Character]) {
var parent = root
for ch in key {
let index = Int(String(ch))!
if parent.children[index] == nil {
parent.children[index] = Trie()
}
parent = parent.children[index]!
}
parent.isEnd = true
}
public func contain(_ root: Trie, _ key: [Character]) -> Bool {
var parent = root
for ch in key {
let index = Int(String(ch))!
if parent.children[index] == nil {
return false
}
if parent.children[index]!.isEnd {
return true
}
parent = parent.children[index]!
}
return true
}
}
var t = Int(readLine()!)!
while t > 0 {
t -= 1
let n = Int(readLine()!)!
var numbers = Trie()
var result = true
for _ in 0..<n {
let number = readLine()!.map { ($0) }
if numbers.contain(numbers, number) {
result = false
}
numbers.insert(numbers, number)
}
if result {
print("YES")
} else {
print("NO")
}
}