1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.StringTokenizer;
public class B20166 {
static int N,M,K;
static char[][] arr;
static String[] likestr;
static int[][] delta = {{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}};
static HashMap<String, Integer> map = new HashMap<>();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
K = Integer.parseInt(st.nextToken());
arr = new char[N][M];
likestr = new String[K];
for(int i=0;i<N;i++) {
arr[i] = br.readLine().toCharArray();
}
for(int i=0;i<N;i++) {
for(int j=0;j<M;j++) {
String key = Character.toString(arr[i][j]);
// key가 있으면 +1, 없으면 default로 생성
map.put(key,map.getOrDefault(key,0) + 1);
go(i,j,1,key);
}
}
// 해당하는 문자의 경우 출력
for(int i=0;i<K;i++) {
sb.append(map.getOrDefault(br.readLine(), 0)).append("\n");
}
System.out.println(sb);
}
private static void go(int x, int y, int depth, String key) {
if(depth==5) return;
for(int d=0;d<8;d++) {
int nx = x + delta[d][0];
int ny = y + delta[d][1];
// 범위 넘었을 때 처리
if (nx < 0) nx = N-1;
if (ny < 0) ny = M-1;
if (nx > N-1) nx = 0;
if (ny > M-1) ny = 0;
String newkey = key + arr[nx][ny];
// 새로운 문자열 count
map.put(newkey, map.getOrDefault(newkey, 0) + 1);
go(nx,ny,depth+1,newkey);
}
}
}
|