Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def main():
- trie = {'': {}}
- term = [False]
- def add_word(trie, word):
- nonlocal term
- current_node = trie['']
- for chr in word:
- if chr not in current_node:
- current_node[chr] = {}
- term.append(False)
- current_node = current_node[chr]
- term[-1] = True
- n = int(input())
- for _ in range(n):
- word = input()
- add_word(trie, word)
- print(trie)
- print(term)
- if __name__ == '__main__':
- main()
- """
- ввод:
- 4
- мы
- мыс
- мыло
- кот
- вывод:
- {'': {'м': {'ы': {'с': {}, 'л': {'о': {}}}}, 'к': {'о': {'т': {}}}}}
- [False, False, True, True, False, True, False, False, True]
- """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement