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
In Java, implement a dynamic programming solution to the Set Partition problem. Recall that the Set...
In Java, implement a dynamic programming solution to the Set Partition problem. Recall that the Set Partition problem, given a set S = {a1, a2, …, an} of positive integers (representing assets) requires partitioning into two subsets S1 and S2 that minimizes the difference in the total values of S1 and S2. For testing your code, a) populate an initial set of 100 assets, with random values between 1 and 1000, identify the (minimum) difference between the two partitions, and...
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
Given a directed acyclic graph G= (V,E), vertex s∈V, design a dynamic programming algorithm to compute...
Given a directed acyclic graph G= (V,E), vertex s∈V, design a dynamic programming algorithm to compute the number of distinct paths from s to v for any v∈V. 1. Define subproblems 2. Write recursion 3. Give the pseudo-code 4. Analyze the running time.
C Programming: Write a function that takes in an array of integers and an integer containing...
C Programming: Write a function that takes in an array of integers and an integer containing the count of elements, then have it returns the sum of all the even values inside the array.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT