Printing Longest Common Subsequence
Given two strings, you have to find and print the longest common subsequence between them.
Input:
T Test case
T no of input string will be given to you.
E.g.
3
abcd abxy
sghk rfgh
svd vjhfd
Constrain
1≤ length (string1) ≤100
1≤ length (string2) ≤100
Output:
Print the length of the longest common subsequence formed from these two strings.
Example
T=3
Input:
abcd abxy
Output:
2 (xy)
Input:
sghk rfgh
Output:
2 (gh)
Input:
svd vjhfd
Output:
2 (vd)
Let there are two strings str1 and str2.
Using a dynamic programming algorithm to find the longest common subsequence between two given string is very efficient and fast as compared to the recursion approach.
Let f(a,b) = count the number of common subsequence from the two string starting from 0 to position a and starting from 0 to position b.
Considering the two facts:
For the two strings:
C++ Implementation:
Output
need an explanation for this answer? contact us directly to get an explanation for this answer