Longest Common Subsequence using bottom up recursion(JAVA)
static List<Character> bulcs (List<Character> cs1, List<Character>
cs2) {
//TODO
}The above question can be coded as follows.
import java.util.*;
import java.lang.*;
import java.io.*;
public class BULCS {
public static void main(String[] args) {
List <Character> s1 = new
ArrayList<Character>();
List <Character> s2 = new
ArrayList<Character>();
//s1 = "aba"
s1.add('a');
s1.add('b');
s1.add('a');
//s2 = "cab"
s2.add('c');
s2.add('a');
s2.add('b');
List <Character> ans = bulcs(s1,s2);
for(int
i=0;i<ans.size();i++)
System.out.print(ans.get(i));
System.out.println("");
}
static List<Character> bulcs
(List<Character> cs1, List<Character> cs2) {
int n = cs1.size();
int m = cs2.size();
int[][] dp = new int[n][m];
//dp[i][j] will store answer for
string (0,i) and (0,j)
int i,j;
//from relation of lcs we know
to calc dp[i][j]
//we need dp[i-1][j] , dp[i][j-1]
and dp[i-1][j-1]
//so we fille matrix from top left
to bottom right one row at a time
//also we fill 0th row and 0th
column as base cases
//filling 0th row
for(i=0;i<m;i++)
if(cs1.get(0) ==
cs2.get(i))
dp[0][i] = 1;
else
dp[0][i] = 0;
//filling 0th column
for(i=0;i<n;i++)
if(cs1.get(i) ==
cs2.get(0))
dp[i][0] = 1;
else
dp[i][0] = 0;
//dp loop
for(i=1;i<n;i++)
{
for(j=1;j<m;j++)
{
if(cs1.get(i) == cs2.get(j))
dp[i][j] = 1 +
dp[i-1][j-1];
else
dp[i][j] =
Math.max(dp[i-1][j] , dp[i][j-1]);
}
}
List <Character> ret = new
ArrayList <Character> ();
int ans = dp[n-1][m-1];
int copy = ans;
i = n-1;
j = m-1;
//regenerate the answer. the
LCS
while(copy > 0)
{
if(cs1.get(i) ==
cs2.get(j))
{
ret.add(cs1.get(i));
copy--;
i--;
j--;
}
else
{
if(dp[i-1][j] > dp[i][j-1])
i--;
else
j--;
}
}
Collections.reverse(ret);
return ret;
}
}



In case of any doubt please comment. Happy coding :)
Longest Common Subsequence using bottom up recursion(JAVA) static List<Character> bulcs (List<Character> cs1, List<Character> cs2) { //TODO...
Problem 1. Write a program in Java to find the Longest Common Subsequence (LCS) using Dynamic Programming. Your program will read two strings from keyboard and display the LCS on the screen. Assume upper and lower case letters as same. Sample Input (taken from keyboard): saginaw gain Sample output (display on the screen): ain
Need help with all 3 parts.
Thanks
Question 1 (Longest Common Subsequence) In the longest common sub- sequence algorithm we discussed in class, we formulated the recursive formula based on prefixes of the two inputs, i.e., X[1...) and Y [1..,]. 1. Rewrite the recursive formula using suffixes instead of prefixes, i.e., X[...m] and Y[j..n]. 2. Develop a bottom-up dynamic programming algorithm based on the recur- sive formula in (a). Describe the algorithm and write a pseudo code. 3. Use the...
1. Write the algorithm pseudocode for the longest common subsequence problem using dynamic programming. What is its running time?
Question 2 (2.5 points) Longest common subsequence Consider the procedure to determine the length of the longest common subsequence, LCS- LENGTH(X, Y). It solves the LCS problem in a bottom-up manner, by filling out a 2-D tabular LCS-LENGTH(X, Y) m = X.length 2. n-Y.length 3. let cO.m, 0n] be a new array 4, for i = 0 to m for/ = 0 to n else if ATY ci,ci ,j-1 +1 10 else 12. return clm, n] For example, X (B,...
// Java Queue LinkedList Assignment // A queue is implemented using a singly linked list. // the variable: back "points" at the first node in the linked list // new elements ( enqueued) are added at the back // the variable: front "points" at the last node in the linked list. // elements are removed (dequeued) from the front // // Several queue instance methods are provided for you; do not change these // Other instance methods are left for...
Using the programming language Java: Write a function to find the longest common prefix string amongst an array of strings.
4. [Tests basic knowledge of recursion] Write a recursive static Java method that accepts an array arr of integers argument returns a list of all permutations of these integers. (A permutation of a sequence of integers is a re-arrangement of the integers. For example, one permutation of 1, 3, 4, 8, 2 is 3, 1, 2, 8, 4.) For this problem, you may assume that the input array contains no duplicate entries. Your method should return an ArrayList of int...
Java/LinkedList Need help with a few of the TODO parts, more info below in comments in bold. Thanks, package lab4; import java.util.IdentityHashMap; public class IntNode implements Cloneable { private int data; private IntNode next; public IntNode(int d, IntNode n) { data = d; next = n; } public IntNode getNext() { return next; } /// Override methods from Object @Override ...
C++ (using recursion or stacks)
• List stretch (string input, int k). A stretch of the input string is generated by repeating each character in order up to k times (and at least once). For input string "abc" and k = 2, the output list should have: abc, aabc, abbc, abcc, aabbc, aabcc, abbcc, aabbcc. (Again, the order does not matter.)
Using any variable and any name for the methods (data structure java eclipse) 1) Find odd average and even number average in a linked list using recursion 2) Remove duplicate from a stack.