Practice on Toph

Participate in exhilarating programming contests, solve unique algorithm and data structure challenges and be a part of an awesome community.


By shefin · Limits 2s, 256 MB

Bob is the owner of a shop named “Color Maker”. He has 256 types of watercolor in his shop. The types are numbered from 0 to 255. Bob stores the watercolors in small boxes. One box contains only one type of watercolor and there can be more than one box containing the same type of watercolor. Bob keeps the boxes on a shelf side by side. As he has a limited supply of watercolors, he sells the boxes of watercolors with a condition applied.
Bob arranges another set of sample boxes containing watercolors and keeps them on another shelf side by side. He does not sell these sample boxes.
After that, Bob numbers the boxes. Let the number of boxes on the first shelf is N and the number of the sample boxes is M. Then Bob numbers the boxes on the first shelf from 1 to N and the sample boxes from 1 to M.
When Alice wants to buy watercolors in this shop, Bob asks her to choose a box. If she chooses the kth box, Bob finds out a position j in the sample boxes and the number of boxes p such that:
Bk = Sj, Bk+1 = Sj+1, Bk+2 = Sj+2,…………..,Bk+p-1 = Sj+p-1, where Bi represents the ith box on the first shelf, Si represents the ith box of the sample boxes and k ≥ 1, k+p-1 ≤ N, j ≥ 1 and j+p-1 ≤ M. Bob then sells p boxes to Alice.

Surprisingly, Bob notices that there can be multiple ways and multiple values of p possible. Alice demands that among the ways, he has to choose the way in which the value of p is maximized and has to sell p boxes. Since calculating the maximum possible value of p is tough for Alice, she asks you to calculate this on behalf of her.


The first line of the input contains two integers N, M - the number of boxes on the first shelf and the number of the sample boxes.
The second line contains N space-separated integers. the ith integer represents the type of the watercolor ith box contains.
The third line contains M space-separated integers. the ith integer represents the type of the watercolor ith sample box contains.
The next line contains an integer Q, the number of the queries.
In each of the next Q lines, there will be an integer k, the position of the box Alice chooses.

1 ≤ N ≤ 106
1 ≤ M ≤ 103
0 ≤ type of watercolor ≤ 255
1 ≤ Q ≤ 106
1 ≤ k ≤ N


For each query, print in this format in a single line (without quotes): “Query x: y”, where x is the no. of the query and y is the maximum possible value of p described in the statement.


6 5
14 25 90 14 81 12
14 14 81 12 0
Query 1: 2
Query 2: 0
Query 3: 3


In the third Query, Alice chooses the 4th box. So, k = 4. To maximize the value of p,
the best way is to select the position 2 in the sample boxes. So, j = 2.
Thus, B4 = S2, B5 = S3, B6 = S4. Here, B4 = 14, B5 = 81, B6 = 12 and S2 = 14, S3 = 81, S4 = 12. As 3 boxes are matched in this way, the value of p is 3 here and its the maximum among other ways.
[B represents the boxes on the first shelf, S represents the sample boxes.]


64% Solution Ratio

tmwilliamlin168Earliest, 2w ago

EgorKulikovFastest, 0.3s

mahbubcsejuLightest, 27 MB

rafi_1703076Shortest, 1502B


Login to submit