Problem:


Score : 200 points

Problem Statement

You are given an image A composed of N rows and N columns of pixels, and a template image B composed of M rows and M columns of pixels.

A pixel is the smallest element of an image, and in this problem it is a square of size 1×1.

Also, the given images are binary images, and the color of each pixel is either white or black.  

In the input, every pixel is represented by a character: . corresponds to a white pixel, and # corresponds to a black pixel.

The image A is given as N strings A_1,...,A_N.

The j-th character in the string A_i corresponds to the pixel at the i-th row and j-th column of the image A (1≦i,j≦N).

Similarly, the template image B is given as M strings B_1,...,B_M.

The j-th character in the string B_i corresponds to the pixel at the i-th row and j-th column of the template image B (1≦i,j≦M).  

Determine whether the template image B is contained in the image A when only parallel shifts can be applied to the images.  

Constraints

1≦M≦N≦50 

A_i is a string of length N consisting of # and ..

B_i is a string of length M consisting of # and ..

Input

The input is given from Standard Input in the following format:

N M
A_1
A_2
:  
A_N
B_1
B_2
:  
B_M

Output

Print Yes if the template image B is contained in the image A. Print No otherwise.

Sample Input 1

3 2
#.#
.#.
#.#
#.
.#

Sample Output 1

Yes

The template image B is identical to the upper-left 2 × 2 subimage and the lower-right 2 × 2 subimage of A. Thus, the output should be Yes.

Sample Input 2

4 1
....
....
....
....
#

Sample Output 2

No

The template image B, composed of a black pixel, is not contained in the image A composed of white pixels.



Sample input:
3 2
#.#
.#.
#.#
#.
.#


Sample output:
Yes

Submission status: Accepted

Solution code:
import math
from math import gcd,pi,sqrt
INF = float("inf")

import sys
sys.setrecursionlimit(10**6)
import itertools
from collections import Counter,deque
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]

import string

def main():
    N,M = i_map()
    a = [s_input() for _ in range(N)]
    b = [s_input() for _ in range(M)]

    for i in range(N-M+1):
        for j in range(N-M+1):
            flg = True

            for x in range(M):
                for y in range(M):
                    if b[x][y] != a[i+x][j+y]:
                        flg = False
                        break
                if flg == False:
                    break
            if flg == True:
                print("Yes")
                exit()
    print("No")

if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
a = [input() for _ in range(n)]
b = [input() for _ in range(m)]
for i in range(n-m+1):
    for j in range(n-m+1):
        if b[0][0] == a[i][j]:
            cnt = 0
            for x in range(m):
                flag = True
                if flag == False:
                    break
                for y in range(m):
                    if b[x][y] == a[i+x][j+y]:
                        cnt += 1
                        pass
                    else:
                        flag = False
                        break
                    if cnt == m**2:
                        print("Yes")
                        exit()
print("No")

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque
from collections import defaultdict
import heapq
import collections
import itertools
import bisect
import copy
import math
sys.setrecursionlimit(10**6)

# lis_of_lis = [[] for _ in range(N)]


def zz(): return list(map(int, sys.stdin.readline().split()))


def z(): return int(sys.stdin.readline())


def S(): return sys.stdin.readline()[:-1]


def C(line): return [sys.stdin.readline() for _ in range(line)]


N, M = zz()
A = [S() for i in range(N)]
B = [S() for i in range(M)]


def check(lis_a, lis_b):
    for a, b in zip(lis_a, lis_b):
        # print(a, b)
        if (a != b):
            return False
    return True


for i in range(N - M+1):
    for j in range(N-M+1):
        ok = 0
        for line in range(M):
            # print(A[i+line][j:j+M], B[line])
            if (A[i+line][j:j+M] != B[line]):
                break
            else:
                ok += 1
        if (ok == M):
            print('Yes')
            exit()
print('No')

Submission status: Accepted

Solution code:
import numpy

n,m = map(int,input().split())
a = numpy.zeros([n,n], dtype=int)
b = numpy.zeros([m,m], dtype=int)
for i in range(n):
    s = input()
    for j in range(n):
        if s[j] == "#":
            a[i,j] = 1
for i in range(m):
    s = input()
    for j in range(m):
        if s[j] == "#":
            b[i,j] = 1

for i in range(n-m+1):
    for j in range(n-m+1):
        if numpy.all(a[i:i+m, j:j+m] == b):
            print("Yes")
            exit()
print("No")

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
A = [input() for i in range(n)]
B = [input() for i in range(m)]

flag = False
for i in range(n-m+1):
    for j in range(n-m+1):
        flag = True
        for k in range(m):
            if B[k] != A[i+k][j:j+m]:
                flag = False
        if flag:
            print("Yes")
            exit()
print("No")

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
def input():
    return sys.stdin.readline()[:-1]

def main():
    N, M = map(int, input().split())
    A = [input() for _ in range(N)]
    B = [input() for _ in range(M)]

    for w in range(N - M + 1):
        for h in range(N - M + 1):
            count = 0
            for i in range(M):
                if B[i] == A[i + h][w: w + M]:
                    count += 1
            if count == M:
                print('Yes')
                exit()
    print('No')


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n,m = map(int, input().split())
a = [input() for i in range(n)]
b = [input() for j in range(m)]
res = "No"

if m == 1:
    for i in a:
        if b[0] in i:
            res = "Yes"
else:
    for k in range(n-m+1): # (0,1) ※aの長さを上限にbが縦に移動できる範囲
        if b[0] in a[k]: # もしa[0]かa[1]にb[0]が含まれるなら
            for l in range(n-m+1): # (0,1) ※aの長さを上限にbが横に移動できる範囲
                if b[0] == a[k][l:m+l]: # もし、b[0]がa[0/1][0:2]かa[0/1][1:3]と一致するなら
                    cnt = 0
                    for o in range(m-1): # (0,1) b[0]は一したのでそれ以外
                        if b[o+1] == a[k+o+1][l:m+l]: # b[1/2]がa[]
                            cnt += 1
                    if cnt == m-1:
                        res = "Yes"
        
print(res)

Submission status: Accepted

Solution code:
n,m = map(int, input().split())
a = [[] for _ in range(n)]
b = ""
c = ""
ans = 0
for i in range(n):
  a[i] = list(input())
for j in range(m):
  b += input()
for i in range(n-m+1):
  for j in range(n-m+1):
    for k in range(i,i+m):
      for l in range(j,j+m):
        c += a[k][l]
    if c == b:
      ans += 1
      break
    c = ""
print("Yes" if ans > 0 else "No")

Submission status: Accepted

Solution code:

from itertools import product


N,M=map(int,input().split())
A=[input() for _ in range(N)]
B=[input() for _ in range(M)]
for sx, sy in product(range(N-M+1), repeat=2):
    is_ok = 1
    for x in range(sx, sx+M):
        for y in range(sy, sy+M):
            if B[x-sx][y-sy] != A[x][y]:
                is_ok=0
                break
        if not is_ok:
            break
    if is_ok:
        print("Yes")
        exit()

print("No")

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
A = [input() for i in range(N)]
B = [input() for i in range(M)]
 
for i in range(N - M + 1):  # tate
    for j in range(N - M + 1):  # yoko
        ans = 1
        for k in range(M):
            if B[k] != A[i + k][j:j+M]:
                ans = 0
        if ans == 1:
            print('Yes')
            exit()
print('No')

Submission status: Accepted

Solution code:
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np
from functools import reduce

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7


n, m = rm()
a = [rr() for _ in range(n)]
b = [rr() for _ in range(m)] 
for i in range(n-m+1):
    for j in range(n-m+1):
        c = [s[j:j+m] for s in a[i:i+m]]
        if c == b:
            print('Yes')
            exit()
print('No')

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
A = [input() for i in range(N)]
B = [input() for i in range(M)]

for i in range(N - M + 1):  # tate
    for j in range(N - M + 1):  # yoko
        ans = 1
        for k in range(M):
            if B[k] != A[i + k][j:j+M]:
                ans = 0
        if ans == 1:
            print('Yes')
            exit()
