-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththe_grid_search.py
More file actions
executable file
·73 lines (56 loc) · 1.65 KB
/
the_grid_search.py
File metadata and controls
executable file
·73 lines (56 loc) · 1.65 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
#!/usr/bin/env python3
import os
import sys
from pathlib import Path
from typing import IO
def gridSearch(G: list[str], P: list[str]) -> str:
"""
Return a string("YES", "NO") if pattern P has been found in G.
Arguments:
G - the grid to search
P - the pattern to search for
"""
p = P[0]
for i in range(len(G) - len(P) + 1):
start = 0
while True:
start = G[i].find(p, start)
if start == -1:
break
found = True
for j in range(1, len(P)):
if G[i + j][start : start + len(p)] != P[j]:
found = False
break
if found:
return "YES"
start += 1
return "NO"
def main(fptr: IO) -> None:
t = int(input().strip())
for _ in range(t):
first_multiple_input = input().rstrip().split()
R = int(first_multiple_input[0])
C = int(first_multiple_input[1])
G = []
for _ in range(R):
G_item = input()
assert len(G_item) == C
G.append(G_item)
second_multiple_input = input().rstrip().split()
r = int(second_multiple_input[0])
c = int(second_multiple_input[1])
P = []
for _ in range(r):
P_item = input()
assert len(P_item) == c
P.append(P_item)
result = gridSearch(G, P)
fptr.write(result + "\n")
if __name__ == "__main__":
if path := os.getenv("OUTPUT_PATH"):
with Path(path).open("wt", encoding="utf-8") as fptr:
main(fptr)
fptr.close()
else:
main(sys.stdout)