-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path19.go
37 lines (33 loc) · 761 Bytes
/
19.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
package main
import "bufio"
import "os"
import "strings"
import "fmt"
var npossibles = make(map[string]int)
var towels []string
func possible(design string) int {
if len(design) == 0 { return 1 }
if n, found := npossibles[design]; found { return n }
var result = 0
for _, t := range towels {
if len(t) <= len(design) && design[0:len(t)] == t {
var p = possible(design[len(t):])
result += p
npossibles[design[len(t):]] = p
}
}
return result
}
func main() {
var scanner = bufio.NewScanner(os.Stdin)
scanner.Scan()
towels = strings.Split(scanner.Text(), ", ")
var possibles, total = 0, 0
scanner.Scan()
for scanner.Scan() {
var p = possible(scanner.Text())
if p > 0 { possibles++ }
total += p
}
fmt.Println(possibles, total)
}