print('No')

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
A = [input() for _ in range(N)]
B = [input() for _ in range(M)]
R = range(N-M+1)

for i in R:
    for j in R:
        for k in range(M):
            if A[j+k][i:i+M] != B[k]:
                break
        else:
            print('Yes')
            exit()
print('No')

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
A = [input() for _ in range(N)]
B = [input() for _ in range(M)]
for y in range(N - M + 1):
    for x in range(N - M + 1):
        matched = True
        for yy in range(y, y + M):
            for xx in range(x, x + M):
                if A[yy][xx] != B[yy - y][xx - x]:
                    matched = False
                if not matched:
                    break
            if not matched:
                break
        if matched:
            print('Yes')
            exit(0)
print('No')

Submission status: Accepted

Solution code:
import sys
n,m = map(int,input().split())
la = [input() for _ in range(n)]
lb = [input() for _ in range(m)]
for i in range(n-m+1):
    for j in range(n-m+1):
        cnt = 0
        for k in range(m):
            for l in range(m):
                if la[i+k][j+l] == lb[k][l]:
                    cnt += 1
        if cnt == m**2:
            print("Yes")
            sys.exit()
print("No")

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
A = [input() for _ in range(n)]
B = [input() for _ in range(m)]

ans = 'No'
for i in range(n - m + 1):
    for j in range(n - m + 1):
        if all([A[i + k][j:j + m] == B[k] for k in range(m)]):
            ans = 'Yes'
print(ans)

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
a = []
b = []
ans = 0
for i in range(n):
    a.append(list(input()))
for i in range(m):
    b.append(list(input()))
for i in range(n-m+1):
    for j in range(n-m+1):
        cnt = 0
        for s in range(m):
            for t in range(m):
                if b[s][t] == a[s+i][t+j]:
                    cnt += 1
        if cnt == m*m:
            ans = 1
if ans == 0:
    print("No")
else:
    print("Yes")

Submission status: Accepted

Solution code:
import math
N, M = map(int, input().split())
A = [list(input()) for _ in range(N)]
B = [list(input()) for _ in range(M)]

for i in range(0, math.ceil(N/M)):
    for j in range(0, math.ceil(N/M)):
        # 畳み込み
        flag = True
        for l in range(0, M):
            for m in range(0, M):
                if A[l+i][m+j] != B[l][m]:
                    flag = False
                    break
        if flag == True:
            print("Yes")
            exit()
print("No")

Submission status: Accepted

Solution code:
N,M = [int(a) for a in input().split()]
A = []
B = []
for i in range(N):
  A.append(input())
for i in range(M):
  B.append(input())  

wa = len(A[0])
wb = len(B[0])
  
matched = False
for i1 in range(N - M + 1):
  for j1 in range(wa - wb +1):
    cnt = 0
    for i2 in range(M):
      #print(B[i2]," vs ", A[i1+i2][j1:j1+wb])
      if B[i2] == A[i1+i2][j1:j1+wb]:
        cnt += 1
    if cnt == M:
      matched = True
      break
          

if matched:
  print("Yes")
else:
  print("No")

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
A = [input() for _ in range(N)]
B = [input() for _ in range(M)]
for i in range(N-M+1):
  for j in range(N-M+1):
    flag = True
    AA = [a[j:j+M] for a in A[i:i+M]]
    for a, b in zip(AA, B):
      if a != b:
        flag = False
    if flag:
      print("Yes")
      exit()
print("No")

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
a_lst = [str(input()) for _ in range(n)]
b_lst = [str(input()) for _ in range(m)]

flag = False
for i in range(n - m + 1):
    for j in range(n - m + 1):

        count = 0
        if a_lst[i][j] == b_lst[0][0]:
            for k in range(m):
                for l in range(m):
                    if a_lst[i + k][j + l] == b_lst[k][l]:
                        count += 1

            if count == m ** 2:
                flag = True

if flag:
    print('Yes')
else:
    print('No')

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
A=[]
B=[]
for i in range(n):
    A.append(input())
