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 C# (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Collections;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using static System.Math;
using System.Numerics;
static class Program{
	const int mod=(int)1e9+7;
	static void Main(){
		Sc sc=new Sc();
		var s=sc.Ia;
		var h=new int[s[0]][];
		var t=new string[s[0]];
		for(int i = 0;i<s[0];i++) {
			h[i]=new int[s[1]];
			t[i]=sc.S;
		}
		var r=sc.Ia;
		var p=sc.Arr(r[0],()=>sc.S);
		Ac ac=new Ac(p,'0','z');
		var ra=new Rh(true);
		for(int i = 0;i<r[0];i++) {ra+=ac.tp[i].n;}
		for(int i = 0;i<s[0];i++) {ac.Match(t[i],(a,e)=>{h[i][a]=e.n;});}
		var li=new List<int[]>();
		for(int i = s[1]-1;i>=r[1]-1;i--) {
			var rb=new Rh(true);
			for(int j = 0;j<s[0];j++) {
				if(j<ra.d){rb+=h[j][i];}
				else{
					rb-=h[j-ra.d][i];
					rb+=h[j][i];
				}
				if(ra==rb){li.Add(new int[]{j+1-r[0],i+1-r[1]});}
			}
		}
		li=li.OrderBy(x=>x[0]).ThenBy(x=>x[1]).ToList();
		StringBuilder sb=new StringBuilder();
		for(int i = 0;i<li.Count;i++) {sb.Append(li[i][0]).Append(" ").Append(li[i][1]).Append("\n");}
		Console.Write(sb);
	}
}
public class Ac{
	static private int o;
	private int n,cnt=1;
	private Nd root;
	public Nd[] tp;
	public class Nd{
		public Nd[] ar;
		public List<Nd> li=new List<Nd>();
		public Nd sx;
		public List<string> stl=new List<string>();
		public int c,l=0,n;
		public bool b=false;
		public Nd(int c,int l){this.c=c-o;this.l=l;}
	}
	public Ac(string[] t,int a,int z){
		n=z-a+1;
		o=a;
		root=new Nd(a-1,0);
		tp=new Nd[t.Length];
		for(int i = 0;i<t.Length;i++) {
			var r=root;
			for(int j = 0;j<t[i].Length;j++) {
				if(!r.b){r.ar=new Nd[n];r.b=true;}
				if(r.ar[t[i][j]-o]==null){r.li.Add(r=r.ar[t[i][j]-o]=new Nd(t[i][j],j+1));cnt++;}
				else{r=r.ar[t[i][j]-o];}
			}
			tp[i]=r;
			r.stl.Add(t[i]);
		}
		int p=1;
		var qu=new Queue<Nd>();
		for(int i = 0;i<root.li.Count;i++,p++) {
			root.li[i].sx=root;
			if(root.li[i].b){qu.Enqueue(root.li[i]);}
			root.li[i].n=p;
		}
		while(qu.Count>0){
			var e=qu.Dequeue();
			for(int i = 0;i<e.li.Count;i++,p++) {
				var r=e.sx;
				while(r!=null&&(!r.b||r.ar[e.li[i].c]==null)){r=r.sx;}
				e.li[i].sx=r!=null?r.ar[e.li[i].c]:root;
				if(e.li[i].sx.stl.Count>0){e.li[i].stl.AddRange(e.li[i].sx.stl);}
				if(e.li[i].b){qu.Enqueue(e.li[i]);}
				e.li[i].n=p;
			}
		}
	}
	public void Match(string t,Action<int,Nd> f){
		var e=root;
		for(int i = 0;i<t.Length;i++) {
			while(e!=root&&(!e.b||e.ar[t[i]-o]==null)){e=e.sx;}
			if(e.ar[t[i]-o]!=null){e=e.ar[t[i]-o];}
			f(i,e);
		}
	}
}
public struct Rh{
	static readonly private int[] mod={1073741783,1073741789};
	static readonly private int[] b={1007,1009};
	public long[] h;
	public int d;
	public Rh(bool b){
		h=new long[]{0,0};
		d=0;
	}
	public static Rh operator+(Rh a,int c){
		for(int i=0;i<2;i++){a.h[i]=(a.h[i]*b[i]+c)%mod[i];}
		a.d++;
		return a;
	}
	public static Rh operator-(Rh a,int c){
		a.d--;
		for(int i=0;i<2;i++){a.h[i]=(a.h[i]+mod[i]-(Fp(b[i],a.d,mod[i])*c%mod[i]))%mod[i];}
		return a;
	}
	public static bool operator==(Rh a,Rh b){return a.h[0]==b.h[0]&&a.h[1]==b.h[1]&&a.d==b.d;}
	public static bool operator!=(Rh a,Rh b){return a.h[0]!=b.h[0]||a.h[1]!=b.h[1]||a.d!=b.d;}
	public override bool Equals(object obj){return false;}
	public override int GetHashCode(){return 0;}
	private static long Fp(long x,long e,int mod){
		long r=1;
		while(e>0){
			if((e&1)>0){r*=x;r%=mod;}
			x=(x*x)%mod;
			e>>=1;
		}
		return r;
	}
}

public class Sc{
	public int I{get{return int.Parse(Console.ReadLine());}}
	public long L{get{return long.Parse(Console.ReadLine());}}
	public double D{get{return double.Parse(Console.ReadLine());}}
	public string S{get{return Console.ReadLine();}}
	public int[] Ia{get{return Array.ConvertAll(Console.ReadLine().Split(),int.Parse);}}
	public long[] La{get{return Array.ConvertAll(Console.ReadLine().Split(),long.Parse);}}
	public double[] Da{get{return Array.ConvertAll(Console.ReadLine().Split(),double.Parse);}}
	public string[] Sa{get{return Console.ReadLine().Split();}}
	public object[] Oa{get{return Console.ReadLine().Split();}}
	public int[] Ia2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),int.Parse);}}
	public int[] Ia3(int a){return Array.ConvertAll((a.ToString()+" "+Console.ReadLine()).Split(),int.Parse);}
	public int[] Ia3(bool a,int b,bool c,int d){return Array.ConvertAll(((a?b.ToString()+" ":"")+Console.ReadLine()+(c?" "+d.ToString():"")).Split(),int.Parse);}
	public long[] La2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),long.Parse);}}
	public long[] La3(int a){return Array.ConvertAll((a.ToString()+" "+Console.ReadLine()).Split(),long.Parse);}
	public long[] La3(bool a,int b,bool c,int d){return Array.ConvertAll(((a?b.ToString()+" ":"")+Console.ReadLine()+(c?" "+d.ToString():"")).Split(),long.Parse);}
	public T[] Arr<T>(int n,Func<T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f();}return a;}
	public T[] Arr<T>(int n,Func<int,T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i);}return a;}
	public T[] Arr<T>(int n,Func<string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(Console.ReadLine().Split());}return a;}
	public T[] Arr<T>(int n,Func<int,string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i,Console.ReadLine().Split());}return a;}
}