-
Notifications
You must be signed in to change notification settings - Fork 27
/
11362.go
67 lines (60 loc) · 1 KB
/
11362.go
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
// UVa 11362 - Phone List
package main
import (
"fmt"
"os"
)
type trie struct {
last bool
next [10]*trie
}
func addToTrie(curr *trie, phone string) bool {
for i := range phone {
if curr.last {
return false
}
idx := phone[i] - '0'
if curr.next[idx] == nil {
curr.next[idx] = &trie{}
}
curr = curr.next[idx]
}
if curr.last {
return false
}
curr.last = true
for _, next := range curr.next {
if next != nil {
return false
}
}
return true
}
func solve(phones []string) bool {
root := trie{}
for _, phone := range phones {
if !addToTrie(&root, phone) {
return false
}
}
return true
}
func main() {
in, _ := os.Open("11362.in")
defer in.Close()
out, _ := os.Create("11362.out")
defer out.Close()
var t, n int
for fmt.Fscanf(in, "%d", &t); t > 0; t-- {
fmt.Fscanf(in, "%d", &n)
phones := make([]string, n)
for i := range phones {
fmt.Fscanf(in, "%s", &phones[i])
}
if solve(phones) {
fmt.Fprintln(out, "YES")
} else {
fmt.Fprintln(out, "NO")
}
}
}