for i in range(m):
    B.append(input())
cnt=0
flag = False
for i in range(n-m+1):
    for j in range(n-m+1):
        ans = 'Yes'
        for k in range(m):
            for l in range(m):
                if A[i+k][j+l]!=B[k][l]:
                    ans = 'No'
        else:
            if ans == 'Yes':
                print(ans)
                exit()
print(ans)

Submission status: Accepted

Solution code:
# B - Template Matching
def main():
    n, m = map(int, input().split())

    a_l = [input() for i in range(n)]
    b_l = [input() for i in range(m)]

    for i in range(n-m+1):
        for j in range(n-m+1):
            ans = 'Yes'
            for k in range(m):
                for l in range(m):
                    if a_l[i+k][j+l] != b_l[k][l]:
                        ans = 'No'
            else:
                if ans == 'Yes':
                    print(ans)
                    exit()
    print(ans)

  
        
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
A = []
for i in range(N):
    a = list(input())
    A.append(a)
B = []
for i in range(M):
    b = list(input())
    B.append(b)
ans = "No"
for i in range(N-M+1):
    for j in range(N-M+1):
        if A[i][j] == B[0][0]:
            count = 0
            for k in range(M):
                for l in range(M):
                    if A[i+k][j+l] == B[k][l]:
                        count += 1
            if count == M*M:
                ans = "Yes"
                break
print(ans)

Submission status: Accepted

Solution code:
#
# abc054 b
#
import sys
from io import StringIO
import unittest


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """3 2
#.#
.#.
#.#
#.
.#"""
        output = """Yes"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """4 1
....
....
....
....
#"""
        output = """No"""
        self.assertIO(input, output)


def resolve():
    N, M = map(int, input().split())
    A = [list(input()) for _ in range(N)]
    B = [list(input()) for _ in range(M)]

    ans = "No"
    for i in range(N-M+1):
        for j in range(N-M+1):
            ans = "Yes"
            for k in range(M):
                if ans == "No":
                    break
                for l in range(M):
                    if A[i+k][j+l] != B[k][l]:
                        ans = "No"
                        break
            if ans == "Yes":
                break
        if ans == "Yes":
            break

    print(ans)


if __name__ == "__main__":
    # unittest.main()
    resolve()

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
mp1 = [input() for _ in range(N)]
mp2 = [input() for _ in range(M)]

for i in range(N-M+1):
  for j in range(N-M+1):
    f = 1
    for k in range(M):
      for l in range(M):
        if mp2[k][l] != mp1[i+k][j+l]:
          f = 0
          break
      if f == 0:
        break
    if f == 1:
      print('Yes')
      exit()
      
print('No')

Submission status: Accepted

Solution code:
def main():
    n,m = map(int,input().split())
    a,b = [], []
    for i in range(n):
        a.append(input())
    for i in range(m):
        b.append(input())
    for i in range(n-m+1):
        for j in range(n-m+1):
            a_ = []
            for k in range(m):
                a_.append(a[i+k][j:j+m])
            if a_==b:
                print('Yes')
                return
    print('No')

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import numpy as np

N,M=map(int,input().split())
A=np.array([list(str(input())) for i in range(N)])
B=np.array([list(str(input())) for j in range(M)])
ans="No"

for i in range(N-M+1):
    for j in range(N-M+1):
        x=[s[j:M+j] for s in A[i:M+i]]
        if np.count_nonzero((x==B)==0) == 0:
            ans="Yes"

print(ans)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
a = [input() for _ in range(n)]
b = [input() for _ in range(m)]
for i in a:
    for j in b:
        if j not in i:
            print('No')
            exit()
print('Yes')

Submission status: Accepted

Solution code:
n, m = list(map(int, input().split()))
a = [input() for _ in range(n)]
b = [input() for _ in range(m)]

ans = False
for y in range(n-m+1):
    for x in range(n-m+1):
        sol = True
        for h in range(m):
            if a[y+h][x:x+m] != b[h]:
                sol = False
                break

        if sol:
            ans = True
            break
    if ans:
        break

