I've read the solutions to the LCS problem. But now there's a Longest Similar Subsequence problem: A sequence C is a similar subsequence of two sequences A, B if and only if C is a subsequence of A and we can replace at most K elements in C such that C is a subsequence of B.
For example, if A = "ABCDE", B = "BCAFE", K = 1, then the longest similar subsequence is "BCDE" ("BCDE is a subsequence of "ABCDE", and we can replace 'D' with 'A' or 'F' to make it a subsequence of "BCAFE").
My problem is that I've only come up with a recursive method to solve it, but obviously this is time-consuming, so I want to use DP instead. Any idea how to use DP to solve this problem?
My recursion method is like this:
LSS(i, j, k)
if(i == 0 or j == 0)
return 0
if(A[i] == B[j])
return LSS(i-1, j-1, k) + 1
if(k > 0)
return max(LSS(i-1, j-1, k-1) + 1, LSS(i-1, j, k), LSS(i, j-1, k))
else
return max(LSS(i-1, j, k), LSS(i, j-1, k))