Longest Common Subsequence of three strings
Given 3 strings X, Y and Z, the task is to find the longest common sub-sequence in all three given sequences.
Input:
Given input is the length of three string N, M, K and then in the next lines the strings X, Y, Z themselves respectively
Output:
Print the length of the longest common sub- sequence of the three strings
Constraints:
1<= N, M, K <=100
Example:
Input:
N = 5, M = 6, K = 7
X = "inclu"
Y = "socluue"
Z = "anyclue"
Output:
The length of longest common subsequence for these three strings are 3
Explanation:
The longest common subsequence for these three strings is:
"clu" which is of length 3
We need a 3D table to store the computed values.
Let's say for sub-sequences,
Now if X[i]==Y[j]==Z[k] then surely, we found a character which is common and we need to recur for the remaining ones
If they are not similar, we need to find maximum of three cases
So, if we formulate the above idea in to our recursion function then
Now, the above recursion will result to many overlapping sub problems. Hence, we need to convert the above to DP.
Obviously, visual illustration for the 3D DP calculation is not possible, but you can go through the computation for LCS between two strings to understand how this 3D table is being filled.
C++ Implementation:
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer