//
// main.cpp
// Longest Common Subsequence
//
// Created by Himanshu on 17/09/21.
//
#include <iostream>
#include <iostream>
using namespace std;
void LCS (string s1, string s2) {
int N = (int)s1.size(), M = (int)s2.size();
int L[N+1][M+1];
for (int i=0; i<=N; i++) {
for (int j=0; j<=M; j++) {
L[i][j] = 0;
}
}
for (int i=0; i<=N; i++) {
for (int j=0; j<=M; j++) {
if (i == 0 || j == 0) {
L[i][j] = 0;
} else if (s1[i-1] == s2[j-1]) {
L[i][j] = L[i-1][j-1] + 1;
} else {
L[i][j] = max(L[i][j-1], L[i-1][j]);
}
}
}
cout<<"Length of Longest Common Subsequence (LCS) is: "<<L[N][M]<<endl;
}
int main() {
string s1 = "abcde", s2 = "ace";
LCS(s1, s2);
}
Ly8KLy8gIG1haW4uY3BwCi8vICBMb25nZXN0IENvbW1vbiBTdWJzZXF1ZW5jZQovLwovLyAgQ3JlYXRlZCBieSBIaW1hbnNodSBvbiAxNy8wOS8yMS4KLy8KCiNpbmNsdWRlIDxpb3N0cmVhbT4KCiNpbmNsdWRlIDxpb3N0cmVhbT4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCnZvaWQgTENTIChzdHJpbmcgczEsIHN0cmluZyBzMikgewogICAgaW50IE4gPSAoaW50KXMxLnNpemUoKSwgTSA9IChpbnQpczIuc2l6ZSgpOwogICAgaW50IExbTisxXVtNKzFdOwoKICAgIGZvciAoaW50IGk9MDsgaTw9TjsgaSsrKSB7CiAgICAgICAgZm9yIChpbnQgaj0wOyBqPD1NOyBqKyspIHsKICAgICAgICAgICAgTFtpXVtqXSA9IDA7CiAgICAgICAgfQogICAgfQoKICAgIGZvciAoaW50IGk9MDsgaTw9TjsgaSsrKSB7CiAgICAgICAgZm9yIChpbnQgaj0wOyBqPD1NOyBqKyspIHsKICAgICAgICAgICAgaWYgKGkgPT0gMCB8fCBqID09IDApIHsKICAgICAgICAgICAgICAgIExbaV1bal0gPSAwOwogICAgICAgICAgICB9IGVsc2UgaWYgKHMxW2ktMV0gPT0gczJbai0xXSkgewogICAgICAgICAgICAgICAgTFtpXVtqXSA9IExbaS0xXVtqLTFdICsgMTsKICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgIExbaV1bal0gPSBtYXgoTFtpXVtqLTFdLCBMW2ktMV1bal0pOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQogICAgCiAgICBjb3V0PDwiTGVuZ3RoIG9mIExvbmdlc3QgQ29tbW9uIFN1YnNlcXVlbmNlIChMQ1MpIGlzOiAiPDxMW05dW01dPDxlbmRsOwogICAgCn0KIAppbnQgbWFpbigpIHsKICAgIHN0cmluZyBzMSA9ICJhYmNkZSIsIHMyID0gImFjZSI7CiAgICBMQ1MoczEsIHMyKTsKfQo=