B. Two Tables

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You've got two rectangular tables with sizes na × ma and nb × mb cells. The tables consist of zeroes and ones. We will consider the rows and columns of both tables indexed starting from 1. Then we will define the element of the first table, located at the intersection of the i-th row and the j-th column, as ai, j; we will define the element of the second table, located at the intersection of the i-th row and thej-th column, as bi, j.

We will call the pair of integers (x, y) a shift of the second table relative to the first one. We'll call the overlap factor of the shift (x, y)value:

where the variables i, j take only such values, in which the expression ai, j·bi + x, j + y makes sense. More formally, inequalities 1 ≤ i ≤ na, 1 ≤ j ≤ ma, 1 ≤ i + x ≤ nb, 1 ≤ j + y ≤ mb must hold. If there are no values of variables i, j, that satisfy the given inequalities, the value of the sum is considered equal to 0.

Your task is to find the shift with the maximum overlap factor among all possible shifts.

Input

The first line contains two space-separated integers na, ma (1 ≤ na, ma ≤ 50) — the number of rows and columns in the first table. Thenna lines contain ma characters each — the elements of the first table. Each character is either a "0", or a "1".

The next line contains two space-separated integers nb, mb (1 ≤ nb, mb ≤ 50) — the number of rows and columns in the second table. Then follow the elements of the second table in the format, similar to the first table.

It is guaranteed that the first table has at least one number "1". It is guaranteed that the second table has at least one number "1".

Output

Print two space-separated integers x, y (|x|, |y| ≤ 109) — a shift with maximum overlap factor. If there are multiple solutions, print any of them.

Sample test(s)
input
3 2
01
10
00
2 3
001
111
output
0 1
input
3 3
000
010
000
1 1
1
output
-1 -1

解题说明:此题其实就是求两个矩阵中某些位置的乘积之和,找到其中的最大值,遍历即可。


#include <iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
using namespace std;

char mat[55][55],mat1[55][55];
int main()
{ 
	int na,nb,ma,mb,i,j,k,l,x,y,max=0,s;
	cin>>na>>ma;
	for (i=1; i<=na; i++)
	{
		for (j=1; j<=ma; j++) 
		{
			cin>>mat[i][j];
		}
	}
	cin>>nb>>mb;
	for (i=1; i<=nb; i++)
	{
		for (j=1; j<=mb; j++) 
		{
			cin>>mat1[i][j];
		}
	}
	for (k=-50; k<=50; k++)
	{
		for (l=-50; l<=50; l++)
		{ 
			s=0;
			for (i=1; i<=na; i++)
			{
				for (j=1; j<=ma; j++)
				{
					if (i+k>0 && i+k<=nb && j+l>0 && j+l<=mb)
					{
						s+=(mat[i][j]-48)*(mat1[i+k][j+l]-48);
					}
				}
			}
			if (max<s) 
			{
				max=s;
				x=k;
				y=l;
			}
		}
	}
	cout<<x<<" "<<y<<endl;
	return 0; 
}


你可能感兴趣的:(B. Two Tables)