isbasov

J

Jan 23rd, 2023
980
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.14 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "bufio"
  5.     "fmt"
  6.     "os"
  7. )
  8.  
  9. func main() {
  10.  
  11.     in := bufio.NewReader(os.Stdin)
  12.     out := bufio.NewWriter(os.Stdout)
  13.     defer out.Flush()
  14.     var (
  15.         n, m int
  16.         line string
  17.     )
  18.  
  19.     fmt.Fscan(in, &n)
  20.  
  21.     voc := make([]string, n, n)
  22.     hashvoc := make(map[string][]int)
  23.     for l := 0; l < n; l++ {
  24.  
  25.         fmt.Fscan(in, &voc[l])
  26.         hashWord(&hashvoc, voc[l], l)
  27.  
  28.     }
  29.     fmt.Fscan(in, &m)
  30.     for l := 0; l < m; l++ {
  31.         fmt.Fscan(in, &line)
  32.  
  33.         out.WriteString(findHash(&hashvoc, &voc, line))
  34.  
  35.         if l != m-1 {
  36.             out.WriteString("\n")
  37.         }
  38.     }
  39.  
  40. }
  41.  
  42. func hashWord(mm *map[string][]int, word string, index int) {
  43.  
  44.     if _, ok := (*mm)[word]; !ok {
  45.         (*mm)[word] = make([]int, 0)
  46.     }
  47.     (*mm)[word] = append((*mm)[word], index)
  48.  
  49.     for j := 0; j <= len(word)-1; j++ {
  50.         (*mm)[word[j:]] = append((*mm)[word[j:]], index)
  51.     }
  52.  
  53. }
  54.  
  55. func findHash(mm *map[string][]int, voc *[]string, word string) string {
  56.  
  57.     for j := 0; j <= len(word)-1; j++ {
  58.         if hash, ok := (*mm)[word[j:]]; ok {
  59.             for l := 0; l < len(hash); l++ {
  60.                 if (*voc)[hash[l]] != word {
  61.                     return (*voc)[hash[l]]
  62.                 }
  63.             }
  64.  
  65.         }
  66.     }
  67.  
  68.     return (*voc)[0]
  69. }
Advertisement
Add Comment
Please, Sign In to add comment