Ticker

6/recent/ticker-posts

Header Ads Widget

Responsive Advertisement

Close Strings


A is the square matrix of size N. Every entry in the matrix will contain a lower case alphabet. Given Z strings, for each string your task is to find out the closest square-matrix starting from (1, 1). Closeness between the sub-matrix and the string is defined as, number of similar characters between the two. The input is guaranteed to have at least one character that is same in string Zi and matrix A, where Zi is the ith string. The same cell in the sub-matrix of A cannot be counted more than once to match with the character in the S.

Input Format: 
First line of input will consist of T Test cases. Each test case will consist of four parts, viz.
1. Size of the matrix (N)
2. The matrix itself (A = N * N) where elements are delimited by space.
3. Number of Strings (Z)
4. Z lines each comprising of string S

Output Format: 
For every string print the bottom right co-ordinates of the square-matrix closest from (1, 1).

Constraints:
1<=T<=5
1<=N<=500
'a'<=Aij<='z', where Aij is the entry of the matrix at ith row and jth column(1-index based). 
1<=Z<=25000
1<=|S|<=500

Sample Input and Output

Input

Output

1
4
a e r d
p l x l
l p x z
c q x o
3
apple
alex
aero

3 3
3 3
4 4


Explanation:
String S: "apple"
Sub matrix: (1, 1) to (3, 3)
a e r
p l x
p x
Sub matrix contains all characters of string S, hence the output

Post a Comment

0 Comments