-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path10391.go
47 lines (41 loc) · 884 Bytes
/
10391.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
// UVa 10391 - Compound Words
package main
import (
"fmt"
"os"
"sort"
"strings"
)
func binarySearch(w string, words []string) bool {
return words[sort.Search(len(words), func(i int) bool { return words[i] >= w })] == w
}
func solve(words []string) []string {
var matched []string
here:
for _, word := range words {
if len(word) > 1 {
for i := 1; i < len(word); i++ {
if w1, w2 := word[:i], word[i:]; binarySearch(w1, words) && binarySearch(w2, words) {
matched = append(matched, word)
continue here
}
}
}
}
return matched
}
func main() {
in, _ := os.Open("10391.in")
defer in.Close()
out, _ := os.Create("10391.out")
defer out.Close()
var words []string
var word string
for {
if _, err := fmt.Fscanf(in, "%s", &word); err != nil {
break
}
words = append(words, word)
}
fmt.Fprintln(out, strings.Join(solve(words), "\n"))
}