Question

(C++)Dynamic Programming of LCS: Write codes for the longest common subsequence.

(C++)Dynamic Programming of LCS: Write codes for the longest common subsequence.

Homework Answers

Answer #1

Dynamic Programming of Longest Common Subsequence:

#include<bits/stdc++.h>
using namespace std;
int max(int a, int b);
int lcs( char *X, char *Y, int m, int n )
{
    int L[m + 1][n + 1];
    int i, j;
    for (i = 0; i <= m; i++)
    {
        for (j = 0; j <= n; j++)
        {
        if (i == 0 || j == 0)
            L[i][j] = 0;

        else if (X[i - 1] == Y[j - 1])
            L[i][j] = L[i - 1][j - 1] + 1;

        else
            L[i][j] = max(L[i - 1][j], L[i][j - 1]);
        }
    }
    return L[m][n];
}
int max(int a, int b)
{
    return (a > b)? a : b;
}
int main()
{
    char str1[100];
    char str2[100];
      cin>>str1>>str2;

    int m = strlen(str1);
    int n = strlen(str2);

    cout << "Length of LCS is "<< lcs( str1, str2, m, n );

    return 0;
}
Know the answer?
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
Similar Questions
a) Find the longest common subsequent of these two strings using dynamic programming. Draw the table...
a) Find the longest common subsequent of these two strings using dynamic programming. Draw the table and the formula. dbdcba dbbca b) Explain master's theorem and all its cases. Then, find the order of these recurrence relationships. 1. T(n) = 2nT(n/2) + nn 2. T(n) = 7T(n/3) + n2
LCS (Longest Common Subsequence) problem consists in finding the longest subsequence of characters that is present...
LCS (Longest Common Subsequence) problem consists in finding the longest subsequence of characters that is present in two given sequences in the same order. A subsequence is a sequence of characters that don't have to be consecutive. For example, for the following inputs, the length of the LCS is 4 X: ABCBDAB Y: BDCABA And one of the LCS is BDAB The following code is a solution for such problem, which values must be stored in the variable key for...
A subsequence is anything obtained from a sequence by extracting a subset of elements, but keeping...
A subsequence is anything obtained from a sequence by extracting a subset of elements, but keeping them in the same order; the elements of the subsequence need not be contiguous in the original sequence. Let A[1 .. m] and B[1 .. n] be two arbitrary arrays. A common subsequence of A and B is another sequence that is a subsequence of both A and B. Describe an efficient algorithm to compute the length of the longest common subsequence of A...
Scheme Programming Write a polynomial time scheme program to find the longest non-decreasing subsequent of a...
Scheme Programming Write a polynomial time scheme program to find the longest non-decreasing subsequent of a list of numbers. For example: (longest ''(2 4 3 1 2 1 3 6 1 3 2 1 2 33 4 2 4 10 11 7)) --> (1 1 1 1 2 4 4 10 11). Do not use side-effecting functions (i.e., functions with exclamation point in their names such as set!), sequencing, iteration (e.g., do, for-each) or vectors. Do not use input/output mechanisms...
write the C program  codes Temperature controlled fan using PIC 16F877A
write the C program  codes Temperature controlled fan using PIC 16F877A
Dynamic programming Make the table for making $12 by using the denominations $1, $4, and $10....
Dynamic programming Make the table for making $12 by using the denominations $1, $4, and $10. Show the traceback for the optimal set of the money used.
In dynamic programming, problems must comply with the property of calculating only once a solution to...
In dynamic programming, problems must comply with the property of calculating only once a solution to a subproblem and then to optimize it by reusing the result obtained. What is the name of such property a) Overlapping subproblems b) Globally optimal c) None of the above d) Feasible e) Optimal substructure f) Locally optimal g) Irrevocable
FOR C PROGRAMMING LANGUAGE Write a recursive C PROGRAMMING LANGUAGE function int sumStrlens(const char* str1, const...
FOR C PROGRAMMING LANGUAGE Write a recursive C PROGRAMMING LANGUAGE function int sumStrlens(const char* str1, const char* str2) which returns the sum of strlen(str1) and strlen(str2). You can assume that strlen(str2) is always strictly greater than strlen(str1) (strlen(str2) > strlen(str1)). You MAY NOT use any function from string.h. You MAY NOT use any square brackets([]) or any type of loops in function.
Write a program that checks the age of a user (C++ Programming)    If the user...
Write a program that checks the age of a user (C++ Programming)    If the user is 18 to 20 print “Sing a song” If the user is younger than 18 then print “Please show a dance step” Else Welcome the user and print “You are audience”
Programming in C language (not C++) Write a main function with a function call to a...
Programming in C language (not C++) Write a main function with a function call to a function called Calculation which has two integer arguments/ parameters. The function returns a character. Declare and initialize the necessary data variables and assign balues needed to make it executable and to prevent loss of information. //input 2 integer values //returns the characterequivalent of the higher of the two integer values char Calculation(int, int);