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:


def read_int():
    return int(input().strip())


def read_ints():
    return list(map(int, input().strip().split(' ')))


def solve():
    N, M = read_ints()
    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):
            matches = True
            for i0 in range(i, i+M):
                for j0 in range(j, j+M):
                    if A[i0][j0] != B[i-i0][j-j0]:
                        matches = False
                        break
                if not matches:
                    break
            if matches:
                return 'Yes'
    return 'No'


if __name__ == '__main__':
    print(solve())

Submission status: Accepted

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

nlis = [list(input()) for _ in range(n)]
mlis = [list(input()) for _ in range(m)]

f = 0

for i in range(n):
    for j in range(n):
        if i+m-1 >=n or j+m-1 >=n:
            continue

        flag = 1
        for k in range(m):
            for l in range(m):
                if nlis[i+k][j+l] != mlis[k][l]:
                    flag = 0
        
        if flag:
            f = 1

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

Submission status: Accepted

Solution code:
import sys
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LS2(): return list(sys.stdin.readline().rstrip())  #空白なし

N,M = MI()
A = [LS2() for _ in range(N)]
B = [LS2() for _ in range(M)]

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

Submission status: Accepted

Solution code:
import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())
N, M = map(int, input().split())

A = [input() for _ in range(N)]
B = [input() for _ in range(M)]

def check(x, y):
    for i in range(M):
        for j in range(M):
            if y + i >= N or x + j >= N:
                return False
            if A[y + i][x + j] != B[i][j]:
                return False
    return True

for i in range(N):
    for j in range(N):
        if check(i, j):
            print("Yes")
            exit()
print("No")

Submission status: Accepted

Solution code:
from sys import exit

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
        for k in range(m):
            for l in range(m):
                if a[i+k][j+l] != b[k][l]:
                    flag = False
                    break
            if not flag:
                break
        if flag:
            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)]

ans = 'No'
for i in range(N-M+1):
    for j in range(N-M+1):
        C = [A[k][i:i+M] for k in range(j, j+M)]
        if B == C:
            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)]
#print(B)
for i in range(n-m+1):
    for j in range(n-m+1):
        tmp=["" for i in range(m)]
        for k in range(m):
            for r in range(m):
                tmp[k]+=A[i+k][j+r]
        #print(tmp)
        if tmp == B:
            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 i in range(N-M+1):
  for j in range(len(A[i])-len(B[0])+1):
    if B[0][0]==A[i][j]:
      flag=True
      p=i
      for k in range(M):
        if not B[k]==A[p][j:j+len(B[k])]:
          flag=False
          break
        p+=1
      if flag:
        print('Yes')
        exit()
print('No')

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
n_pict=[]
m_pict=[]
for _ in range(n):
  n_pict.append(input())
for _ in range(m):
  m_pict.append(input())
  
for y in range(n-m+1):
  for x in range(n-m+1):
    found=True
    for a in range(m):
      if m_pict[a]!=n_pict[y+a][x:x+m]:
        found=False
        break
    if found:
      print("Yes")
      exit()
print("No")

Submission status: Accepted

Solution code:
# import math
# import collections
# from pprint import pprint

# n = int(input())
n, m = map(int, input().split(' '))
# a = list(map(int, input().split(' ')))
a = []
for i in range(n):
    a.append(list(input()))
b = []
for i in range(m):
    b.append(list(input()))

judge = False
for offset_i in range(n - m + 1):
    for offset_j in range(n - m + 1):

        flg = 0
        for i in range(m):
            for j in range(m):
                if a[i + offset_i][j + offset_j] == b[i][j]:
                    flg += 1

        if flg == m * m:
            judge = True
            # print('find', offset_i, offset_j)

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[8]:


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


# In[11]:


f = False
for h in range(N-M+1):
    for w in range(N-M+1):
        for i in range(M):
            for j in range(M):
                if B[i][j] != A[i+h][j+w]:
                    break
            else:
                continue
            break
        else:
            f = True
            print("Yes")
            break
    else:
        continue
    break
if f != True:
    print("No")