print("Yes" if ans else "No")

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
A = []
B = []
for _ in range(N):
    A.append(input())
for _ in range(M):
    B.append(input())
flag = 0
for n_i in range(N - M + 1):
    for n_j in range(N - M + 1):
        for m_i in range(M):
            for m_j in range(M):
                if A[n_i + m_i][n_j + m_j] == B[m_i][m_j]:
                    continue
                break
            else:
                continue
            break
        else:
            flag = 1
            break
    else:
        continue
    break
if flag == 1:
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
a = [input() for i in range(n)]
b = [input() for i in range(m)]
ans = "Yes"
for i in a:
  for j in b:
    if j not in i:
      ans = "No"
print(ans)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
a=[list(input()) for _ in range(n)]
b=[list(input()) for _ in range(m)]
for i in range(0,n-m+1):
    for j in range(0,n-m+1):
        p=[a[x][i:i+m] for x in range(j,j+m)]
        ok=0
        for h in range(m):
            x,y=p[h],b[h]
            for k in range(m):
                if x[k]!=y[k]:
                    ok+=1
        if ok==0:
            print('Yes')
            exit()
print('No')

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
A = []
B = []
for i in range(N):
    A.append(input())
for i in range(M):
    B.append(input())

for i in range(N-M+1):
    if B[0] in A[i]:
        k = A[i].index(B[0])
        points = 1
        for j in range(1,M):
            if B[j] == A[i+j][k:k+M]:
                points += 1
        if points == M:
            print('Yes')
            exit()
print('No')

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()

def main():
    n, m = map(int, input().split())
    A = [input() for _ in range(n)]
    B = [input() for _ in range(m)]
    for i in range(n - m + 1):
        for j in range(n - m + 1):
            correspond = True
            for p in range(m):
                for q in range(m):
                    if A[i + p][j + q] != B[p][q]:
                        correspond = False
                        break
            if correspond:
                print("Yes")
                return
    print("No")

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
A=[input() for i in range(N)]
B=''.join([input() for i in range(M)])
f=0
for i in range(N-M+1):
    for j in range(N-M+1):
        if ''.join([A[l][j:j+M] for l in range(i,i+M)])==B:
            f=1
print(['No','Yes'][f])

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
a, b = [], []
for _ in range(n):
    a.append(input())
for _ in range(m):
    b.append(input())

# (i, j)ずらす
for i in range(n-m+1):
    for j in range(n-m+1):
        flag = True
        for x in range(m):
            for y in range(m):
                if b[x][y] != a[x+i][y+j]:
                    break
            else:
                continue
            flag = False
            break
        if flag:
            print('Yes')
            exit()

print('No')

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

# input = stdin.readline

def solve(n,m,original,temp):
  def judge(sy,sx):
    for i in range(m):
      for j in range(m):
        if original[sy+i][sx+j] != temp[i][j]:
          return False
    return True
  

  sy = 0
  while sy + m - 1 < n:
    sx = 0
    while sx + m - 1 < n:
      if judge(sy,sx):
        return True
      sx += 1
    sy += 1

  return False

def main():
  N,M = map(int,input().split())
  a = [input() for _ in range(N)]
  b = [input() for _ in range(M)]
  print("Yes" if solve(N,M,a,b) else "No")
  return

if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
import numpy as np
N,M = (int(T) for T in input().split())
A = np.zeros((N,N),dtype=bool)
B = np.zeros((M,M),dtype=bool)
for TN in range(0,N):
    A[TN,:] = [True if T=='.' else False for T in input()]
for TM in range(0,M):
    B[TM,:] = [True if T=='.' else False for T in input()]
MatchFlag = False
for TR in range(0,N-M+1):
    for TC in range(0,N-M+1):
        if np.sum(A[TR:TR+M,TC:TC+M]^B)==0:
            MatchFlag = True
print(['No','Yes'][MatchFlag])

Submission status: Accepted

Solution code:
import numpy

