-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.kt
73 lines (64 loc) · 2.18 KB
/
solution.kt
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
fun main() {
System.setOut(java.io.PrintStream(System.`out`, false, "UTF-8"))
class LsPattern(val pattern: List<String?>) {
fun matches(dp: MutableMap<Int, Boolean>, str: String, patternIndex: Int, strIndex: Int): Boolean {
val dpKey = patternIndex * 1000 + strIndex
return dp.getOrPut(dpKey) {
if (patternIndex == pattern.size) {
strIndex == str.length
} else {
val patternPart = pattern[patternIndex]
if (patternPart != null) {
strIndex + patternPart.length <= str.length &&
str.substring(strIndex, strIndex + patternPart.length) == patternPart &&
matches(dp, str, patternIndex + 1, strIndex + patternPart.length)
} else {
(strIndex..str.length).any { matches(dp, str, patternIndex + 1, it) }
}
}
}
}
fun matches(str: String): Boolean {
val dp = mutableMapOf<Int, Boolean>()
val res = matches(dp, str, 0, 0)
return res
}
}
fun parsePattern(s: String): LsPattern {
val result = mutableListOf<String?>()
val buffer = StringBuilder()
var isConstant = s[0] != '*'
fun flush() {
if (buffer.isEmpty()) {
return
}
if (isConstant) {
result.add(buffer.toString())
} else {
result.add(null)
}
buffer.setLength(0)
}
for (c in s) {
if (c == '*') {
if (isConstant) {
flush()
}
isConstant = false
} else {
if (!isConstant) {
flush()
}
isConstant = true
}
buffer.append(c)
}
flush()
return LsPattern(result)
}
val pattern = parsePattern(readLine()!!)
val words = List(readLine()!!.toInt()) {
readLine()!!
}.filter { pattern.matches(it) }
words.forEach(::println)
}