Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "bufio"
- "fmt"
- "os"
- )
- func main() {
- in := bufio.NewReader(os.Stdin)
- out := bufio.NewWriter(os.Stdout)
- defer out.Flush()
- var (
- n, m int
- line string
- )
- fmt.Fscan(in, &n)
- voc := make([]string, n, n)
- hashvoc := make(map[string][]int)
- for l := 0; l < n; l++ {
- fmt.Fscan(in, &voc[l])
- hashWord(&hashvoc, voc[l], l)
- }
- fmt.Fscan(in, &m)
- for l := 0; l < m; l++ {
- fmt.Fscan(in, &line)
- out.WriteString(findHash(&hashvoc, &voc, line))
- if l != m-1 {
- out.WriteString("\n")
- }
- }
- }
- func hashWord(mm *map[string][]int, word string, index int) {
- if _, ok := (*mm)[word]; !ok {
- (*mm)[word] = make([]int, 0)
- }
- (*mm)[word] = append((*mm)[word], index)
- for j := 0; j <= len(word)-1; j++ {
- (*mm)[word[j:]] = append((*mm)[word[j:]], index)
- }
- }
- func findHash(mm *map[string][]int, voc *[]string, word string) string {
- for j := 0; j <= len(word)-1; j++ {
- if hash, ok := (*mm)[word[j:]]; ok {
- for l := 0; l < len(hash); l++ {
- if (*voc)[hash[l]] != word {
- return (*voc)[hash[l]]
- }
- }
- }
- }
- return (*voc)[0]
- }
Advertisement
Add Comment
Please, Sign In to add comment