Sequence Transformation
Limits: 2 sec., 512 MiB
Kerem and Asli are playing with sequences.
Kerem has a sequence \(S\) consisting of integer numbers, and his task is to split the sequence into a number (possibly one) of continuous parts. For each part independently, Asli then chooses any integer value and removes all occurrences of this value in the corresponding part (note that there could be no such occurrences). After that, Kerem will combine all parts in the same order to achieve a new sequence.
Their common goal is to obtain the sequence \(T\). What is the minimum number of parts that Kerem has to split the sequence to in order to achieve the goal?
Input
The first line contains two integers \(n\) and \(m\). The second line contains exactly \(n\) space-separated integers \(S_1, \ldots, S_n\) representing the initial sequence \(S\). The third line contains exactly \(m\) space-separated integers \(T_1, \ldots, T_m\) representing the final sequence \(T\).
Output
If it’s not possible to transform \(S\) into \(T\), print a single integer
-1. Otherwise, print the minimum number of parts.
Constraints
\(1 \le n, m \le 2000\),
\(1 \le S_i, T_i \le 10^9\).
Samples
| Input (stdin) | Output (stdout) |
|---|---|
| 4 2 4 2 7 2 4 7 | 1 |
| Input (stdin) | Output (stdout) |
|---|---|
| 7 2 4 7 1 4 1 7 1 4 7 | 3 |
| Input (stdin) | Output (stdout) |
|---|---|
| 3 2 7 4 2 4 7 | -1 |
Submit a solution
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|