https://www.acmicpc.net/problem/5052
import sys
class Node:
def __init__(self, value=0, end=False):
self.value = value
self.end = end
self.children = []
def __str__(self):
return f'{self.value} {self.end}'
def insert(root, num):
curr = root
i = 0
while i < len(num):
exist = False
for j in range(len(curr.children)):
child = curr.children[j]
if num[i] == child.value:
exist = True
break
if exist:
if curr.children[j].end:
return True
curr = curr.children[j]
else:
curr.children.append(Node(num[i], i==len(num)-1))
curr = curr.children[-1]
i += 1
return False
def solution():
read = sys.stdin.readline
t = int(read())
for _ in range(t):
n = int(read())
numbers = [list(read().rstrip()) for _ in range(n)]
numbers.sort(key=lambda x:len(x))
root = Node()
for num in numbers:
exist = insert(root, num)
if exist:
break
if exist:
print('NO')
else:
print('YES')
solution()