-
Notifications
You must be signed in to change notification settings - Fork 27
/
353.go
46 lines (40 loc) · 780 Bytes
/
353.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
// UVa 353 - Pesky Palindromes
package main
import (
"fmt"
"os"
)
func isPalindrome(word string) bool {
for i := 0; i <= len(word)/2; i++ {
if word[i] != word[len(word)-1-i] {
return false
}
}
return true
}
func solve(line string) int {
cache := make(map[string]bool)
var count int
for i := range line {
for j := i + 1; j <= len(line); j++ {
if !cache[line[i:j]] && isPalindrome(line[i:j]) {
cache[line[i:j]] = true
count++
}
}
}
return count
}
func main() {
in, _ := os.Open("353.in")
defer in.Close()
out, _ := os.Create("353.out")
defer out.Close()
var line string
for {
if _, err := fmt.Fscanf(in, "%s", &line); err != nil {
break
}
fmt.Fprintf(out, "The string '%s' contains %d palindromes.\n", line, solve(line))
}
}