Advertisement
giGii

бор

Jun 20th, 2022
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.74 KB | None | 0 0
  1. def main():
  2.  
  3.     trie = {'': {}}
  4.     term = [False]
  5.    
  6.     def add_word(trie, word):
  7.         nonlocal term
  8.         current_node = trie['']
  9.         for chr in word:
  10.             if chr not in current_node:
  11.                 current_node[chr] = {}
  12.                 term.append(False)
  13.             current_node = current_node[chr]
  14.         term[-1] = True
  15.  
  16.     n = int(input())
  17.     for _ in range(n):
  18.         word = input()
  19.         add_word(trie, word)
  20.     print(trie)
  21.     print(term)
  22.  
  23.  
  24. if __name__ == '__main__':
  25.     main()
  26.  
  27. """
  28. ввод:
  29. 4
  30. мы
  31. мыс
  32. мыло
  33. кот
  34.  
  35. вывод:
  36. {'': {'м': {'ы': {'с': {}, 'л': {'о': {}}}}, 'к': {'о': {'т': {}}}}}
  37. [False, False, True, True, False, True, False, False, True]
  38. """
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement