ARDA1 - The hunt for Gollum


Along the skirts of the Dead Marshes I followed it, and then I had him. Lurking by a stagnant mere, peering in the water as the dark eve fell, I caught him, Gollum. He was covered with green slime. He will never love me, I fear; for he bit me, and I was not gentle. Nothing more did I ever get from his mouth than the marks of his teeth.

“The Lord of the Rings: The fellowship of the Ring”

 

Hearing Gandalf’s advice, Aragorn has started hunting creature Gollum. After several days following his footprints, he has arrived to the Dead Marshes. He has a map of the marshes, that can be viewed as an M1 × M2 matrix containing lowercase letters form English alphabet (i.e. letters from ‘a’ to ‘z’).

Being a skilled ranger, Aragorn has been able to fully characterize Gollum preferred place (if you are interested, you should know that it must be dark, wet, creepy and full of fishes!). It can be described as an N1 × N2 matrix containing lowercase letters form English alphabet.

Your task is simple: write a program that, given Gollum’s preferred place description and Aragorn’s map, output all possible locations of the creature.

Let’s look at an example: suppose Gollum’s preferred place is described by the following 3 × 3 matrix:

  • aba
  • bab
  • aba

and that Aragorn’s map looks something like this:

  • aababa
  • ababab
  • bababa
  • ababab
  • ababab
  • bababa
  • ababab

Then your program must output the following: (1,2), (1,4), (2,1), (2,3), (5,1) and (5,3), these being the upper-left corners of all places on the Dead Marshes that match Gollum’s preferred place description. If none match is found, you should output the string “NO MATCH FOUND...” without the quotes.

Input

Line 1: Two integers: N1 and N2.

Lines 2… N1 + 1: A string with N2 characters as described above.

Lines N1 + 2: Two integers: M1 and M2.

Lines N1 + 3… N1 + M1 + 3: A string with M2 characters as described above.

Constraints

1 ≤ N1, N2 ≤ 300

1 ≤ M1 * M2 ≤ 2000

N1 ≤ M1

N2 ≤ M2

Output

On each line print the upper-left corner of all places that match Gollum’s preferred place description on the form “(x,y)” without the quotes, where x stands for the row and y for the column. They should be lexicographically sorted, i.e. imagine them as an ordered pair. Then (x1, y1) < (x2, y2) if and only if x1 < x2 or, if they are equal, y1 < y2.

Example

Input:
3 3
aba
bab
aba
7 6
aababa
ababab
bababa
ababab
ababab
bababa
ababab

Output:
(1,2)
(1,4)
(2,1)
(2,3)
(5,1)
(5,3)

hide comments
VV: 2013-08-05 15:50:10

got AC with O(N1*N2 + M1*M2)

Last edit: 2013-08-05 15:51:28
Kousik Kumar: 2013-05-16 20:26:56

NO MATCH FOUND...

forhad ahmed: 2012-10-17 17:24:51

Is it true in Input constraint:
M1 * M2 <=2000
???
assert case give me runtime error... please check it

Oleg: 2010-08-23 10:51:48

I have O(n1*n2 + m1*m2).

Last edit: 2010-08-23 13:09:54
Nikhil Garg: 2010-08-22 20:47:01

There is. I have written an O(n1*m1*m2 + n1*n2) algorithm.

Ishan: 2010-06-12 10:37:57

O(m1*m2*n1*n2) is getting AC . Isn't there a better algorithm?

Last edit: 2010-06-12 10:38:33
~!(*(@*!@^&: 2010-04-19 01:17:28

NO MATCH FOUND...

Carlos Eduardo Rodrigues Alves [USJT]: 2010-03-18 21:20:59

Can Gollum's place be rotated or mirrored (the example is symmetric)? Also, the input and output examples have extra line feeds. Are they important?

Edit: the answers are NO and NO.

Last edit: 2010-03-19 03:45:14

Added by:Camilo Bravo Valdés
Date:2010-03-11
Time limit:0.405s-5s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: NODEJS OBJC PERL6 SQLITE VB.NET
Resource:own problem