-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeek13_A_12171675_JeongChanLee.cpp
More file actions
221 lines (202 loc) · 3.66 KB
/
Week13_A_12171675_JeongChanLee.cpp
File metadata and controls
221 lines (202 loc) · 3.66 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include <iostream>
#include <string>
using namespace std;
string matrix[3001];
int fail[3001];
int n, m;
bool isOk(int y, int x) {
return y > -1 && y < n && x > -1 && x < n;
}
void failure(string word) {
fail[0] = 0;
int i = 1;
int j = 0;
while (i < word.length()) {
if (word[i] == word[j]) {
fail[i] = j + 1;
i++;
j++;
}
else if (j > 0) {
j = fail[j - 1];
}
else {
fail[i] = 0;
i++;
}
}
}
bool KMP(string check, string word) {
int i = 0;
int j = 0;
while (i < check.length()) {
if (check[i] == word[j]) {
if (j == word.length() - 1)
return true;
i++;
j++;
}
else if (j > 0)
j = fail[j - 1];
else
i++;
}
return false;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
string check;
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> matrix[i];
check += matrix[i];
check += ';';
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
check += matrix[j][i];
}
check += ';';
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (!isOk(j, i + j))
break;
check += matrix[j][i + j];
}
check += ';';
}
for (int i = 1; i < n; i++) {
for (int j = 0; j < n; j++) {
if (!isOk(i + j, j))
break;
check += matrix[i + j][j];
}
check += ';';
}
for (int i = 0; i < m; i++) {
string s;
cin >> s;
failure(s);
if (KMP(check, s))
cout << 1 << '\n';
else
cout << 0 << '\n';
}
return 0;
}
//#include <iostream>
//#include <string>
//using namespace std;
//
//int n, m;
//int fail[3001];
//string matrix[3001];
//
//bool isOk(int y, int x) {
// return y > -1 && y < n && x > -1 && x < n;
//}
//
//void failure(string s) {
// fail[0] = 0;
// int i = 1;
// int j = 0;
//
// while (i < s.length()) {
// if (s[i] == s[j]) {
// fail[i] = j + 1;
// i++;
// j++;
// }
// else if (j > 0)
// j = fail[j - 1];
// else {
// fail[i] = 0;
// i++;
// }
// }
//}
//
//bool KMP(string check, string word) {
// int i = 0;
// int j = 0;
//
// while (i < check.length()) {
// if (check[i] == word[j]) {
// if (j == word.length() - 1)
// return true;
//
// i++;
// j++;
// }
// else if (j > 0)
// j = fail[j - 1];
// else
// i++;
// }
//
// return false;
//}
//
//int main() {
// ios::sync_with_stdio(false);
// cin.tie(NULL);
// cout.tie(NULL);
//
// string check = "";
// cin >> n >> m;
//
// for (int i = 0; i < n; i++) {
// cin >> matrix[i];
// check += matrix[i];
// check += ";";
// }
//
// for (int i = 0; i < n; i++) {
// for (int j = 0; j < n; j++) {
// check += matrix[j][i];
// }
// check += ";";
// }
//
// for (int i = 0; i < n; i++) {
// for (int j = 0; j < n; j++) {
// if (!isOk(j, i + j))
// break;
//
// check += matrix[j][i + j];
// }
// check += ";";
// }
//
// for (int i = 1; i < n; i++) {
// for (int j = 0; j < n; j++) {
// if (!isOk(i + j, j))
// break;
//
// check += matrix[i + j][j];
// }
// check += ";";
// }
//
// for (int i = 0; i < m; i++) {
// string word;
// cin >> word;
//
// failure(word);
//
// if (KMP(check, word))
// cout << 1 << '\n';
// else
// cout << 0 << '\n';
// }
//
// return 0;
//}
//
//// 2차원 격자판을 상-하 좌-우 좌상단-우하단으로 읽어 하나의 문자열으로 만든다.
//// padding 문자를 중간중간 삽입해서 한줄의 긴 문자열로 만든다. --> 여기서 KMP
//// 문자열이 불일치 할 때 탐색을 시작했던 텍스트 위치의 다음부터가 아닌, 미리 계산했던 정보를 이용하여 몇 칸은 건너뛴 후 탐색
//// 패턴의 각 위치에서 접두사와 접미사가 일치하는 최대 길이