3
$\begingroup$

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).

$\endgroup$
  • 1
    $\begingroup$ 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. $\endgroup$ – D.W. Aug 2 '18 at 22:33
  • $\begingroup$ $O(n k)$ sounds optimal to me. $\endgroup$ – Jakube Aug 3 '18 at 11:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.