Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This is the output: Example 1: X={2, 4, 6, 8, 10, 12, 14, 16, 18, 20} Y={13, 15, 18, 1, 3, 5, 8, 10, 12,

This is the output:

Example 1:

X={2, 4, 6, 8, 10, 12, 14, 16, 18, 20}

Y={13, 15, 18, 1, 3, 5, 8, 10, 12, 20}

Longest common subsequence is S={8, 10, 12}

with length 3.

n = 3 i = 4 j = 7

What I need help with is the code, I know the algorithm but I can't see to get the code right. I don't know what I'm missing or If I got any errors..

====================================================================

import java.util.*; import java.io.*; import java.util.Scanner; class LCSExample1 { public static int findLengthOfLCS(String str1, String str2, int[] X, int[] Y) { int m = X.length; int n = Y.length; int[][] tableForLCS = new int[m + 1][n + 1]; for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { if (X[i - 1] == Y[j - 1]) { tableForLCS[i][j] = tableForLCS[i - 1][j - 1] + 1; } else { tableForLCS[i][j] = Math.max(tableForLCS[i - 1][j], tableForLCS[i][j - 1]); } } } return tableForLCS[m][n]; } public static void main(String[] args) { String str1, str2, LCS; Scanner sc= new Scanner(System.in); //System.in is a standard input stream. System.out.print("Enter first sequence: "); str1 = sc.nextLine(); //reads string. System.out.print("Enter second sequence: "); str2 = sc.nextLine(); //reads string. int m = str1.length(); int n = str2.length(); LCS = findLengthOfLCS(str1, str2, X, Y); System.out.print("Sequence1: " + str1 + " Sequence2: " + str2); System.out.println(" LCS: "+LCS); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Autonomous Database In Enterprise Architecture

Authors: Bal Mukund Sharma, Krishnakumar KM, Rashmi Panda

1st Edition

1801072248, 978-1801072243

Students also viewed these Databases questions