#C9841. Padlock Unlocking via Contiguous Sequence

    ID: 53979 Type: Default 1000ms 256MiB

Padlock Unlocking via Contiguous Sequence

Padlock Unlocking via Contiguous Sequence

You are given an attempted sequence of numbers and a correct padlock combination. The padlock unlocks if the correct combination appears as a contiguous subsequence in the attempted sequence. The catch is that the padlock only allows a limited number of entries. Formally, you are given two integers (n) and (m) where (n) is the length of the attempted sequence and (m) is the maximum allowed number of entries. In addition, you are given the attempted sequence of (n) integers and the correct sequence (combination) which is of length (k). The padlock unlocks if there exists an index (i) (0-based) such that the subarray (attemptedSequence[i], attemptedSequence[i+1], \ldots, attemptedSequence[i+k-1]) exactly matches the correct sequence and (k \leq m). If the combination can be found under these conditions, output (k) (i.e., the number of entries needed); otherwise, output -1.


The mathematical formulation can be expressed in (\LaTeX) as follows: [ \text{Find } i \text{ such that } (a_i, a_{i+1}, \ldots, a_{i+k-1}) = (c_0, c_1, \ldots, c_{k-1}) \quad \text{and} \quad k \leq m. ] If such an index exists, the answer is (k); otherwise, the answer is -1.

inputFormat

The input is read from standard input and consists of three lines. The first line contains two integers (n) and (m), where (n) is the number of entries in the attempted sequence and (m) is the maximum allowed number of entries. The second line contains (n) integers separated by spaces representing the attempted sequence. The third line contains a sequence of integers separated by spaces representing the correct padlock combination. (\newline) Example:
10 5
1 2 3 4 5 6 7 8 9 10
3 4 5 6

outputFormat

Output a single integer to standard output. If the correct combination is found as a contiguous subsequence and the length of the combination is less than or equal to (m), output the length of the combination (i.e., (k)). Otherwise, output -1.## sample

10 5
1 2 3 4 5 6 7 8 9 10
3 4 5 6
4