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:
n, m = map(int,input().split())
a = [None]*n
b = [None]*m
for i in range(n):
  a[i] = input()
for j in range(m):
  b[j] = input()
r = 'No'
for i in range(n - m + 1):
  end_yoko = i + m
  a_t = [s[i:end_yoko] for s in a]
  for j in range(n - m + 1):
    end_tate = j + m
    if a_t[j:end_tate] == b:
      r = 'Yes'
print(r)

Submission status: Accepted

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

    Ai = []
    for _ in range(N):
        Ai.append(input())
    Bi = []
    for _ in range(M):
         Bi.append(input())

    return N, M, Ai, Bi


def solve(N, M, Ai, Bi):
    for xi in range(N - M + 1):
        if Ai[0][xi:xi + M] == Bi[0][:]:
            for yi in range(1, M):
                if not Ai[yi][xi:xi + M] == Bi[yi][:]:
                    break
            return True

    return False


if __name__ == '__main__':
    res = solve(*get_args())
    if res:
      print("Yes")
    else:
      print("No")

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
s=[input() for _ in range(n)]
t=[input() for _ in range(m)]
for i in range(n-m+1):
  for j in range(n-m+1):
    f=True
    for x in range(m):
      for y in range(m):
        if t[x][y]!=s[x+i][y+j]:
          f=False
          break
      if f is False:
        break
    if f:
      break
  if f:
    print("Yes")
    break
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 a:
    for j in b:
        if j not in i:
            print("No")
            quit()
print("Yes")

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):
        tmp = []
        for k in a[i:i+m]:
            tmp.append(k[j:j+m])
        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)]

def f(i, j):
  for s in range(M):
    for t in range(M):
      if A[i+s][j+t]!=B[s][t]:
        return False
  return True

for i in range(N-M+1):
  for j in range(N-M+1):
    r=f(i, j)
    if r==True:
      print('Yes')
      exit()
print('No')

Submission status: Accepted

Solution code:
def is_same(si, sj):
    for i in range(M):
        for j in range(M):
            if gridsA[sj + j][si + i] != gridsB[j][i]:
                return False
    return True


N, M = map(int, input().split())
gridsA = [input() for _ in range(N)]
gridsB = [input() for _ in range(M)]

if N == M:
    if is_same(0, 0):
        print('Yes')
    else:
        print('No')
else:
    for i in range(N - M):
        for j in range(N - M):
            if is_same(i, j):
                print('Yes')
                exit()
    print('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
cnt = 0
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]:
                cnt += 1
        if cnt == m:
            flag = 1
        cnt = 0
if flag == 1:
    print('Yes')
else:
    print('No')

Submission status: Accepted

Solution code:
def main():
    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):
            tmp=[]
            for k in range(m):
                tmp.append(A[i+k][j:j+m])
            if B==tmp:
                print('Yes')
                return
    print('No')
main()

Submission status: Accepted

Solution code:
import numpy as np
n,m = map(int,input().split())
a = np.array([list(input()) for _ in range(n)])
b = np.array([list(input()) for _ in range(m)])
flg = 0
for i in range(n-m+1):
  for j in range(n-m+1):
    if (a[i:i+m,j:j+m] == b).all():
      flg = 1
print("Yes" if flg == 1 else "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)]
r = any([r[j:j+m] for r in a[i:i+m]]==b for i in range(n-m+1) for j in range(n-m+1))
print("Yes" if r == True else "No")

Submission status: Accepted

Solution code:
import numpy as np
import sys
n,m=map(int,input().split())
a= [list(input()) for i in range(n)] 
b= [list(input()) for i in range(m)] 
na=np.array(a)

sa=n-m+1

for x in range(sa):
    for y in range(sa):

        if na[x:x+m,y:y+m].tolist()==b:
            print("Yes")
            sys.exit()
print("No")

Submission status: Accepted

Solution code:
INT = lambda: int(input())
INTM = lambda: map(int,input().split())
STRM = lambda: map(str,input().split())
STR = lambda: str(input())
LIST = lambda: list(map(int,input().split()))
LISTS = lambda: list(map(str,input().split()))
def do():
    n,m=INTM()
    A=[]
    B=[]
    flg=0
    for i in range(n):
        A.append(STR())
    for i in range(m):
        B.append(STR())
    
    for i in range(n-m+1):
        for j in range(n-m+1):
            if A[i][j:j+m] == B[0]:
                temp=1
                for k in range(1,m):
                    if A[i+k][j:j+m] != B[k]:
                        temp=0
                if temp==1:
                    flg=1
    if flg==1:
        print('Yes')
    else:
        print('No')
    
    
if __name__ == '__main__':
    do()

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')
            quit()
print('Yes')

Submission status: Accepted

Solution code:
from __future__ import print_function

import sys
input = sys.stdin.readline


def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)
    return


