Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Part I: Maximum increasing subsequence (adapted from Programming Exercise 22.2 25 points) Write a program MaxIncreasingSubseq.java that prompts the user to enter a string and
Part I: Maximum increasing subsequence (adapted from Programming Exercise 22.2 25 points) Write a program MaxIncreasingSubseq.java that prompts the user to enter a string and displays a maximum length increasing subsequence of characters. Here are four sample runs: Enter a string: zebras ers Enter a string: Welcome Welo Enter a string: mmmoooooovvvvee mov Enter a string: abqrstcxyzdefghij abcdefghij You must use dynamic programming, as discussed below and in the Dynamic Programming - HW2.ppt notes on Canvas, and your algorithm must run in O(n) time, where n is the length of the input string. Also, in your program, you don't want to return the max score, you want to display the substring that results in the max score, i.e., display the maximum increasing subsequence. That is what the prev array is for. 5 6 1 4 4 In the Welcome example above we would have: i: 0 1 2 3 4 charAt(i) W' 'e' 1 'c' 'o' 'm 'e' score[i] 2 3 2 3 prev[i] 0 1 0 2 3 Since the maximum score is achieved at i = 4 and at i = 5, the maximum increasing subsequence should end at the 'o' or at the 'm'. In cases of ties like that, let's choose the earlier option. So we will end our subsequence at index 4: 'o'. Using the prev array, we see that the character that should be previous to the character at index 4 is the character at index prev[4] = 2. So the 'l' should precede the 'o'. The character that should precede the 'l' is the character at index prev[2] = 1. So the 'e' should precede the l. The character that should preced the 'e' is the character at index prev[1] = 0. So the 'W' should precede the 'e' prev[0] is -1, so nothing should precede the 'W'. One approach for printing out the sequence is to put the values 'o', '1', 'e', and 'W' in an ArrayList, in that order, and then print out the array list elements in reverse order. Then our highly detailed) pseudocode becomes: for i = 0,1,...,n-1 init score[i] to 1 init prev[e] to - 1 for j = 0,...,.-1 if the character at index j is less than the character at index i and the score at j is at least the score at i update score[i] update prev[i] Determine which score is the maximum, and the index (index) at which is occurs (requires a loop). Allocate an array list to hold the maximum increasing subsequence. while index is >= 0 add the corresponding character to the array list update index to prev[index] Print out the array list in reverse order (requires a loop)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started