Skip to main content
1 of 2
xskxzr
  • 7.6k
  • 5
  • 23
  • 47

You can add an extra parameter $q$, which represents a state of the DFA, for your dynamic programming.

Let $f(i,q)$ be the length of the longest subsequence of $A[1\ldots i]$ accepted by a DFA obtained by changing the end state of the primary DFA to $q$. Now $f(i+1,q)$ can be computed by comparing $f(i,q)$ (representing $A[i+1]$ is not chosen) and $f(i,p)+1$ (representing $A[i+1]$ is chosen) for each state $p$ that transforms to $q$ when reading $A[i+1]$.

xskxzr
  • 7.6k
  • 5
  • 23
  • 47