Question
I need this using C++.
In this project, you will implement the dynamic programming-based solution to find the longest common subsequence (LCS) of tw
0 0
Add a comment Improve this question Transcribed image text
Answer #1

A example given in sample output with answer ATGGC is wrong

There can be many lcs for a pair of strings i have only printed one

#include <bits/stdc++.h>
using namespace std;
void LongestCommonSubsequence(string X,string Y)
{
   int m=X.length(),n=Y.length();
   int dp[m+1][n+1];
   //initialise dp with 0
   memset(dp,0,sizeof(dp));
   for(int i=0;i<=m;i++)
   {
       for(int j=0;j<=n;j++)
       {
           //if length of any of the string is 0 its lcs is 0
           if(i==0||j==0)
           dp[i][j]=0;
           //if two characters are equal
           else if(X[i-1]==Y[j-1])
           dp[i][j]=1+dp[i-1][j-1];
           else if(X[i-1]!=Y[j-1])
           dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
       }
   }
   //lcs stores answer
   string lcs="";
   // Start from the right-most-bottom-most corner and
// one by one store characters in lc
   int i=m,j=n;
   while(i>0 && j>0)
   {
       if(X[i-1]==Y[j-1])
       {
           lcs=lcs+X[i-1];
           i--;
           j--;
       }
       else if(dp[i-1][j]>=dp[i][j-1])
       i--;
       else if(dp[i-1][j]<dp[i][j-1])
       j--;
   }
   //print matrix
   for(int i=0;i<=m;i++)
   {
       for(int j=0;j<=n;j++)
       cout<<dp[i][j]<<" ";
       cout<<endl;
   }
   //lcs will be reverse of actual answer so reverse it and print it
   reverse(lcs.begin(),lcs.end());
   cout<<lcs<<endl;
}
int main() {
   string X="TCGCCTT";
   string Y="GGGGTAACT";
   LongestCommonSubsequence(X,Y);
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
I need this using C++. In this project, you will implement the dynamic programming-based solution to...
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
  • Problem 1. Write a program in Java to find the Longest Common Subsequence (LCS) using Dynamic...

    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 the answer to b not a. thanks! 2. (40 pts) Let A, B, and C be three strings each n characters long. We want t...

    need the answer to b not a. thanks! 2. (40 pts) Let A, B, and C be three strings each n characters long. We want to compute the longest subsequence that is common to all three strings. (a) Let us first consider the following greedy algorithm for this problem. Find the longest common subsequence between any pair of strings, namely, LCS(A, B). LCS(B,C), LCS(A, C). Then, find the longest common subsequence between this LCS and the 3rd string. That is,...

  • 1 (15 pts) Implement recursive, memoized, and dynamic programming Fibonacci and study their performances using different...

    1 (15 pts) Implement recursive, memoized, and dynamic programming Fibonacci and study their performances using different problem instans You can choose to look at the perfor- mance by either timing the functions or counting the basic operations (in code) Provide your results below, and submit your code. Also, describe the pros and cons of your choice of performance metric Note: If you decide to use timing, the standard way to time an algorithm is to run the same problem 100...

  • In this assignment, you will implement a deterministic finite automata (DFA) using C++ programming language to...

    In this assignment, you will implement a deterministic finite automata (DFA) using C++ programming language to extract all matching patterns (substrings) from a given input DNA sequence string. The alphabet for generating DNA sequences is {A, T, G, C}. Write a regular expression that represents all DNA strings that contains at least two ‘A’s. Note: assume empty string is not a valid string. Design a deterministic finite automaton to recognize the regular expression. Write a program which asks the user...

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

    I really would appreciate some help with this assignment and include comments please as I am in this for learning. Part I: Maximum increasing subsequence (adapted from Programming Exercise 22.2) Write a program MaxlncreasingSubseq.java that prompts the user to enter a string and displays a maximum length increasing subsequence of characters. Here are four sample runs Enter a string: zebras ers Enter a string: Welcome Welo Enter a string: mmmoooooovwwvee mov Enter a string: abqrstcxyzdefghij abcdefghij You must use dynamic...

  • Questions 33 to 35 refer to the following Longest Common Subsequence problem. Given two sequences X-XI,...

    Questions 33 to 35 refer to the following Longest Common Subsequence problem. Given two sequences X-XI, X2,..., ...., X and Y y, y......... ya. Let C[ij]be the length of Longest Common Subsequence of x1, x2,..., Xi and y, y,..... Then Cij] can be recursively defined as following: CO if i=0 or j = 0 Cli,j][i-1.j-1]+1 ifi.j> 0 and x = y, max{C[i-1.7].[1.j-1); if i j>0 and x*y 0 The following is an incomplete table for sequence of AATGTT and AGCT....

  • 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...

  • Problem 1. (Calculating Edit Distance Using Dynamic Programming) A direct implementation of the above recursive scheme...

    Problem 1. (Calculating Edit Distance Using Dynamic Programming) A direct implementation of the above recursive scheme will work, but it is spectacularly inefficient. If both input strings have N characters, then the number of recursive calls will exceed 2N. To overcome this performance bug, we use dynamic programming. Dynamic programming is a powerful algorithmic paradigm, first introduced by Bellman in the context of operations research, and then applied to the alignment of biological sequences by Needleman and Wunsch. Dynamic programming...

  • READ CAREFULLY AND CODE IN C++ Dynamic Programming: Matrix Chain Multiplication Description In this assignment you...

    READ CAREFULLY AND CODE IN C++ Dynamic Programming: Matrix Chain Multiplication Description In this assignment you are asked to implement a dynamic programming algorithm: matrix chain multiplication (chapter 15.2), where the goal is to find the most computationally efficient matrix order when multiplying an arbitrary number of matrices in a row. You can assume that the entire input will be given as integers that can be stored using the standard C++ int type and that matrix sizes will be at...

  • The following table is partially filled. 0 1 4 0 Xi 4 D a) Explain why c[1,1] to c[1,5] and c[2,1...

    The following table is partially filled. 0 1 4 0 Xi 4 D a) Explain why c[1,1] to c[1,5] and c[2,1] to c[5,1] are all 1s? b) Compute c[2,2], and which cell do you refer to when computing it? c) Compute c 2,31 and c[3,2], which cell do you refer to directly this time? d) Fill up the rest of the cells. Assume that you take c[i,j - 1] when there is a draw in line 11. (i.e., take the...

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