# In[ ]:

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, M = map(int, readline().split())
    A = [0] * N
    for i in range(N):
        A[i] = readline().strip()
    B = [0] * M
    for i in range(M):
        B[i] = readline().strip()

    for i in range(N - M + 1):
        for j in range(N - M + 1):
            ok = True
            for x in range(M):
                for y in range(M):
                    if A[i + x][j + y] != B[x][y]:
                        ok = False
                        break
                if not ok:
                    break
            if ok:
                print('Yes')
                return

    print('No')
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
a,b=map(int,input().split())
ListA = []
for i in range (a):
  ListA.append(input())
ListB = []
for i in range (b):
  ListB.append(input())
flag = False
  
def checkAB(List1,List2,aa,bb):
  for k in range(b):
    if List1[aa+k][bb:bb+b] != List2[k]:
      return False
  return True
  
for i in range(a-b+1):
  for j in range(a-b+1):
    if checkAB(ListA,ListB,i,j):
      flag = True
if flag:
  print("Yes")
else:
  print("No")

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
a=[]
b=[]
for _ in range(n):
    x=[1 if s=='#' else 0 for s in input()]
    a.append(x)

for _ in range(m):
    x=[1 if s=='#' else 0 for s in input()]
    b.append(x)

for h in range(n-m+1):
    for w in range(n-m+1):
        tmpa=[a[x][w:w+m] for x in range(h,h+m)]
        if tmpa==b:
            print('Yes')
            exit()
print('No')

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
A = []
B = []
for i in range(N):
  a = str(input())
  A.append(a)
for i in range(M):
  b = str(input())
  B.append(b)
  
flag = False
for i in range(N-M+1):
  for j in range(N-M+1):
    if A[i][j:j+M] == B[0]:
      k = 1
      while k < M:
        if A[i+k][j:j+M] == B[k]:
          k += 1
        else:
          break
      if k == M:
        flag = True
        break
        
if flag:
  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)]
for i in range(N-M+1):
    for j in range(len(A[0])-len(B[0])+1):
        l=[]
        for k in range(M):
            l.append(A[i+k][j:j+M])
        if l==B:print('Yes');exit()
print('No')

Submission status: Accepted

Solution code:
def main():
    import sys

    def input(): return sys.stdin.readline().rstrip()
    
    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 = 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:
                break
        if not flag:
            break
    if not flag:
        print('Yes')
    else:
        print('No')
                
    

            

    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    import sys

    def input(): return sys.stdin.readline().rstrip()
    
    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
            for k in range(m):
                for l in range(m):
                    if a[i+k][j+l] != b[k][l]:
                        flag = False
                        break
                else:
                    continue
                break
            if flag:
                break
        else:
            continue
        break
    if flag:
        print("Yes")
    else:
        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)]

d = n - m + 1
flg = False
for i in range(d):
    for j in range(d):
        if a[i][j : j + m] == b[0]:
            for k in range(1, m):
                if a[i + k][j : j + m] != b[k]:
                    break
            else:
                print("Yes")
                flg = True
                break
    if flg:
        break
else:
    print("No")

Submission status: Accepted

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import product, accumulate, combinations, product
#import bisect# lower_bound etc
#import numpy as np
#from copy import deepcopy
#from collections import deque

def run():
    N,M = map(int, sysread().split())
    A, B = [],[]
    for i in range(N):
        A.append(input())
    for i in range(M):
        B.append(input())
    # extract candidates
    b = B[0]
    collection = []
    for i in range(N-M+1):
        a = A[i]
        for j in range(N-M+1):
            for k in range(M):
                if a[j+k] != b[k]:break
            collection.append((i,j))

    for start in collection:
        i, j = start
        breaked = False
        for x in range(M):
            if A[i+x][j:j+M] != B[x]:
                breaked = True
                break
        if not breaked:
            print("Yes")
            return None
    print("No")




if __name__ == "__main__":
    run()

Submission status: Accepted

Solution code:
import sys


N,M=map(int,input().split())
A=[sys.stdin.readline().strip() for _ in range(N)]
B=[sys.stdin.readline().strip() for _ in range(M)]

for i in range(N-M+1):
  for j in range(N-M+1):
    As=[x[i:i+M] for x in A[j:j+M]]
    if B==As:
      print('Yes')
      sys.exit()
