Answered step by step
Verified Expert Solution
Question
1 Approved Answer
How do I write a pseudo code for the following code and the time complexity (line by line) and the total. the code: import java.util.Scanner;
How do I write a pseudo code for the following code and the time complexity (line by line) and the total. the code: import java.util.Scanner; public class LCS { public static void main(String[] args) { System.out.println(); Scanner input = new Scanner(System.in); // Taking the elemest and length of X and Y from user System.out.print("Enter the length of the sequences X and Y: "); int n = input.nextInt(); int[] X = new int[n]; int[] Y = new int[n]; System.out.print(" Enter the elements of sequence X: "); for (int i = 0; i < n; i++) { X[i] = input.nextInt(); } System.out.print(" Enter the elements of sequence Y: "); for (int i = 0; i < n; i++) { Y[i] = input.nextInt(); } input.close(); // Printing X and Y System.out.println(); System.out.print("X: {"); for (int i = 0; i < n; i++) { System.out.print(X[i]); if (i != n - 1) { System.out.print(", "); } } System.out.print("}"); System.out.println(); System.out.print("Y: {"); for (int j = 0; j < n; j++) { System.out.print(Y[j]); if (j != n - 1) { System.out.print(", "); } } System.out.print("}"); // Finding the LCS int[][] lcs = new int[n + 1][n + 1]; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (X[i - 1] == Y[j - 1]) { lcs[i][j] = lcs[i - 1][j - 1] + 1; } else { lcs[i][j] = Math.max(lcs[i - 1][j], lcs[i][j - 1]); } } } // Getting the length of LCS int length = lcs[n][n]; // Getting the LCS elements int[] S = new int[length]; int i = n; int j = n; int k = length - 1; while (i > 0 && j > 0) { if (X[i - 1] == Y[j - 1]) { S[k--] = X[i - 1]; i--; j--; } else if (lcs[i - 1][j] > lcs[i][j - 1]) { i--; } else { j--; } } // Printing the LCS elements System.out.println(" "); System.out.print("S: {"); for (int m = 0; m < length; m++) { System.out.print(S[m]); if (m != length - 1) { System.out.print(", "); } } System.out.println("}"); // Printing the length of LCS System.out.println("Length of LCS: " + length); // Finding the starting index in X and Y int startIndexX = -1; int startIndexY = -1; for (i = 0; i < n; i++) { if (X[i] == S[0]) { startIndexX = i; break; } } for (j = 0; j < n; j++) { if (Y[j] == S[0]) { startIndexY = j; break; } } // Printing the start index of X and Y System.out.println("Start index in X: " + startIndexX); System.out.println("Start index in Y: " + startIndexY); System.out.println(); } }
the code:
import java.util.Scanner;
public class LCS {
public static void main(String[] args) {
System.out.println();
Scanner input = new Scanner(System.in);
// Taking the elemest and length of X and Y from user
System.out.print("Enter the length of the sequences X and Y: ");
int n = input.nextInt();
int[] X = new int[n];
int[] Y = new int[n];
System.out.print(" Enter the elements of sequence X: ");
for (int i = 0; i < n; i++) {
X[i] = input.nextInt();
}
System.out.print(" Enter the elements of sequence Y: ");
for (int i = 0; i < n; i++) {
Y[i] = input.nextInt();
}
input.close();
// Printing X and Y
System.out.println();
System.out.print("X: {");
for (int i = 0; i < n; i++) {
System.out.print(X[i]);
if (i != n - 1) {
System.out.print(", ");
}
}
System.out.print("}");
System.out.println();
System.out.print("Y: {");
for (int j = 0; j < n; j++) {
System.out.print(Y[j]);
if (j != n - 1) {
System.out.print(", ");
}
}
System.out.print("}");
// Finding the LCS
int[][] lcs = new int[n + 1][n + 1];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (X[i - 1] == Y[j - 1]) {
lcs[i][j] = lcs[i - 1][j - 1] + 1;
} else {
lcs[i][j] = Math.max(lcs[i - 1][j], lcs[i][j - 1]);
}
}
}
// Getting the length of LCS
int length = lcs[n][n];
// Getting the LCS elements
int[] S = new int[length];
int i = n;
int j = n;
int k = length - 1;
while (i > 0 && j > 0) {
if (X[i - 1] == Y[j - 1]) {
S[k--] = X[i - 1];
i--;
j--;
} else if (lcs[i - 1][j] > lcs[i][j - 1]) {
i--;
} else {
j--;
}
}
// Printing the LCS elements
System.out.println(" ");
System.out.print("S: {");
for (int m = 0; m < length; m++) {
System.out.print(S[m]);
if (m != length - 1) {
System.out.print(", ");
}
}
System.out.println("}");
// Printing the length of LCS
System.out.println("Length of LCS: " + length);
// Finding the starting index in X and Y
int startIndexX = -1;
int startIndexY = -1;
for (i = 0; i < n; i++) {
if (X[i] == S[0]) {
startIndexX = i;
break;
}
}
for (j = 0; j < n; j++) {
if (Y[j] == S[0]) {
startIndexY = j;
break;
}
}
// Printing the start index of X and Y
System.out.println("Start index in X: " + startIndexX);
System.out.println("Start index in Y: " + startIndexY);
System.out.println();
}
}
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