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
hieroph4nt: 2022-03-19 16:01:21

kmp with O(n1 * n2 + m1 * m2 * n1)

cena08: 2020-06-12 07:52:21

leave one blank line after each output.AC in one go

luongtx1998: 2019-12-15 04:01:30

Finally i get AC. Don't forget printing "NO MATCH FOUND..."

tab47: 2019-10-26 08:15:16

simple brute force works fine..

Last edit: 2019-10-26 08:16:16
johhnywhite: 2018-05-01 06:39:47

Implemented O(N1*N2 + M1*M2) solution in JAVA, passes all my local tests with very large data sets, here it says 'time limit exceeded'. There is no faster solution than O(N1*N2 + M1*M2). Also, haven't seen any java solution that has been accepted for this problem. Can authors/admins check acceptance criteria for JAVA submissions ?

Last edit: 2018-05-01 06:40:07
karthik1997: 2017-12-16 18:43:16

Rolling 2D Hash is also a very good approach . Please try it and m1 ,m2 <=2000 and n1,n2 <=300 . Really Good question . Try to run it in O(M1*M2 + n1*n2) . Better optimise memory usage .

vanvinhbk94: 2017-11-10 08:45:26

finally accepted ^^

coolio_1: 2017-07-02 20:35:57

m1*m2<=2000..definitely not!!!

rajeev_899: 2017-06-18 19:00:29

Nailed it :) :)

mehmetin: 2017-05-20 16:40:04

Solved with 2D rolling hash. And it should be something like 1 ≤ M1 , M2 ≤ 600 in the problem description.
[ assert(M1 <= 600 && M2 <= 600) doesn't fail ]
[ assert(M1 <= 550 && M2 <= 550) fails ]

Last edit: 2017-05-20 17:06:15

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