1/*
2Redistribution and use in source and binary forms, with or without
3modification, are permitted provided that the following conditions are met:
4
5    * Redistributions of source code must retain the above copyright
6    notice, this list of conditions and the following disclaimer.
7
8    * Redistributions in binary form must reproduce the above copyright
9    notice, this list of conditions and the following disclaimer in the
10    documentation and/or other materials provided with the distribution.
11
12    * Neither the name of "The Computer Language Benchmarks Game" nor the
13    name of "The Computer Language Shootout Benchmarks" nor the names of
14    its contributors may be used to endorse or promote products derived
15    from this software without specific prior written permission.
16
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27POSSIBILITY OF SUCH DAMAGE.
28*/
29
30/* The Computer Language Benchmarks Game
31 * http://shootout.alioth.debian.org/
32 *
33 * contributed by The Go Authors.
34 */
35
36package main
37
38import (
39	"bufio"
40	"bytes"
41	"fmt"
42	"io/ioutil"
43	"os"
44	"sort"
45)
46
47var in *bufio.Reader
48
49func count(data string, n int) map[string]int {
50	counts := make(map[string]int)
51	top := len(data) - n
52	for i := 0; i <= top; i++ {
53		s := data[i : i+n]
54		counts[s]++
55	}
56	return counts
57}
58
59func countOne(data string, s string) int {
60	return count(data, len(s))[s]
61}
62
63type kNuc struct {
64	name  string
65	count int
66}
67
68type kNucArray []kNuc
69
70func (kn kNucArray) Len() int      { return len(kn) }
71func (kn kNucArray) Swap(i, j int) { kn[i], kn[j] = kn[j], kn[i] }
72func (kn kNucArray) Less(i, j int) bool {
73	if kn[i].count == kn[j].count {
74		return kn[i].name > kn[j].name // sort down
75	}
76	return kn[i].count > kn[j].count
77}
78
79func sortedArray(m map[string]int) kNucArray {
80	kn := make(kNucArray, len(m))
81	i := 0
82	for k, v := range m {
83		kn[i].name = k
84		kn[i].count = v
85		i++
86	}
87	sort.Sort(kn)
88	return kn
89}
90
91func print(m map[string]int) {
92	a := sortedArray(m)
93	sum := 0
94	for _, kn := range a {
95		sum += kn.count
96	}
97	for _, kn := range a {
98		fmt.Printf("%s %.3f\n", kn.name, 100*float64(kn.count)/float64(sum))
99	}
100}
101
102func main() {
103	in = bufio.NewReader(os.Stdin)
104	three := []byte(">THREE ")
105	for {
106		line, err := in.ReadSlice('\n')
107		if err != nil {
108			fmt.Fprintln(os.Stderr, "ReadLine err:", err)
109			os.Exit(2)
110		}
111		if line[0] == '>' && bytes.Equal(line[0:len(three)], three) {
112			break
113		}
114	}
115	data, err := ioutil.ReadAll(in)
116	if err != nil {
117		fmt.Fprintln(os.Stderr, "ReadAll err:", err)
118		os.Exit(2)
119	}
120	// delete the newlines and convert to upper case
121	j := 0
122	for i := 0; i < len(data); i++ {
123		if data[i] != '\n' {
124			data[j] = data[i] &^ ' ' // upper case
125			j++
126		}
127	}
128	str := string(data[0:j])
129
130	print(count(str, 1))
131	fmt.Print("\n")
132
133	print(count(str, 2))
134	fmt.Print("\n")
135
136	interests := []string{"GGT", "GGTA", "GGTATT", "GGTATTTTAATT", "GGTATTTTAATTTATAGT"}
137	for _, s := range interests {
138		fmt.Printf("%d %s\n", countOne(str, s), s)
139	}
140}
141