Question

Part I: Maximum increasing subsequence (adapted from Programming Exercise 22.2) Write a program MaxlncreasingSubseq.java thatBesides computing the scores for each of the characters in the string, you will need to print out the maximum increasing subsLongest increasing subsequences The algorithm runs as follows: for i=0,1, n-1 score[i] = 1 + max{scorelil: j 〈 i && a[j] 〈 alLongest increasing subsequences Augment this so that it is easy to recover the sequence elements: for i - 0,1,... ,n-1 score[

I really would appreciate some help with this assignment and include comments please as I am in this for learning.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//java program

import java.util.Scanner;
public class MaxIncreasingSubseq {
   public static void main(String args[]) {
       Scanner in = new Scanner (System.in);
       String string;
       System.out.print("Enter string : ");
       string = in.nextLine();
      
       printLIS(string);
       in.close();
      
   }
   public static void printLIS(String s) {
       int length = s.length();
       int DP[] = new int [length];
       int prev[] = new int [length];
       for(int i=0;i<length;i++)DP[i]=1;
       prev[0] = -1;
       int max,maxvalue;
       for(int i=1;i<length;i++) {
           max=0;maxvalue=0;
           for(int j=i-1;j>=0;j--) {
               if(s.charAt(i)>s.charAt(j)){
                   if(DP[j]>maxvalue) {
                       maxvalue=DP[j];
                   max=j;
                   }
               }
           }
           prev[i]=max;
           DP[i]=maxvalue+1;
       }
       max=0;
       for(int i=1;i<length;i++) {
           if(DP[i]>DP[max])max=i;
       }
      
       String str="";
       while(max!=-1) {
           str+=(s.charAt(max));
           max=prev[max];
       }
       for(int i=str.length()-1;i>=0;i--) {
           System.out.print(str.charAt(i));
       }
   }
}
//sample output

<terminated> MaxlncreasingSubseq Java Application] CProgram Files Javaljdk1.8.0 151\binljavaw.exe Enter string welcome belo

Add a comment
Know the answer?
Add Answer to:
I really would appreciate some help with this assignment and include comments please as I am...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need this using C++. In this project, you will implement the dynamic programming-based solution to...

    I need this using C++. In this project, you will implement the dynamic programming-based solution to find the longest common subsequence (LCS) of two sequences. Your inputs will be the two sequences (as Strings) and the outputs are the longest common subsequence (printed as a String) and the final matrix (printed as a two-dimensional array) depicting the length of the longest common subsequences (as shown in the slides) for all possible subsequences of the two input sequences. The two input...

  • C++ Help. I am new to Visual Studio and C++ so please include comments! Please follow...

    C++ Help. I am new to Visual Studio and C++ so please include comments! Please follow the directions and make as simple as possible. Write a C++ program that reads the following list of input data (book.dat) and then allows users to search and view the books from a Menu List all available books Search for book using A. Title or B. ISBN? Exit Program The format of the file is as follows: //    The title of the book //   ...

  • 2. (25) [Rising trend] Textbook Exercise 17 in Chapter 6. The problem to solve is, in other words...

    2. (25) [Rising trend] Textbook Exercise 17 in Chapter 6. The problem to solve is, in other words, to select from a sequence of n numbers a subset, including the first number, such that the selected numbers make a longest monotonously increasing sequence. In the exercise b, (i) write an optimal substructure (with an explanation),ii) write the iterative dynamic programming algorithm (pseudocode), and (iii) show the running-time analysis. Hints: the algorithm in the exercise a is a greedy algorithm; the...

  • Please I need help in C language, I am trying to modify the code per the...

    Please I need help in C language, I am trying to modify the code per the below instructions, but I am getting errors. Can't fgure it out. The attempted code modification and data file is privided below, after the instructions. Thank you. Instructions:                  1.      First, add a function named printMsg that displays a message, a greeting, or an introduction to the program for the user. Add a statement that calls that function from the main function when your program starts....

  • Can I please get help with this? will upvote upon completion. Problem 2. The longest common...

    Can I please get help with this? will upvote upon completion. Problem 2. The longest common substring problem is to find the longest string that is a substring of two strings. The longest common substring of the strings "ABABC", and "ABCBA" is string "ABC" of length 3. A substrings of strings is a series of consecutive letters of s. For example, "ABA” is a substring of “ABABC", but "ABAC" is not a substring of "ABABC". Design an algorithm such that...

  • I would really appreciate your help with solving this whole question of my assignment due in...

    I would really appreciate your help with solving this whole question of my assignment due in a few hours. Willing to rate and like!! Thank you so much Q1/ Solve the following series question a) is the following sequence increasing, decreasing, or not monotonic? Is the sequence bounded? a. an = 2n13 b) Use power series to approximate the definite integral to six decimal places. a. Jo 773 dx b. So 2 x ln(1 + x²) dx 0.3 x c)...

  • I'm stuck on an assignment and would really appreciate some help with the parts that are...

    I'm stuck on an assignment and would really appreciate some help with the parts that are in bold below: Write a program that takes a value from a user and stores it in the registry. You can use any key name that you like but also store the current time as another value inside of your new key. Finally, get a directory listing of your current working directory and store that value. You may need to use REG_MULTI_SZ for that...

  • this is for java programming Please Use Comments I am not 100% sure if my code...

    this is for java programming Please Use Comments I am not 100% sure if my code needs work, this code is for the geometric if you feel like you need to edit it please do :) Problem Description: Modify the GeometricObject class to implement the Comparable interface and define a static max method in the GeometricObject class for finding the larger (in area) of two GeometricObject objects. Draw the UML and implement the new GeometricObject class and its two subclasses...

  • I have already finished most of this assignment. I just need some help with the canMove...

    I have already finished most of this assignment. I just need some help with the canMove and main methods; pseudocode is also acceptable. Also, the programming language is java. Crickets and Grasshoppers is a simple two player game played on a strip of n spaces. Each space can be empty or have a piece. The first player plays as the cricket pieces and the other plays as grasshoppers and the turns alternate. We’ll represent crickets as C and grasshoppers as...

  • I really need some help with this please I would greatly appreciate it! ACLIVILIS anu Due...

    I really need some help with this please I would greatly appreciate it! ACLIVILIS anu Due Dates -- General Chemistry Lab - Falliy ty and Hess' Law Pre-Lab Assignment Score: 95/500 Resources Give Up? Hint Check Answer Question 5 of 5 > In a constant-pressure calorimeter, 60.0 mL of 0.320 M Ba(OH), was added to 60.0 mL of 0.640 M HCI. The reaction caused the temperature of the solution to rise from 21.23 °C to 25.59 °C. If the solution...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT