Skip to main content
added 566 characters in body
Source Link
xskxzr
  • 7.6k
  • 5
  • 23
  • 47

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

Let $f(i,q)$$f(i,s)$ 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$$s$. Now $f(i+1,q)$$f(i+1,s)$ can be computed by comparing $f(i,q)$$f(i,s)$ (representing $A[i+1]$ is not chosen) and $f(i,p)+1$$f(i,t)+1$ (representing $A[i+1]$ is chosen) for each state $p$$t$ that transforms to $q$$s$ when reading $A[i+1]$.

But note, the time complexity this algorithm is not bounded by $O(qn)$ because we may have to check no more than constant $f(i,t)$'s for each computation. A simple improvement is to consider $A[i\ldots n]$ instead of $A[1\ldots i]$. Now $f(i,s)$ represents the length of the longest subsequence of $A[i\ldots n]$ accepted by a DFA obtained by changing the start state of the primary DFA to $s$, and $f(i,s)$ can be computed by comparing $f(i+1,s)$ and $f(i+1,t)+1$ where $s$ transforms to $t$ when reading $A[i]$. Now the running time is improved to $O(qn)$.

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]$.

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

Let $f(i,s)$ 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 $s$. Now $f(i+1,s)$ can be computed by comparing $f(i,s)$ (representing $A[i+1]$ is not chosen) and $f(i,t)+1$ (representing $A[i+1]$ is chosen) for each state $t$ that transforms to $s$ when reading $A[i+1]$.

But note, the time complexity this algorithm is not bounded by $O(qn)$ because we may have to check no more than constant $f(i,t)$'s for each computation. A simple improvement is to consider $A[i\ldots n]$ instead of $A[1\ldots i]$. Now $f(i,s)$ represents the length of the longest subsequence of $A[i\ldots n]$ accepted by a DFA obtained by changing the start state of the primary DFA to $s$, and $f(i,s)$ can be computed by comparing $f(i+1,s)$ and $f(i+1,t)+1$ where $s$ transforms to $t$ when reading $A[i]$. Now the running time is improved to $O(qn)$.

Source Link
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]$.