-
Notifications
You must be signed in to change notification settings - Fork 27
/
417.go
54 lines (49 loc) · 877 Bytes
/
417.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
// UVa 417 - Word Index
package main
import (
"fmt"
"os"
)
func dp() [27][6]int {
var cache [27][6]int
for i := range cache {
cache[i][0] = 1
if i != 0 {
for j := 1; j < len(cache[i]); j++ {
cache[i][j] = cache[i-1][j-1] + cache[i-1][j]
}
}
}
return cache
}
func main() {
in, _ := os.Open("417.in")
defer in.Close()
out, _ := os.Create("417.out")
defer out.Close()
cache := dp()
var word string
here:
for {
if _, err := fmt.Fscanf(in, "%s", &word); err != nil {
break
}
for i := 0; i < len(word)-1; i++ {
if word[i] >= word[i+1] {
fmt.Fprintln(out, 0)
continue here
}
}
total := 1
for i := 1; i < len(word); i++ {
total += cache[26][i]
}
for ch, i := byte('a'), 0; i < len(word); i++ {
for ; ch < word[i]; ch++ {
total += cache['z'-ch][len(word)-i-1]
}
ch++
}
fmt.Fprintln(out, total)
}
}