Skip to content

Commit e0ca98a

Browse files
committed
문자열 지옥에 빠진 호석 풀이
1 parent e4bc5e8 commit e0ca98a

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#
2+
# https://www.acmicpc.net/problem/20166
3+
4+
import sys
5+
from collections import defaultdict, deque
6+
7+
input = sys.stdin.readline
8+
cnt = 0 # 최대 탐색 길이
9+
N,M,K = map(int, input().split())
10+
board = [list(input()) for _ in range(N)]
11+
god_words = []
12+
for _ in range(K):
13+
word = input().rstrip()
14+
cnt = max(cnt, len(word))
15+
god_words.append(word)
16+
17+
word_dict = defaultdict(int)
18+
arrow = ((-1,0),(1,0),(0,-1),(0,1),(-1,1),(1,-1),(-1,-1),(1,1))
19+
20+
def find(n, m):
21+
queue = deque([(n,m,board[n][m])])
22+
23+
while queue:
24+
x,y,s = queue.pop()
25+
26+
if len(s) <= cnt:
27+
word_dict[s] += 1
28+
if len(s) == cnt:
29+
continue
30+
31+
for dx,dy in arrow:
32+
nx,ny = (x+dx)%N, (y+dy)%M
33+
queue.append((nx,ny,s+board[nx][ny]))
34+
35+
for n in range(N):
36+
for m in range(M):
37+
find(n, m)
38+
39+
answer = []
40+
for god_word in god_words:
41+
answer.append(str(word_dict[god_word]))
42+
43+
print("\n".join(answer))

0 commit comments

Comments
 (0)