-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy path409.go
80 lines (72 loc) · 1.41 KB
/
409.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
76
77
78
79
80
// UVa 409 - Excuses, Excuses!
package main
import (
"bufio"
"fmt"
"os"
)
func isAlphabetic(b byte) bool { return b >= 'A' && b <= 'Z' || b >= 'a' && b <= 'z' }
func toLower(b byte) byte {
if b >= 'A' && b <= 'Z' {
return b - 'A' + 'a'
}
return b
}
func split(line string) []string {
var words []string
var word string
var started bool
for i := range line {
if !started && isAlphabetic(line[i]) {
started = true
}
if started {
if !isAlphabetic(line[i]) {
words = append(words, word)
word = ""
started = false
} else {
word += string(toLower(line[i]))
}
}
}
return words
}
func main() {
in, _ := os.Open("409.in")
defer in.Close()
out, _ := os.Create("409.out")
defer out.Close()
s := bufio.NewScanner(in)
s.Split(bufio.ScanLines)
var k, e int
for kase := 1; s.Scan(); kase++ {
fmt.Sscanf(s.Text(), "%d%d", &k, &e)
keywords := make(map[string]bool)
for i := 0; i < k && s.Scan(); i++ {
keywords[s.Text()] = true
}
excuses := make([]string, e)
scores := make([]int, e)
var max int
for i := range excuses {
s.Scan()
excuses[i] = s.Text()
for _, word := range split(excuses[i]) {
if keywords[word] {
scores[i]++
}
}
if scores[i] > max {
max = scores[i]
}
}
fmt.Fprintf(out, "Excuse Set #%d\n", kase)
for i, excuse := range excuses {
if scores[i] == max {
fmt.Fprintln(out, excuse)
}
}
fmt.Fprintln(out)
}
}