-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy path10029.go
More file actions
74 lines (65 loc) · 1.54 KB
/
10029.go
File metadata and controls
74 lines (65 loc) · 1.54 KB
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
// UVa 10029 - Edit Step Ladders
package main
import (
"fmt"
"os"
)
var dp = make(map[int]map[string]int)
func add(word string) int {
var longest int
for j := 0; j <= len(word); j++ {
for k := 'a'; k <= 'z'; k++ {
if newWord := word[:j] + string(k) + word[j:]; dp[len(newWord)][newWord] > 0 && word > newWord {
longest = max(longest, dp[len(newWord)][newWord])
}
}
}
return longest
}
func remove(word string) int {
var longest int
for j := 0; j < len(word); j++ {
if newWord := word[:j] + word[j+1:]; dp[len(newWord)][newWord] > 0 && word > newWord {
longest = max(longest, dp[len(newWord)][newWord])
}
}
return longest
}
func replace(word string) int {
var longest int
for j := 0; j < len(word); j++ {
for k := 'a'; k <= 'z'; k++ {
if newWord := word[:j] + string(k) + word[j+1:]; dp[len(newWord)][newWord] > 0 && word > newWord {
longest = max(longest, dp[len(newWord)][newWord])
}
}
}
return longest
}
func solve(words []string) int {
var longest int
for _, word := range words {
dp[len(word)][word] = max(max(add(word), remove(word)), replace(word)) + 1
longest = max(longest, dp[len(word)][word])
}
return longest
}
func main() {
in, _ := os.Open("10029.in")
defer in.Close()
out, _ := os.Create("10029.out")
defer out.Close()
var word string
var words []string
for {
if _, err := fmt.Fscanf(in, "%s", &word); err != nil {
break
}
words = append(words, word)
if dp[len(word)] == nil {
dp[len(word)] = make(map[string]int)
}
dp[len(word)][word] = 1
}
fmt.Fprintln(out, solve(words))
}