print('No')

Submission status: Accepted

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

H = N - M + 1
flag = 'No'

for i in range(H):
    for j in range(H):
        cnt = 0
        for d in range(M):
            if B[d] != A[i+d][j:j+M]:
                break
            cnt += 1
        if cnt == M:
            flag = 'Yes'
            break
    else:
        continue
    break

print(flag)

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):
        cnt = 0
        for k in range(m):
            if a[i+k][j:j+m] == b[k]:
                cnt += 1
            if cnt == m:
                print("Yes")
                exit()
print("No")

Submission status: Accepted

Solution code:
import numpy as np
N,M = list(map(int,input().split()))

A=[]
B=[]
for n in range(N):
    A_ = list(input())
    A.append(A_)
    
for m in range(M):
    B_ = list(input())
    B.append(B_)
    
B=np.array(B)

ans='No'
for n1 in range(N-M+1):
    for n2 in range(N-M+1):
        check = np.array(A)[n1:n1+M,n2:n2+M]
        c=np.unique(check == B)
        
        if False not in set(c):
            ans='Yes'
            break
            
print(ans)

Submission status: Accepted

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

if n == m:
    print ("Yes") if A == B else print ("No")
    exit ()

for A_i in range(len(A)):
    for a_i in range(len(A[A_i])):
        if A[A_i][a_i] == B[0][0]:
            if m <= n-A_i and m <= n-a_i:
                tmp = []
                for i in range(A_i,A_i+m):
                    tmp_l = []
                    for j in range(a_i,a_i+m):
                        tmp_l.append(A[i][j])
                    tmp.append(tmp_l)
                if B == tmp:
                    print ("Yes")
                    exit ()
print ("No")

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
grida = []
gridb = []
for i in range(n):
    grida.append(list(input()))
for i in range(m):
    gridb.append(list(input()))

ans = False
for i in range(n-m+1):
    for j in range(n-m+1):
        bad = False 
        for k in range(m):
            for l in range(m):
                if grida[k+i][l+j] != gridb[k][l]:
                    bad = True
        if not bad:
            ans = True

if ans:
    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)]

if N == M:
    if A == B:
        print("Yes")
    else:
        print("No")
    exit()

for a_i in range(N-M):
    if B[0] in A[a_i]:
        #スタート地点を記憶
        start = []
        for a_j in range(N-M+1):
            if B[0] == A[a_i][a_j:M+a_j]:
                start.append(a_j)
        for s in start:
            f = True
            for b_i in range(M):
                #記録したスタート地点の列で、他の行も一致するか確認
                if B[b_i] != A[a_i+b_i][s:s+M]:
                    f = False
            if f == True:
                print("Yes")
                exit()
print("No")

Submission status: Accepted

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

def search(h,w):
    for i in range(m):
        if not b[i]==a[h+i][w:w+m]:
            return False
    return True

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

Submission status: Accepted

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

def chk(upper, left):
    for i in range(m):
        for j in range(m):
            if a[i+upper][j+left] != b[i][j]:
                return False
    return True

f = False
for upper in range(n-m+1):
    for left in range(n-m+1):
        if chk(upper, left):
            print('Yes')
            f = True
            break
    if f:
        break
if not f:
    print('No')

Submission status: Accepted

Solution code:
import numpy as np
N, M = 3, 2

ARR = [
    list("#.#"),
    list(".#."),
    list("#.#"),
]

ARR2 = [
    "#.",
    ".#"
]


#
#
# N,M = 4, 1
# ARR = [
#     "....",
#     "....",
#     "....",
#     "....",
# ]
#
# ARR2 = [
#     "#"
# ]
# #
# N,M = 5,2
#
# ARR = [
#     "...##.",
#     "###..#",
#     "...##.",
#     "......",
#     "....##"
# ]
#
# ARR2 = [
#     "......",
#     "....##"
# ]
#
N,M = map(int,input().split())
ARR = []

for i in range(N):
    ARR.append(list(input()))

ARR2 = []

for j in range(M):
    ARR2.append(input())


