Problem:
Pattern Search

Find places where a R × C pattern is found within a H × W region. Print top-left coordinates (i, j) of sub-regions where the pattern found. The top-left and bottom-right coordinates of the region is (0, 0) and (H-1, W-1) respectively.

Input

In the first line, two integers H and W are given. In the following H lines, i-th lines of the region are given.

In the next line, two integers R and C are given. In the following R lines, i-th lines of the pattern are given.

output

For each sub-region found, print a coordinate i and j separated by a space character in a line. Print the coordinates in ascending order of the row numbers (i), or the column numbers (j) in case of a tie.

Constraints

1 ≤ H, W ≤ 1000

1 ≤ R, C ≤ 1000

The input consists of alphabetical characters and digits

Sample Input 1

4 5
00010
00101
00010
00100
3 2
10
01
10

Sample Output 1

0 3
1 2

Solution in OCaml (judge verdict: Accepted)

open Scanf
open Printf
       
let id x = x
let tuple x y = (x, y)
             
let calc_rh a h w r c =
  let bh = 10007 in
  let bv = 999983 in
  let rec pow x n =
    if n = 0 then 1
    else if n mod 2 = 0 then let y = pow x (n / 2) in y * y
    else x * pow x (n-1) in
  let rh a b i j =
    let rec iter r k =
      if k = j then r
      else iter (r * b + a.(k)) (k+1) in
    iter 0 i
  in
  let bh' = pow bh c in
  let ta = Array.make_matrix h (w - c + 1) 0 in
  let rec set_ta i j =
    if i = h then ()
    else if j = w - c + 1 then set_ta (i+1) 0
    else begin
        if j = 0 then ta.(i).(j) <- rh a.(i) bh 0 c
        else ta.(i).(j) <- ta.(i).(j-1) * bh + a.(i).(j+c-1) - a.(i).(j-1) * bh';
        set_ta i (j+1)
      end
  in
  let bv' = pow bv r in
  let rht = Array.make_matrix (h - r + 1) (w - c + 1) 0 in
  let rec set_rht i j =
    if j = w - c + 1 then ()
    else if i = h - r + 1 then set_rht 0 (j+1)
    else begin
        if i = 0 then rht.(i).(j) <- rh (Array.init r (fun k -> ta.(k).(j))) bv 0 r
        else rht.(i).(j) <- rht.(i-1).(j) * bv + ta.(i+r-1).(j) - ta.(i-1).(j) * bv';
        set_rht (i+1) j
      end
  in
  set_ta 0 0;
  set_rht 0 0;
  rht
    
let () =
  let h, w = scanf "%d %d " tuple in
  let fld = Array.init h (fun _ ->
                          let s = scanf "%s " id in
                          Array.init w (fun i -> 
                                        Char.code s.[i]
                                     )
                         ) in
  let r, c = scanf "%d %d " tuple in
  let ptn = Array.init r (fun _ ->
                          let s = scanf "%s " id in
                          Array.init c (fun i -> 
                                        Char.code s.[i]
                                       )
                         ) in
  let ft = calc_rh fld h w r c in
  let pt = calc_rh ptn r c r c in
  let rec loop i j =
    if i = h - r + 1 then ()
    else if j = w - c + 1 then loop (i+1) 0
    else begin
        if ft.(i).(j) = pt.(0).(0) then printf "%d %d\n" i j;
        loop i (j+1)
      end
  in
  loop 0 0