用python写字典树时间会更多,需要将单词排序,长度短的先插入
class tnode(object):
def __init__(self):
self.cnt = 0
self.op = {}
def insert(self, s):
flag = True
cur = self
for c in s:
if c not in cur.op:
cur.op[c] = tnode()
cur = cur.op[c]
if cur.cnt == 1:
flag = False
break
cur.cnt = 1
if cur.op != {}:
flag = False
return flag
t = int(input())
for _ in range(t):
n = int(input())
rt = tnode()
a = []
for _ in range(n):
a.append(input())
a = sorted(a)
flag = True
for s in a:
flag = flag and rt.insert(s)
if flag:
print('YES')
else:
print('NO')