def main():
    n,m=map(int,input().split())
    a=numpy.array([list(input()) for _ in range(n)])
    b=numpy.array([list(input()) for _ in range(m)])
    for i in range(n-m+1):
        for j in range(n-m+1):
            if numpy.all(a[i: i+m, j: j+m] == b):
                print("Yes")
                return
    print("No")
    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n ,m = map(int, input().split())
ln = []
lm = []

for i in range(n):
    ln.append(input())
for i in range(m):
    lm.append(input())

lim = n - m + 1
for i in range(lim):
    for j in range(lim):
        l = 0
        for k in ln[i:i+m]:
            if k[j:j+m] != lm[l]:
                break
            l += 1
        else:
            print("Yes")
            break
    else:
        continue
    break

else:
    print("No")

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
A=[[] for i in range(N)]
B=[[] for i in range(M)]
for i in range(N):
  L=list(input())
  A[i]=L
for i in range(M):
  L=list(input())
  B[i]=L
for i in range(N-M+1):
  for j in range(N-M+1):
    ans=0
    for k in range(M):
      for s in range(M):
        #i,jを始点とした
        if B[k][s]!=A[i+k][j+s]:
          ans=-1
        if k==M-1 and s==M-1:
          if ans==0:
            print("Yes")
            exit()
print("No")

Submission status: Accepted

Solution code:
n, m=map(int,input().split())
pic_a = [input() for _ in range(n)]
pic_b = [input() for _ in range(m)]
top = pic_b[0][0]
if m==1:
    for line_a in pic_a:
        for chr_a in line_a:
            if top==chr_a:
                print('Yes')
                exit()
    print('No')
    exit()
for i, line_a in enumerate(pic_a[:-m+1]):
    for j, chr_a in enumerate(line_a[:-m+1]):
        if top == chr_a:
            for k, line_b in enumerate(pic_b):
                if line_b != pic_a[i+k][j:j+m]:
                    break
            else:
                print('Yes')
                exit()
print('No')

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
a = [input() for _ in range(n)]
b = [input() for _ in range(m)]

if n == m:
    for i in range(n):
        if a[i] != b[i]:
            break
    else:
        print("Yes")
        exit()
    print("No")
    exit()

for i in range(n-m):
    start_h = i
    end_h = i+m
    for j in range(n-m):
        start_w = j
        end_w = j+m
        for k in range(m):
            if a[start_h+k][start_w:end_w] != b[k]:
                break
        else:
            print("Yes")
            exit()
print("No")

Submission status: Accepted

Solution code:
import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = float('inf')
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    N, M = LI()
    A = [SS() for _ in range(N)]
    B = [SS() for _ in range(M)]

    is_ok = False
    for i, j in itertools.product(range(N - M + 1), repeat=2):
        tmp = True
        for k in range(M):
            if B[k] != A[i+k][j:j+M]:
                tmp = False
                break
        if tmp:
            is_ok = True
            break

    if is_ok:
        print('Yes')
    else:
        print('No')

if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
n,m = map(int, input().split())
a = [input() for _ in range(n)]
b = [input() for _ in range(m)]
for i in range(n-m+1):
    for j in range(n-m+1):
        tnp = 0
        for k in range(m):
            for l in range(m):
                if (a[i+k][j+l]!=b[k][l]):
                    tnp += 1
        if (tnp==0):
            print("Yes")
            exit()
print("No")

Submission status: Accepted

Solution code:
import sys
N, M = map(int, input().split())
A = []
B = []
for _ in range(N):
    A.append(input())
for _ in range(M):
    B.append(input())

for i in range(N-M+1):
    for j in range(N-M+1):
        for k in range(M):
            #print(A[i+k][j:j+M])
            #print(B[k])
            if A[i+k][j:j+M] != B[k]:
                break
        else:
            print("Yes")
            sys.exit()
print("No")

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
a=[]
b=[]

cnt=0
k=0
flag=0
for i in range(n):
    a.append(input())
for i in range(m):
    b.append(input())

for j in range(n-m+1):
    k=0
    for i in range(n-m+1):
        if b[k]==a[i][j:j+m]:
            k+=1
            if k==m:
                print("Yes")
                flag+=1
                break
    else:
        continue
    break


