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)

#include <stdio.h>
#define mod 1000000009
#define e 32769

char s[1010][1010];
char data1[1010][1010];
int tttthassh[1010][1010];

int main(){
	int n,m,a,b;
	scanf("%d%d",&n,&m);
	for(int i=0;i<n;i++)scanf(" %s",s[i]);
	scanf("%d%d",&a,&b);
	for(int i=0;i<a;i++)scanf(" %s",data1[i]);
	long po=1;
	for(int j=0;j<b;j++)po=po*e%mod;
	long pop=1;
	for(int i=0;i<a;i++)pop=pop*po%mod;
	for(int j=0;j<m;j++){
		long hhhhhhash=0;
		for(int i=0;i<a;i++)hhhhhhash=(hhhhhhash*po+s[i][j])%mod;
		for(int i=a;i<=n;i++){
			tttthassh[i-a][j]=hhhhhhash;
			hhhhhhash=((hhhhhhash*po+s[i][j]-s[i-a][j]*pop)%mod+mod)%mod;
		}
	}
	long thash=0;
	for(int i=0;i<a;i++)for(int j=0;j<b;j++)thash=(thash*e+data1[i][j])%mod;

	for(int i=0;i<=n-a;i++){
		long hhhhhhash=0;
		for(int j=0;j<b;j++)hhhhhhash=(hhhhhhash*e+tttthassh[i][j])%mod;
		for(int j=b;j<=m;j++){
			if(thash==hhhhhhash)printf("%d %d\n",i,j-b);
			hhhhhhash=((hhhhhhash*e+tttthassh[i][j]-tttthassh[i][j-b]*po)%mod+mod)%mod;
		}
	}
	return 0;
}