OpenJudge - 01056:IMMEDIATE DECODABILITY
这道题主要考察对于字符串的应用,尽管其内涵是前缀树的唯一性(Huffman树),但题目是很简单的。
话不多说,上代码~
def check(lst):
n = len(lst)
for i in range(n):
for j in range(i+1, n):
if lst[i] == lst[j][:len(lst[i])]:
return True
return False
cases = 0
while True:
try:
cases += 1
lst = []
while True:
tmp = input()
if tmp == '9':
break
lst.append(tmp)
lst.sort()
if check(lst):
print(f'Set {cases} is not immediately decodable')
else:
print(f'Set {cases} is immediately decodable')
except EOFError:
break