if flag==0:
    print("No")

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
a=[input() for i in range(n)]
b=[input() for j in range(m)]
for i in range(n-m+1):
    for j in range(n-m+1):
        for k in range(m):
            if a[k+i][0+j:m+j]!=b[k]:
                break
        else:
            print("Yes")
            break
    else:
        continue
    break
else:
    print("No")

Submission status: Accepted

Solution code:
N, M = map(int,input().split())

A = []
for i in range(N):
    A.append(input())

B = []
for i in range(M):
    B.append(input())

for i in range(N-M+1):
    for j in range(N-M+1):
        for k in range(M):
            if A[i+k][j:j+M] != B[k]:
                break
            if k == M-1:
                print("Yes")
                exit()

print("No")

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
a = ["" for _ in range(n)]
b = ["" for _ in range(m)]
for i in range(n):
  ai = str(input())
  a[i] = ai
for i in range(m):
  bi = str(input())
  b[i] = bi
for i in range(n-m+1):
  for j in range(n-m+1):
    flag = False
    for k in range(m):
      for l in range(m):
        if a[i+k][j+l] != b[k][l]:
          flag = True
          break
      if flag:
        break
    if not flag:
      print("Yes")
      exit()
print("No")

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
a = []
for i in range(n):
  a.append(input())
b = []
for i in range(m):
  b.append(input())
flag = 0
judge = []
for i in range(n-m+1):
  judge = []
  for j in range(n-m+1):
    judge = []
    for k in range(m):
      judge.append(a[i+k][j:j+m])
    if judge == b:
      flag += 1
if flag == 0:
  print("No")
else:
  print("Yes")

Submission status: Accepted

Solution code:
n,m=map(int,input().split())

x=[input() for i in range(n)]
y=[input() for i in range(m)]

ans='No'

for i in range(n-m+1):
  for j in range(n-m+1):
    maze=[]
    for k in range(m):
      pin=''
      for l in range(m):
        pin+=x[i+k][j+l]
      
      maze.append(pin)

    if maze==y:
      ans='Yes'
      break


print(ans)

Submission status: Accepted

Solution code:
import sys
n, m = map(int, input().split())
a, b = [], []

for _ in range(n):
    a.append(input())
for _ in range(m):
    b.append(input())

for i in range(0, n-m+1):
    for j in range(0, n-m+1):
        for k in range(m*m):
            if a[i+k//m][j+k%m] != b[k//m][k%m]:
                break
            elif k == m*m-1:
                print('Yes')
                sys.exit()
print('No')

Submission status: Accepted

Solution code:
n, m=map(int,input().split())
A=[input() for _ in range(n)]
B=[input() for _ in range(m)]

def dfs(y, x):
    for Y in range(m):
        for X in range(m):
            if A[y+Y][x+X]!=B[Y][X]:
                return False
    return True

ans="No"
for y in range(n-m+1):
    for x in range(n-m+1):
        if A[y][x]==B[0][0]:
            if dfs(y,x):
                ans="Yes"
                break
    else:
        continue
    break

print(ans)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
if N < M:
  print('No')
else:
  A = [input() for _ in range(N)]
  B = ''
  cnt = 0
  for _ in range(M):
    B += input().rstrip()
  for i in range(N-M+1):
    if cnt==1:
      break
    for j in range(N-M+1):
      judge = ''
      for k in range(M):
        judge += A[i+k][j:j+M]
      if judge == B:
        cnt += 1
        break
  if cnt == 1:
    print('Yes')
  else:
    print('No')

Submission status: Accepted

Solution code:
import sys
N,M=map(int,input().split())
A=[]
for i in range(N):
  a=input()
  A.append(a)
B=[]
for j in range(M):
  b=input()
  B.append(b)
for k in range(N-M+1):
  for l in range(N-M+1):
    z=0
    for m in range(M):
      try:
        if B[m]==A[k+m][l:l+M]:
          z+=1
      except:
        sys.stderr.write(f"{k,l,m}")
        pass
    if z==M:
      print("Yes")
      sys.exit()
print("No")