#P4696. Subarray Permutation Matching

    ID: 17940 Type: Default 1000ms 256MiB

Subarray Permutation Matching

Subarray Permutation Matching

Given a permutation (p = (p_1, p_2, \ldots, p_n)) of ({1,2,\ldots,n}) and an integer sequence (h = (h_1, h_2, \ldots, h_m)), we say that a subarray (contiguous segment) (a = (a_1, a_2, \ldots, a_n)) of (h) satisfies the permutation (p) if and only if:

  1. All elements in (a) are distinct.
  2. If we sort (a) in increasing order, we obtain [ (a_{p_1}, a_{p_2}, \ldots, a_{p_n}) ] that is, for every (k) from (1) to (n), the (k)-th smallest element in (a) is (a_{p_k}).

Your task is to find all subarrays of (h) of length (n) that satisfy the permutation (p].

Note: The subarrays are contiguous segments of (h). If no valid subarray exists, output -1.

inputFormat

The first line contains two integers (n) and (m) where (n) is the length of the permutation and (m) is the length of the sequence (h).
The second line contains (n) distinct integers denoting the permutation (p) (a permutation of (1) through (n)).
The third line contains (m) integers denoting the sequence (h).

outputFormat

Print the 1-indexed starting positions of all subarrays of length (n) in (h) that satisfy the permutation (p), separated by a space. If there is no such subarray, print -1.

sample

3 5
2 1 3
2 1 3 2 5
1 3