-
Notifications
You must be signed in to change notification settings - Fork 27
/
123.go
75 lines (66 loc) · 1.37 KB
/
123.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// UVa 123 - Searching Quickly
package main
import (
"bufio"
"fmt"
"io"
"os"
"sort"
"strings"
)
var out io.WriteCloser
func outputLine(title []string, idx int) {
newTitle := make([]string, len(title))
copy(newTitle, title)
newTitle[idx] = strings.ToUpper(newTitle[idx])
fmt.Fprintln(out, strings.Join(newTitle, " "))
}
func output(titles [][]string, keywordMap map[string]bool) {
var keywords []string
for keyword := range keywordMap {
keywords = append(keywords, keyword)
}
sort.Strings(keywords)
for _, keyword := range keywords {
for _, title := range titles {
for idx, word := range title {
if keyword == word {
outputLine(title, idx)
}
}
}
}
}
func main() {
in, _ := os.Open("123.in")
defer in.Close()
out, _ = os.Create("123.out")
defer out.Close()
s := bufio.NewScanner(in)
s.Split(bufio.ScanLines)
ignores := make(map[string]bool)
var word string
for s.Scan() {
if word = s.Text(); word == "::" {
break
}
ignores[word] = true
}
keywordMap := make(map[string]bool)
var titles [][]string
for s.Scan() {
var title []string
for r := strings.NewReader(s.Text()); ; {
if _, err := fmt.Fscanf(r, "%s", &word); err != nil {
break
}
word = strings.ToLower(word)
title = append(title, word)
if !ignores[word] {
keywordMap[word] = true
}
}
titles = append(titles, title)
}
output(titles, keywordMap)
}