JavaScript (ES6), 93 bytes
a=>b=>a.reduce(r=>1/r[p=b.map((m,j)=>t=m-a[i]?p[j]:j?p[j-1]:i),++i-t]?a.slice(t,i):r,a,i=p=0)
For inputs \$ a\left[1\dots N\right] \$ and \$ b\left[1\dots M\right] \$
Let matrix \$p\$ be
$$ p_{i,j}=\begin{cases} i & b_j=a_i \land j=1 \\ p_{i-1,j-1} & b_j=a_i \land j>1 \land i>1 \\ p_{i-1,j} & b_j\ne a_i \land i > 1 \\ -\infty & \text{otherwise} \\ \end{cases} $$
the minimal length of output \$L\$ is
$$ L = \min_{i \in 1\dots N} i-p_{i,N} $$
And the output is
$$ a\left[p_{i,N}\dots i\right] $$
where \$i\$ is the value for minimal \$L\$.
- 36.2k
- 2
- 36
- 133