# import re
# import array
# import copy
# import functools
# import operator

# import math
# import string
# import fractions
# from fractions import Fraction
from fractions import gcd

# import collections
# import itertools
# import bisect

# import heapq
# from heapq import heappush
# from heapq import heappop
# from heapq import heappushpop
# from heapq import heapify
# from heapq import heapreplace
# from queue import PriorityQueue as pq
from queue import Queue

# from itertools import accumulate

# from collections import deque
# from collections import Counter

# from operator import mul
# from functools import reduce

# def lcm(n,m):
#     return int(n*m/gcd(n,m))

# def reduce(p, q):
#     common = fractions.gcd(p, q)
#     return (p//common , q//common )

# def combinations_count(n, r):
#     r = min(r, n - r)
#     numer = reduce(mul, range(n, n - r, -1), 1)
#     denom = reduce(mul, range(1, r + 1), 1)
#     return numer // denom


# import random
# import time
def crange(a, b, i):
    return range(a, b + 1, i)


def main():
    n, m = map(int, input().strip().split())
    a = [input().strip() for _ in range(n)]
    b = [input().strip() for _ in range(m)]
    n, m = (lambda x, y: (x - 1, y - 1))(n, m)
    eprint("n,m ", end=": ")
    eprint(n,m)
    # success_flag = 0
    ii, jj, colx, rowx = 0, 0, 0, 0
    for i in crange(0, n - m, 1):
        for j in crange(0, n - m, 1):
            failure_flag = 0
            for col in crange(0, m, 1):
                for row in crange(0, m, 1):
                    if a[i+col][j+row] != b[col][row]:
                        failure_flag = 1
                        ii, jj, colx, rowx = i, j, col, row
            if failure_flag == 0:
                print("Yes")
                eprint("ii,jj,colx,rowx ", end=": ")
                eprint(ii, jj, colx, rowx)
                return
    print("No")
    eprint("ii,jj,colx,rowx ", end=": ")
    eprint(ii, jj, colx, rowx)
    return


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 i in range(m)]
for i in range(n-m+1):
  row=a[i]
  if b[0] in row:
    j=row.find(b[0])
    c=0
    for k in range(i,i+m):
      if a[k][j:j+m]==b[k-i]:
        c+=1
      else:
        break
    if c==m:
      print("Yes")
      exit()
print("No")

Submission status: Accepted

Solution code:
#python3

def main():
    n, m = map(int, input().split())
    a = [input() for _ in range(n)]
    b = [input() for _ in range(m)]
    flag = False

    for i in range(n-m+1):
        for j in range(n-m+1):
            na = []
            for l in a[i:i+m]:
                na.append(l[j:j+m])
                if na == b:
                    flag = True
                

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

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)]

#二次元配列の部分配列を抽出する手法
#[l[i:m+i] for l in a[i:m+i]]
if m==1:
    for i in range(n):
        if b[0] in a[i]:
            print('Yes')
            exit()

    print('No')
    exit()
if m==n:
    if a==b:
        print('Yes')
        exit()
    else:
        print('No')
        exit()

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

Submission status: Accepted

Solution code:
def find_indices(a, b, L):
    # aからbのパターンをみつける
    return [i for i in range(L) if a[i:].startswith(b)]

def solve():
    N,M = map(int, input().split())
    A = [input() for _ in range(N)]
    B = [input() for _ in range(M)]
    idx = 0
    start_pos = {}
    for S_a in A:
        indices = set(find_indices(S_a, B[idx], N))
        if not start_pos:
            start_pos = indices
        else:
            start_pos = start_pos&indices
        
        if start_pos:
            idx += 1
        else:
            start_pos = {}
            idx = 0
            
        if idx == M:
            break
    
    print('Yes' if start_pos else 'No')
    
solve()

Submission status: Accepted

Solution code:
def check():
    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):
                if A[i+k][j:j+M] != B[k]:
                    flag = False
            if flag:
                return 'Yes'
    return 'No'
print(check())

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(0, N-M+1):
    pos = A[i].find(B[0])
    if pos >= 0:
        for j in range(1, M):
            if A[i+j][pos:pos+M] != B[j]:
                break
        else:
            print('Yes')
            break
else:
    print('No')

Submission status: Accepted

Solution code:
a,b = map(int,input().split())
l= []
L = []
for i in range(a):
    t= input()
    l.append(t)
for i in range(b):
    t = input()
    L.append(t)
f = 'No'
for i in range(a-b+1):
    for j in range(a-b+1):
        s = 0
        for I in range(b):
            for J in range(b):
                if l[i+I][j+J] == L[I][J] :s+=1
        if s==b**2:f='Yes'
print(f)

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())

def func(base, target):
   for i in range(n-m+1):
      check = base[i:i+m]
      for j in range(n-m+1):
         check2 = [line[j:j+m] for line in check]
         if check2 == target:
            return print('Yes')
   return print('No')

func(a,b)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from operator import itemgetter
sys.setrecursionlimit(10000000)
INF = 10**30

def main():
    n, m = list(map(int, input().strip().split()))
    a = []
    b = []
    for i in range(n):
        a.append(input().strip())
    for i in range(m):
        b.append(input().strip())
    for i in range(n):
        if i + m > n:
            break
        for j in range(n):
            if j + m > n:
                break
            flag = False
            for p in range(m):
                for q in range(m):
                    if b[p][q] != a[i+p][j+q]:
                        flag = True
                        break 
            if flag:
                continue
            else:
                print('Yes')
                return
    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(list(input()))
for i in range(m):
    b.append(list(input()))
num=n-(m-1)
ok=0 #一致したやつが０
def check(y,x):
    c=1 #一致した
    for i in range(m):
        for j in range(m):
            if a[y+i][x+j]!=b[i][j]:
                c=0
                break
        if c==0:
            break
    return c
for i in range(num):
    for j in range(num):
        #print(str(i)+str(j))
        if check(i,j)==1:
            ok=1
            break
    if ok==1:
        break
            
print("Yes" if ok==1 else "No")

Submission status: Accepted

Solution code:
N, M = [int(i) for i in 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):
        for ii in range(M):
            for jj in range(M):
                if A[i+ii][j+jj] != B[ii][jj]:
                    break
            else:
                continue
            break
        else:
            print("Yes")
            exit()
print("No")

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n,m=map(int,input().split())
A=[input().strip() for _ in range(n)]
B=[input().strip() for _ in range(m)]
for i in range(n-m+1):
    for j in range(n-m+1):
        flg=True
        for k in range(m):
            #print(A[i+k][j:j+m],B[k])
            if A[i+k][j:j+m]!=B[k]:
                flg=False
        if flg:
            print("Yes")
            exit()

print("No")

Submission status: Accepted

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

a_mat = [input() for _ in range(N)]
b_mat = ''.join([input() for _ in range(M)])


def to_vectors(mat, size=M):
    vectors = []
    for h in range(N-M+1):
        for w in range(N-M+1):
            tmp = [mat[h+i][w:w+M] for i in range(M)]
            vectors.append(''.join(tmp))
    return vectors


if b_mat in to_vectors(a_mat, M):
    print('Yes')
else:
    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 all_true(i,j):
    global a,b
    for k in range(i,i+m):
        for l in range(j,j+m):
            if a[k][l]!=b[k-i][l-j]:
                return False
    return True
f=0
for i in range(n-m+1):
    for j in range(n-m+1):
        if all_true(i,j):
            f=1
            break
    if f==1:
        print("Yes")
        break
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)]

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

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

Submission status: Accepted

Solution code:
[n,m]=list(map(int,input().split()))
alist=[]
blist=[]
counter=0
for i in range(n):
    alist.append(list(input()))
for i in range(m):
    blist.append(list(input()))
clist=[]
if n!=m:
    for i in range(n-m):
        for j in range(n-m):
            for k in range(m):
                kari=[]
                for l in range(m):
                    kari.append(alist[i+k][j+l])
                clist.append(kari)
            
            if blist==clist:
                counter=1
                break
            clist=[]

else:
    if alist==blist:
        counter=1

if counter==1:
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N, M = [int(x) for x in input().split()]
A = []
for _ in range(N):
    A.append(list(input().rstrip()))
B = []
for _ in range(M):
    B.append(list(input().rstrip()))
n = len(A[0])
m = len(B[0])
def search(point):
    x, y = point
    for i in range(M):
        if y > N - 1:
            return 0
        a = A[y][x:x + m]
        if a != B[i]:
            return 0
        y += 1
    return 1
for i in range(N):
    for j in range(n):
        if search([i, j]):
            print("Yes")
            sys.exit()
print("No")

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict, deque
import bisect
from heapq import *
from math import factorial, ceil, floor
from collections import defaultdict
import itertools
sys.setrecursionlimit(200000)
input = sys.stdin.readline

# N, M, = map(int, input().split())
# N = int(input())
# L = [int(v) for v in input().split()]
# L = [[int(v) for v in input().split()] for _ in range(N)]
# L = [int(input()) for _ in range(N)]
# L = [list(input().strip())  for _ in range(N)]
# S = input().strip()

N, M, = map(int, input().split())
a = [list(input().strip())  for _ in range(N)]
b = [list(input().strip())  for _ in range(M)]

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

        f = True
        for k in range(M):
            for l in range(M):
                if a[i + k][j + l] != b[k][l]:
                    f = False
        if f:
            print("Yes")
            exit()

print("No")

Submission status: Accepted

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

A = []
for i in range(N):
    tmp = str(input())
    A.append(tmp)
    
B = []
for i in range(M):
    tmp = str(input())
    B.append(tmp)
    
ans = "No"
for i in range(N-M+1):
    for j in range(N-M+1):
        if [a[j:j+M] for a in A[i:i+M]] == B:
            ans = "Yes"

print(ans)

Submission status: Accepted

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

aList = []
for i in range(n):
  aList.append(input())

bList = []
for j in range(m):
  bList.append(input())

for k in range(n-(m-1)):
  aIMatch = aList[k].find(bList[0])
  aICount = aList[k].count(bList[0])
  for ct in range(aICount):
 # if aIMatch >= 0:
    sucFlag = True 
    for l in range(1,m):
      aNMatch = aList[k+l].find(bList[l],aIMatch)
      if not(aNMatch >= 0 and aIMatch == aNMatch):
        sucFlag = False
        break
    if sucFlag:
      print("Yes")
      exit(0)
    aIMatch = aList[k].find(bList[0],aIMatch+1)

print("No")

Submission status: Accepted

Solution code:
import numpy as np
n,m = map(int,input().split())
A = np.array([list(input()) for _ in range(n)])
B = np.array([list(input()) for _ in range(m)])
bl = False

for i in range(n-m+1):
    for j in range(n-m+1):
        if (A[i:i+m,j:j+m] ==B).all():
            bl =True

