Codeforces Round #141 (Div. 2)——B

来源:互联网 发布:sql数据库没有服务器 编辑:程序博客网 时间:2024/06/09 16:56
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 thej-th column, as ai, j; we will define the element of the second table, located at the intersection of thei-th row and the j-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. Then na 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 20110002 3001111
output
0 1
input
3 30000100001 11
output
-1 -1

#include <iostream>#include <cstdio>#include <cstring>using namespace std;#define maxn 100char sa[maxn][maxn];char sb[maxn][maxn];int na,nb,ma,mb;int value(int x,int y){    int sum=0;    for(int i=0; i<na; i++)        for(int j=0; j<ma; j++)            if(x+i>=0&&x+i<nb&&y+j>=0&&y+j<mb)            {                if(sa[i][j]=='1'&&sb[i+x][j+y]=='1') sum++;            }    return sum;}int main(){    int i;    scanf("%d%d\n",&na,&ma);    for(i=0; i<na; i++)        scanf("%s",sa[i]);    scanf("%d%d\n",&nb,&mb);    for(i=0; i<nb; i++)        scanf("%s",sb[i]);    int max=0;    int idx=0,idy=0;    for(int x=-50; x<50; x++)        for(int y=-50; y<50; y++)        {            int sum=value(x,y);            if(max<sum)            {                max=sum;                idx=x;                idy=y;            }        }    printf("%d %d\n",idx,idy);    return 0;}


原创粉丝点击