import sys
input = sys.stdin.readline
def check(word):
now = colors
for i in range(len(word)):
if now.get(0) and word[i:] in names:
return 1
if not now.get(word[i]):
return
now = now[word[i]]
C,N = map(int,input().split())
colors = {}
for _ in range(C):
now = colors
for c in input().strip():
if not now.get(c):
now[c] = {}
now = now[c]
now[0] = 1
names = {input().strip() for i in range(N)}
for _ in range(int(input())):
print("Yes" if check(input().strip()) else "No")
트라이와 셋을 이용해서 해결하였다.
풀이를 설명하면,
1. 색상에 대한 트라이를 dict를 이용해서 만든다.
2. 이름은 set에 넣는다.
3. 팀명에 대해서 트라이를 실행하고, 색상을 발견할 때마다 남은 문자열을 슬라이싱해서 이름 set에 들어있는지 확인한다.
간단한 문제인데 생각지도 못한 부분에서 골머리를 앓았던 문제였다. 바로 출력을 "Yes" "No"가 아닌 "YES" "NO"로 했던 것이다. 대체 어디가 틀린건지 며칠을 고민했었는데 허무함만 남았다...
오늘의 교훈) 출력사항을 확실하게 체크하자