def calculate(n, m, arr1, arr2):
    arr1 = np.array(arr1)
    isOK = False
    for j in range(0, n - m + 1):
        for i in range(0, len(arr1[0]) - len(arr2[0]) + 1):
            # print(j, i)
            c1 = arr1[j:j + m,i:i + len(arr2[0])]
            s1 = np.ravel(c1)
            s1 = "".join(s1)
            s2 = "".join(arr2)
            if s1 == s2:
                isOK = True
                break

        if isOK == True:
            break

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


calculate(N, M, ARR, ARR2)

Submission status: Accepted

Solution code:
def grinp(h):
  ret=[]
  for i in range(h):
    ret.append(list(input()))
  return ret
n,m=map(int,input().split())
ngr=grinp(n)
mgr=grinp(m)

for i in range(n-m+1):
  for j in range(n-m+1):
    flag=True
    for k in range(m):
      for l in range(m):
        if ngr[i+k][j+l]!=mgr[k][l]:
          flag=False
          break
      if not flag:
        break
    if flag:
      print("Yes")
      break
  if flag:
    break
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())

for i in range(N-M+1):
    for j in range(N-M+1):
        f=True
        for k in range(M):
            if A[j+k][i:M+i]==B[k]:continue
            else:
                f=False
                break
        if f:
            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):
    for j in range(N-M+1):
        flg = True
        for k in range(M):
            for l in range(M):
                if A[i+k][j+l] == B[k][l]:
                    pass
                else:
                    flg=False
                    break
            if flg==False:
                break

        if flg:
            break
    if flg:
        break
if flg:
    print('Yes')
else:
    print('No')

Submission status: Accepted

Solution code:
import sys,math,collections,itertools
input = sys.stdin.readline

N,M=list(map(int,input().split()))

A = [input().rstrip() for _ in range(N)]
B = [input().rstrip() for _ in range(M)]

for h in range(N-M+1):
    for w in range(N-M+1):
        cutA = [ A[h+i][w:w+M] for i in range(M)]
        if cutA == B:
            print('Yes')
            exit()
        else:
            continue
        
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 di,dj in [(pi,pj) for pi in range(n-m+1) for pj in range(n-m+1)]:
    for i,j in [(pi,pj) for pi in range(m) for pj in range(m)]:
        if a[di+i][dj+j] != b[i][j]:
            break
    else:
        ans = 'Yes'
        break
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)]
ans = False
for pattern_h in range(n-m+1):
  for pattern_w in range(n-m+1):
    p = []
    for h in range(m):
      p.append(a[pattern_h+h][pattern_w:pattern_w+m])
    if p==b:
      ans = True
      break
  if ans: break
print('Yes' if ans else 'No')

Submission status: Accepted

Solution code:
# 53
import sys


def input(): return sys.stdin.readline().strip()
def I(): return int(input())
def LI(): return list(map(int, input().split()))
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def S(): return input()
def LS(): return input().split()


INF = float('inf')


n, m = LI()
a = SR(n)
b = SR(m)
ans = 'No'
for h in range(n - m + 1):
    for w in range(n - m + 1):
        is_ok = True
        for i in range(m):
            for j in range(m):
                if a[h + i][w + j] != b[i][j]:
                    is_ok = False
                    break
        if is_ok:
            ans = 'Yes'
            break
print(ans)

Submission status: Accepted

Solution code:
s = input().split()

n = int(s[0])
m = int(s[1])

N = [[0 for j in range(n)] for i in range(n)]

M = [[0 for j in range(m)] for i in range(m)]

for num in range(n):
    p = input()
    words = list(p)
    for number in range(n):
        N[num][number] = words[number]

for num in range(m):
    p = input()
    words = list(p)
    for number in range(m):
        M[num][number] = words[number]



exist = False

for num in range(n):
    for number in range(n):
        if num + m - 1 >= n or number + m - 1 >= n:
            continue

        check = True
        for i in range(m):
            for j in range(m):
                if N[num + i][number + j] != M[i][j]:
                    check = False
        
        if check:
            exist = True

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

Submission status: Accepted

Solution code:
import sys, os, math, bisect, itertools, collections, heapq, queue
# from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
from decimal import Decimal
from collections import defaultdict, deque

sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N, M = il()
    A = [iss() for _ in range(N)]
    B = [iss() for _ in range(M)]

    for i in range(N - M + 1):
        for j in range(N - M + 1):
            t = i
            for b in B:
                if A[t][j:j + M] == b:
                    t += 1
                else:
                    break
            else:
                print('Yes')
                exit()
    else:
        print('No')


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys, os, math, bisect, itertools, collections, heapq, queue
# from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
from decimal import Decimal
from collections import defaultdict, deque

sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N, M = il()
    A = [iss() for _ in range(N)]
    B = [iss() for _ in range(M)]

    for i in range(N - M + 1):
        for j in range(N - M + 1):
            t = i
            for b in B:
                # if t > M: break
                # if j + M > N: break
                if A[t][j:j+M] == b:
                    t += 1
                else:
                    break
            else:
                print('Yes')
                exit()
    else:
        print('No')


if __name__ == '__main__':
    main()

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):
    for j in range(n-m+1):
        is_match = True
        for k in range(m):
            for l in range(m):
                if a[i+k][j+l] != b[k][l]:
                    is_match = False
        if is_match:
            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 j in range(m):
    b.append(input())

# 考え：同じ文字列の並びがあるか各行、各列で見る
# 方針：画像の左上から各行ごとに先頭からAとBの文字列を比較していきマッチするかどうかを調べる

"""ボツ案
a_pix = []
b_pix = []
a_temp = []
b_temp = []

judge = ''

for i in a:
    for i_pix in i:
        a_temp.append(i_pix)
    a_pix.append(a_temp)
    a_temp = []

for j in b:
    for j_pix in j:
        b_temp.append(j_pix)
    b_pix.append(b_temp)
    b_temp = []

# print(a_pix)
# print(b_pix)
"""

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


        judge = 'Yes'
        for k in range(m):
            for l in range(m):
                # 横にずらして文字列を比較
                if a[i+k][j+l] != b[k][l]:
                    judge = 'No'

        if judge == 'Yes':
            print('Yes')
            exit()

print('No')

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(N-M+1):
    for j in range(N-M+1):
        flag = True
        for k in range(M):
            if A[i+k][j:j+M] != B[k]:
                flag = False
                break
        if flag:
            print("Yes")
            exit(0)

print("No")

Submission status: Accepted

Solution code:
from math import ceil,floor,comb,factorial,gcd,pow,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from copy import deepcopy
from time import time
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n, m = MAP()
a = [input() for i in range(n)]
b = [input() for i in range(m)]

ans = "No"
for i in range(n-m+1):
    for j in range(n-m+1):
        for k in range(m):
            for l in range(m):
                if a[i+k][j+l] != b[k][l]:
                    break
            else:
                continue
            break
        else:
            ans = "Yes"
            break
    else:
        continue
    break

print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(500000)
MOD = 10**9+7

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

def mi():
    return map(int, input().split())

def ii():
    return int(input())

def i2(n):
    tmp = [list(mi()) for i in range(n)]
    return [list(i) for i in zip(*tmp)]


def main():
    N, M = mi()
    A = [input() for i in range(N)]
    B = [input() for i in range(M)]

    for x in range(N-M+1):
        for y in range(N-M+1):
            bl = True
            for i in range(M):
                for j in range(M):
                    if A[x+i][y+j] != B[i][j]:
                        bl = False
            if bl:
                print('Yes')
                return
    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):#列
    g=1
    for k in range(m):#m
      
      if a[i+k][j:j+m]!=b[k]:
        g*=0
        break
    if g==1:
      print("Yes")
      import sys
      sys.exit()
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)]

ans = 'No'
for i in range(n-m+1):
    for j in range(n-m+1):
        match = 1
        for k in range(m):
            for l in range(m):
                if a[i+k][j+l] != b[k][l]:
                    match = 0
        if match:
            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)]
ans = False

for i in range(N-M+1):
  for j in range(N-M+1):
    flg = True
    for k in range(M):
      if A[i+k][j:j+M] != B[k]:
        flg = False
    if flg:
      ans = True

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