Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
930 B
4
Indexable
Never
#include<bits/stdc++.h>
using namespace std;

int main()
{
    string seq1, seq2;
    cout << "Enter the sequences: " << endl;
    cin >> seq1 >> seq2;

    int n = seq1.size(), m = seq2.size();

    int ar[n+1][m+1];

    for(int i=0; i<=n; i++)
        ar[i][0] = 0;
    for(int j=0; j<=m; j++)
        ar[0][j] = 0;

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            if(seq1[i-1] == seq2[j-1])
            {
               ar[i][j] = ar[i-1][j-1] + 1;
               
            }
            else
            {
                if(ar[i-1][j] >= ar[i][j-1])
                {
                    ar[i][j] = ar[i-1][j];
                }
                else
                {
                    ar[i][j] = ar[i][j-1];
                }
            }
        }

    }

    cout << "Length of the LCS: " << ar[n][m] << endl;


    return 0;
}