/
Solution.scala
40 lines (36 loc) · 1.67 KB
/
Solution.scala
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
/**
* Created by Kotobotov.ru on 27.11.2018.
*/
object Solution extends App {
val (input,n) = (readLine, readInt)
val words = for(i <- 0 until n) yield readLine
val alphabit = Map(
'A' -> ".-", 'B' -> "-...", 'C' -> "-.-.", 'D' -> "-..", 'E' -> ".", 'F' -> "..-.",
'G' -> "--.", 'H' -> "....", 'I' -> "..", 'J' -> ".---", 'K' -> "-.-", 'L' -> ".-..",
'M' -> "--", 'N' -> "-.", 'O' -> "---", 'P' -> ".--.", 'Q' -> "--.-", 'R' -> ".-.",
'S' -> "...", 'T' -> "-", 'U' -> "..-", 'V' -> "...-", 'W' -> ".--", 'X' -> "-..-",
'Y' -> "-.--", 'Z' -> "--..")
val transformedWords = words
.map(item => item.map(char => alphabit(char)).mkString)
.map(item => (item, 1))
.groupBy(_._1)
.mapValues(_.size)
val wordsSizes = transformedWords.keys.map(item => item.length).toList
val TARGET_SIZE = input.size
var memoize = Map.empty[Int, Long]
def count(startFrom: Int): Long = {
startFrom match {
case TARGET_SIZE => 1L
case start if memoize.contains(start) => memoize(start)
case start => val sumOfCombination: Long =
(for {nextPosition <- wordsSizes if (startFrom + nextPosition) <= TARGET_SIZE
result = if (transformedWords.contains(input.substring(startFrom, startFrom + nextPosition)))
transformedWords(input.substring(startFrom, startFrom + nextPosition)
) * count(startFrom + nextPosition) else 0
} yield result).sum
memoize += (start -> sumOfCombination)
sumOfCombination
}
}
println(count(0))
}