print('Yes' if bl else '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):
        for k in range(m):
            if a[i+k][j:j+m] != b[k]:
                break
        else:
            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(list(input()))
    
for i in range(M):
    B.append(list(input()))

k=N-M+1
ans=False

for i in range(k):
    for j in range(k):
        flg=True
        
        for p in range(M):
            for q in range(M):
                if A[i+p][j+q] != B[p][q]:
                    flg=False
                    
        ans=flg
        if ans:
            break
    if ans:
        break

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

Submission status: Accepted

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

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

flag = False

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 a[i+k][j+l] == b[k][l]:
                    cnt += 1
        if cnt == m**2:
            flag = True
            break
    if flag:
        break
print("Yes" if flag else "No")

Submission status: Accepted

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

k = (N-1)-(M-1)+1

def judge(x, y):
    for i in range(M):
        for j in range(M):
            if not(A[x+i][y+j]==B[i][j]):
                return False
    return True


for i in range(k):
    for j in range(k):
        if judge(i, j):
            print("Yes")
            quit()

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 f(i, j):
  for k in range(M):
    for l in range(M):
      if A[i+k][j+l] != B[k][l]:
        return False
  return True

def slv():
  for i in range(N-M+1):
    for j in range(N-M+1):
      if f(i, j):
        return 'Yes'
  return 'No'
print(slv())

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from itertools import product

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

b = "".join(B)

ans = "No"
for i, j in product(range(N - M + 1), repeat=2):
    a = "".join(["".join(x[j:j + M]) for x in A[i:i + M]])
    if a == b:
        ans = "Yes"
        break
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

n,m = map(int, input().split())

a = []
b = []

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


#上段が一致している箇所を探索
upperMatched = []

b0 = b[0]
for i in range(n-m+1):
    for j in range(n-m+1):
        #print("-----")
        #print("i:",i,"j:",j)
        #print("a[j][i:i+m]:", a[j][i:i+m])
        if b0 == a[j][i:i+m]:
            #print("--upper found--")
            #print("i:",i,"j:",j)
            upperMatched.append([i,j])
            #print("---------------")
#print(upperMatched)

#一か所も一致がなければ
if len(upperMatched) == 0:
    print("No")
    exit()

#print("--------------")
#そうでなければ、上段以降が一致しているか確認
for uM in upperMatched:
    isMatched = True
    i = uM[0]
    j = uM[1]

    for r in range(1, m):
        #print("b[r]:",b[r])
        #print("a[i+r][j:j+m]:",a[i+r][j:j+m])
        if b[r] == a[i+r][j:j+m]:
            pass
        else:
            isMatched = False
    if isMatched:
        print("Yes")
        exit()
    else:
        pass

print("No")

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

def build_grid(H, W, intv, _type, space=True, padding=False):
    # 入力がスペース区切りかどうか
    if space:
        _input = lambda: input().split()
    else:
        _input = lambda: input()
    _list = lambda: list(map(_type, _input()))
    # 余白の有無
    if padding:
        offset = 1
    else:
        offset = 0
    grid = list2d(H+offset*2, W+offset*2, intv)
    for i in range(offset, H+offset):
        row = _list()
        for j in range(offset, W+offset):
            grid[i][j] = row[j-offset]
    return grid

N, M = MAP()
grid1 = build_grid(N, N, '', str, space=0)
grid2 = build_grid(M, M, '', str, space=0)

def check(i, j):
    for k in range(i, i+M):
        for l in range(j, j+M):
            if grid1[k][l] != grid2[k-i][l-j]:
                return False
    return True

for i in range(N-M+1):
    for j in range(N-M+1):
        if check(i, j):
            Yes()
            exit()
No()

Submission status: Accepted

Solution code:
m,n = map(int,input().split())
a = []*m
b = []*n
flag = False
for i in range(m):
    a.append(input())
for j in range(n):
    b.append(input())
flag = False
for i in range(m-n+1):#比較開始を縦方向に動かす
    for j in range(m-n+1):#比較開始を横方向へ動かす
        for k in range(n):#各行を1行ずつ比較
            if b[k] != a[i+k][j:j+n]:
                break
        else:
            flag = True
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(n-m+1):
        ans = 0
        for ii in range(m):
            for jj in range(m):
                if b[ii][jj] == a[i+ii][j+jj]:
                    ans += 1
        if ans == m*m:
            print("Yes")
            exit()

print("No")

Submission status: Accepted

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

flag = False
for i in range(n-m+1):
    if flag: break
    for j in range(n-m+1):
        suba = a[i:i+m]
        for k in range(len(suba)):
            suba[k] = suba[k][j:j+m]
        if suba == b:
            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(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')
            break
    if flag:
        break

if not flag:
    print('No')

Submission status: Accepted

Solution code:
import sys
N, M = map(int, input().split())
A = []
B = []
for _ in range(N):
    a1 = input()
    A.append(a1)
for _ in range(M):
    b1 = input()
    B.append(b1)
    
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]:
                pass
            else:
                break
            if k == M-1:
                print("Yes")
                sys.exit()
print("No")

Submission status: Accepted

Solution code:
# coding: utf-8
N, M = map(int, input().split())
A, B = [], []
for i in range(N):
    tmp = input()
    A.append(tmp[:])
for i in range(M):
    tmp = input()
    B.append(tmp[:])
for r in range(len(A[0]) - len(B[0]) + 1):
    for i in range(N - M + 1):
        cnt = 0
        for j in range(i, M+i):
            # print(A[j][r:r+len(B[0])], B[j-i])
            if A[j][r:r+len(B[0])] == B[j-i]:
                cnt += 1
        if cnt == M:
            print("Yes")
            exit()
        # print()
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 = False
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:
            ans = True
            break
    if ans:
        break
print('Yes' if ans else 'No')

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

N, M = map(int, input().split())
A = list(list(input()) for i in range(N))
B = list(list(input()) for i in range(M))

ans = False
for i in range(N):
    for j in range(N):
        if i + M - 1 >= N or j + M - 1 >= N:
            continue
        match = True
        for y in range(M):
            for x in range(M):
                if A[i + y][j + x] != B[y][x]:
                    match = False
        if match:
            ans = True

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