Advertisement
Guest User

Untitled

a guest
Mar 18th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.95 KB | None | 0 0
  1. class Autocomplete:
  2.     def __init__(self):
  3.         self.hash = {}
  4.  
  5.     def get_word(self, prefix):
  6.         auto_words = self.hash.get(prefix)
  7.         if auto_words and len(auto_words) == 1:
  8.             return auto_words[0]
  9.         return None
  10.  
  11.     def add_in_dict(self, word):
  12.         for i in range(1, len(word) + 1):
  13.             prefix = word[0: i]
  14.             if prefix in self.hash:
  15.                 if word not in self.hash[prefix]:
  16.                     self.hash[prefix] = self.hash[prefix] + [word]
  17.             else:
  18.                 self.hash[prefix] = [word]
  19.  
  20.  
  21. result = 0
  22. auto_complete = Autocomplete()
  23.  
  24. n = int(input())
  25. words = str(input())
  26. words = words.split()[0:n]
  27.  
  28. for word in words:
  29.     auto_complete_flag = False
  30.     count = 0
  31.  
  32.     for i in range(1, len(word) + 1):
  33.         count += 1
  34.         prefix = word[0: i]
  35.         auto_word = auto_complete.get_word(prefix)
  36.         if auto_word and auto_word == word:
  37.             autocompl_flag = True
  38.             break
  39.  
  40.     if not auto_complete_flag:
  41.         auto_complete.add_in_dict(word)
  42.  
  43.     result += count
  44.  
  45. print(auto_complete.hash)
  46. print(result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement