228B.md (2481B)
1 Source: https://codeforces.com/problemset/problem/228/B 2 3 B. Two Tables 4 5 time limit per test 6 7 2 seconds 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 stdin 16 17 output 18 19 stdout 20 21 You've got two rectangular tables with sizes *n**a* × *m**a* and *n**b* × *m**b* 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 *a**i*, *j*; we will define the element of the second table, located at the intersection of the *i*-th row and the *j*-th column, as *b**i*, *j*. 22 23 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: 24 25  26 27 where the variables *i*, *j* take only such values, in which the expression *a**i*, *j*·*b**i* + *x*, *j* + *y* makes sense. More formally, inequalities 1 ≤ *i* ≤ *n**a*, 1 ≤ *j* ≤ *m**a*, 1 ≤ *i* + *x* ≤ *n**b*, 1 ≤ *j* + *y* ≤ *m**b* 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. 28 29 Your task is to find the shift with the maximum overlap factor among all possible shifts. 30 31 Input 32 33 The first line contains two space-separated integers *n**a*, *m**a* (1 ≤ *n**a*, *m**a* ≤ 50) — the number of rows and columns in the first table. Then *n**a* lines contain *m**a* characters each — the elements of the first table. Each character is either a "0", or a "1". 34 35 The next line contains two space-separated integers *n**b*, *m**b* (1 ≤ *n**b*, *m**b* ≤ 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. 36 37 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". 38 39 Output 40 41 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. 42 43 Examples 44 45 Input 46 47 ``` 48 3 2 49 01 50 10 51 00 52 2 3 53 001 54 111 55 ``` 56 57 Output 58 59 ``` 60 0 1 61 ``` 62 63 Input 64 65 ``` 66 3 3 67 000 68 010 69 000 70 1 1 71 1 72 ``` 73 74 Output 75 76 ``` 77 -1 -1 78 ```