Question

15.4-1 -- Determine an LCS of (1, 0, 0, 1, 0, 1, 0, 1) and (0,...

15.4-1 -- Determine an LCS of (1, 0, 0, 1, 0, 1, 0, 1) and (0, 1, 0, 1, 1,0, 1, 1, 0)

4. -- Implement algorithm for finding LCS of any two binary strings x and y. Use it to find the LCS of two strings in 15.4-1

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

Code:

#include<iostream>
#include<algorithm>
using namespace std;
int main(){
string s1,s2;
cout<<"Enter first binary string\n";
cin>>s1;
cout<<"Enter second binary string\n";
cin>>s2;
//creating lcs array for dp
int lcs_array[s1.length()+1][s2.length()+1];
for(int i=0;i<=s1.length();i++){
for(int j=0;j<=s2.length();j++){
if(i==0 || j==0){
lcs_array[i][j]=0;
}
else if(s1[i-1]==s2[j-1]){
lcs_array[i][j]=lcs_array[i-1][j-1]+1;
}
else{
lcs_array[i][j]=max(lcs_array[i-1][j],lcs_array[i][j-1]);
}
}
}

string result="";
int i=s1.length(),j=s2.length();
while(i>0 && j>0){
if(s1[i-1]==s2[j-1]){
result+=s1[i-1];
i-=1;
j-=1;
}
else if(lcs_array[i-1][j]>lcs_array[i][j-1]){
i--;
}
else{
j--;
}

}
// as the answer we get from bottom up dp would be in reverse order
//thus by reversing we get the correct answer
reverse(result.begin(), result.end());
cout<<"The longest common subsequence is : "<<result<<endl;
}

Output:

Feel free to reach out to me in the comment in case of any doubt or clarification. I will be more than happy to reply.

Java Code:

import java.util.*;
public class lcs{
public static int max(int a,int b)
{
if(a>b)
return a;
else
return b;
}

public static void main(String []args){
String s1,s2;
Scanner sc=new Scanner(System.in);
System.out.println("Enter first binary string");
s1=sc.nextLine();
System.out.println("Enter second binary string");
s2=sc.nextLine();
//creating lcs array for dp
int lcs_array [][] = new int[s1.length()+1][s2.length()+1];
for(int i=0;i<=s1.length();i++){
for(int j=0;j<=s2.length();j++){
if(i==0 || j==0){
lcs_array[i][j]=0;
}
else if(s1.charAt(i-1)==s2.charAt(j-1)){
lcs_array[i][j]=lcs_array[i-1][j-1]+1;
}
else{
lcs_array[i][j]=max(lcs_array[i-1][j],lcs_array[i][j-1]);
}
}
}

String result="";
int i=s1.length(),j=s2.length();
while(i>0 && j>0){
if(s1.charAt(i-1)==s2.charAt(j-1)){
result+=s1.charAt(i-1);
i-=1;
j-=1;
}
else if(lcs_array[i-1][j]>lcs_array[i][j-1]){
i--;
}
else{
j--;
}
}
// as the answer we get from bottom up dp would be in reverse order
//thus by reversing we get the correct answer
StringBuilder b=new StringBuilder(result);
System.out.println("The longest common subsequence is : "+b.reverse().toString());
}
}

Output:

Select C:\Users\virus\Desktop\LCS.exe Enter first binary string 10010101 Enter second binary string 010110110 The longest common subsequence is : 010101 Process returned @ (exe) execution time : 14.099 s Press any key to continue.

Select C:\Users\virus\Desktop\LCS.exe Enter first binary string 10010101 Enter second binary string 010110110 The longest common subsequence is : 010101 Process returned @ (exe) execution time : 14.099 s Press any key to continue.

1. Result $javac lcs.java Sjava -Xmx128M -Xms 16M lcs Enter first binary string 10010101 Enter second binary string 010110110 The longest common subsequence is : 010101

Add a comment
Know the answer?
Add Answer to:
15.4-1 -- Determine an LCS of (1, 0, 0, 1, 0, 1, 0, 1) and (0,...
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
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