Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Autocomplete:
- def __init__(self):
- self.hash = {}
- def get_word(self, prefix):
- auto_words = self.hash.get(prefix)
- if auto_words and len(auto_words) == 1:
- return auto_words[0]
- return None
- def add_in_dict(self, word):
- for i in range(1, len(word) + 1):
- prefix = word[0: i]
- if prefix in self.hash:
- if word not in self.hash[prefix]:
- self.hash[prefix] = self.hash[prefix] + [word]
- else:
- self.hash[prefix] = [word]
- result = 0
- auto_complete = Autocomplete()
- n = int(input())
- words = str(input())
- words = words.split()[0:n]
- for word in words:
- auto_complete_flag = False
- count = 0
- for i in range(1, len(word) + 1):
- count += 1
- prefix = word[0: i]
- auto_word = auto_complete.get_word(prefix)
- if auto_word and auto_word == word:
- autocompl_flag = True
- break
- if not auto_complete_flag:
- auto_complete.add_in_dict(word)
- result += count
- print(auto_complete.hash)
- print(result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement