# Sequence matching

Let $T$ and $P$ be respectively two sequences $t_1, · · · , t_n$ and $p_1, · · · , p_k$ of characters such that $k ≤ n$. The characters range over a finite alphabet Σ. With each position of $T$, we associate a non-negative value. Let $v(i)$ denote the value for the i-th position in $T$ for $1 ≤ i ≤ n$. Find a matching subsequence of $T$ for the pattern $P$ that maximizes the sum of values. That is, find the sequence of indices $1 ≤ i_1 < i_2 < · · · < i_k ≤ n$ such that for all $1 ≤ j ≤ k$, we have $t_{i_j} = p_j$ and $\sum$ $v(i_j )$ is maximized.

I have a $O(nk)$ solution, wondering if it's the most efficient (I'm being told $O(nk^2)$ is the best you can do).

• Welcome to Computer Science! Note that you can use LaTeX here to typeset mathematics in a more readable way. See here for a short introduction. – D.W. Aug 2 '18 at 22:33
• $O(n k)$ sounds optimal to me. – Jakube Aug 3 '18 at 11:48