-
Notifications
You must be signed in to change notification settings - Fork 222
/
the-grid-search.py
66 lines (56 loc) · 1.75 KB
/
the-grid-search.py
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
#!/bin/python3
import sys
import re
def occurrences(string, sub):
res = []
ind = 0
while ind < len(string) - len(sub) + 1:
found = string.find(sub, ind)
#print("ind = {} found = {}".format(ind, found))
if found != -1:
res.append(found)
ind = found + 1
else:
break
return res
def gridSearch(G, P):
for ind_g in range(len(G) - len(P) + 1):
cur = -1
all_occurrences = []
for ind_p, s_pat in enumerate(P):
all_occurrences.append(occurrences(G[ind_g + ind_p], s_pat))
#ret = G[ind_g + ind_p].find(s_pat)
#print("ind_g = {} ind_p = {} check {} in {}".format(ind_g, ind_p, s_pat, G[ind_g + ind_p]))
#if ret == -1 or (ind_p > 0 and cur != ret):
# break
#elif ind_p == len(P) - 1:
# return 'YES'
#else:
# cur = ret
#print(all_occurrences)
ourset = set(all_occurrences[0])
for lst in all_occurrences:
ourset &= set(lst)
#print("ourset = {}".format(ourset))
if len(ourset) >= 1:
return 'YES'
return 'NO'
if __name__ == "__main__":
t = int(input().strip())
for a0 in range(t):
R, C = input().strip().split(' ')
R, C = [int(R), int(C)]
G = []
G_i = 0
for G_i in range(R):
G_t = str(input().strip())
G.append(G_t)
r, c = input().strip().split(' ')
r, c = [int(r), int(c)]
P = []
P_i = 0
for P_i in range(r):
P_t = str(input().strip())
P.append(P_t)
result = gridSearch(G, P)
print(result)