-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathtask.go
139 lines (110 loc) · 2.24 KB
/
task.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
"strings"
"unicode"
)
const (
SUCCESS = "YES"
FAIL = "NO"
)
type Node struct {
isBlack bool
tree PrefixMap
}
type PrefixMap map[string]*Node
type DP [][]*Node
func main() {
s := strings.Builder{}
t, root := initInput(bufio.NewScanner(os.Stdin))
Solution(t, root, &s)
fmt.Println(s.String())
}
// Solution решение задачи
func Solution(t string, rootNode *Node, s *strings.Builder) {
if rootNode.tree[string(t[0])] == nil {
s.WriteString(FAIL)
return
}
dp := make(DP, len(t))
dp[0] = append(dp[0], rootNode.tree[string(t[0])])
for i := 1; i < len(t); i++ {
c := string(t[i])
var isSomeBlack bool
dp[i], isSomeBlack = findNodes(dp[i-1], c)
if isSomeBlack && rootNode.tree[c] != nil {
dp[i] = append(dp[i], rootNode.tree[c])
}
if len(dp[i]) == 0 {
break
}
}
for _, v := range dp[len(t)-1] {
if v.isBlack {
s.WriteString(SUCCESS)
return
}
}
s.WriteString(FAIL)
}
// findNodes поиск ноды по символу
func findNodes(n []*Node, char string) ([]*Node, bool) {
var res []*Node
var isBlack bool
for _, v := range n {
if v.isBlack {
isBlack = true
}
if v.tree[char] != nil {
res = append(res, v.tree[char])
}
}
if len(res) == 0 {
return res, isBlack
}
return res, isBlack
}
// addPrefix добавление префикса в ноду
func addPrefix(root *Node, s string) {
for i := 0; i < len(s); i++ {
if _, ok := root.tree[string(s[i])]; !ok {
root.tree[string(s[i])] = &Node{
isBlack: false,
tree: PrefixMap{},
}
}
root = root.tree[string(s[i])]
}
root.isBlack = true
}
// initInput парсим input-данные
func initInput(scanner *bufio.Scanner) (t string, root *Node) {
const bufCapacity = 10000000 // fix long string
buf := make([]byte, bufCapacity)
scanner.Buffer(buf, bufCapacity)
scanner.Scan()
t = scanner.Text()
scanner.Scan()
n, err := strconv.Atoi(scanner.Text())
if err != nil {
log.Fatal(err)
}
root = &Node{
isBlack: false,
tree: PrefixMap{},
}
for i := 0; i < n; i++ {
scanner.Scan()
p := scanner.Text()
if unicode.IsSpace(rune(p[len(p)-1])) {
addPrefix(root, p[:len(p)-1])
} else {
addPrefix(root, p)
}
}
return
}