ps | |
---|---|
링크 | acmicpc.net/… |
출처 | BOJ |
문제 번호 | 5052 |
문제명 | 전화번호 목록 |
레벨 | 골드 4 |
분류 |
트라이, 정렬 |
시간복잡도 | O(nl) |
인풋사이즈 | n<=10000, l<=10 |
사용한 언어 | Python |
제출기록 | 30096KB / 188ms |
최고기록 | 176ms |
해결날짜 | 2021/01/04 |
"""Solution code for "BOJ 5052. 전화번호 목록".
Using Trie.
- Problem link: https://www.acmicpc.net/problem/5052
- Solution link: http://www.teferi.net/ps/problems/boj/5052
"""
import sys
from teflib import string
def main():
t = int(sys.stdin.readline())
for _ in range(t):
n = int(sys.stdin.readline())
trie = string.Trie(sys.stdin.readline().rstrip() for _ in range(n))
if any(word_node.child_count > 0 for word_node in trie.words()):
print('NO')
else:
print('YES')
if __name__ == '__main__':
main()
"""Solution code for "BOJ 5052. 전화번호 목록".
- Problem link: https://www.acmicpc.net/problem/5052
- Solution link: http://www.teferi.net/ps/problems/boj/5052
"""
import sys
def main():
t = int(sys.stdin.readline())
for _ in range(t):
n = int(sys.stdin.readline())
words = set(sys.stdin.readline().rstrip() for _ in range(n))
for w in words:
if any(w[:i] in words for i in range(1, len(w))):
print('NO')
break
else:
print('YES')
if __name__ == '__main__':
main()
"""Solution code for "BOJ 5052. 전화번호 목록".
Using sorting.
- Problem link: https://www.acmicpc.net/problem/5052
- Solution link: http://www.teferi.net/ps/problems/boj/5052
"""
import sys
def main():
t = int(sys.stdin.readline())
for _ in range(t):
n = int(sys.stdin.readline())
words = sorted(sys.stdin.readline().rstrip() for _ in range(n))
print('NO' if any(y.startswith(x) for x, y in zip(words, words[1:]))
else 'YES')
if __name__ == '__main__':
main()