Design, test, and run a recursive static method, lengthLCS, which has two String objects for its parameters, and returns the length of the longest common subsequence of the two strings. This is a problem that has been of interest to biologists who try to measure the closeness (or homology) of two sequences (for example, RNA or DNA). For example, if the two strings are AZBBACXCABACC and CBBACCCAABBCXYZ, then the longest common subsequence (BBACCABC) has length 8. Note that our program does not actually produce a longest common subsequence, but only measures its length.
Sketch of the recursive algorithm:
Suppose that str1 and str2 are given. If either string has length 0, then the method should return 0.
Next, suppose that str1 and str2 each has length at least 1.
If str1.charAt(0) equals str2.charAt(0), then the length of a longest common
subsequence of str1 and str2 is 1 more than the length of a longest common
subsequence of str1.substring(1) and str2.substring(1).
If str1.charAt(0) is different from str2.charAt(0), then the length
of a longest common subsequence of str1 and str2 is the larger of the lengths
of a longest common subsequence of the pair str1
and
str2.substring(1) and of the pair str2 and
str1.substring(1).
Sample output:
Welcome to Dr. Moreau's longest common subsequence program.
Please enter your first
string:
ABBCCCAABCABC
Please enter your second
string:
ACBBCABDABBCA
Thank you. The length of a longest common subsequence is 9.
Do you wish to continue?
If so enter Yes; if not, enter No:
Yes
Please enter your first
string:
CAABCCA
Please enter your second
string:
ACABCCCC
Thank you. The length of a longest common subsequence is 5.
Do you wish to continue?
If so enter Yes; if not, enter No:
No
Thank you for using Dr.
Moreau's program. Press enter to terminate this session.
What to submit:
All of the following should be contained in a two-pocket folder with your name, "Comp 170", and "day"/"evening" designation in large letters on the outside of the folder.
I remember too that night which is at the
middle of the Thousand and One Nights when Scheherazade (through a magical
oversight of the copyist) begins to relate word for word the story of the
Thousand and One Nights, establishing the risk of coming once again to
the night when she must repeat it, and thus to infinity.
- Jorge Luis Borges, The Garden of Forking Paths