#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int longestCommonSubsequence(string text1, string text2) {
int m = text1.size();
int n = text2.size();
vector< vector<int>> dp(m+1, vector<int> (n+1,-1));
for(int i = 0; i <= m; ++i) {
for(int j = 0; j <= n; ++j) {
if(i == 0 || j == 0) {
dp[i][j] = 0;
} else {
if(text1[i-1] == text2[j-1]) {
dp[i][j] = 1 + dp[i-1][j-1];
} else {
dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
}
}
}
}
return dp[m][n];
}
};
int main() {
Solution lcs;
string text1 = "abdr";
string text2 = "acdr"; int ans = lcs.longestCommonSubsequence(text1,text2);
cout<<ans;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKY2xhc3MgU29sdXRpb24gewpwdWJsaWM6CiAgICBpbnQgbG9uZ2VzdENvbW1vblN1YnNlcXVlbmNlKHN0cmluZyB0ZXh0MSwgc3RyaW5nIHRleHQyKSB7CiAgICAgICAgaW50IG0gPSB0ZXh0MS5zaXplKCk7CiAgICAgICAgaW50IG4gPSB0ZXh0Mi5zaXplKCk7CiAgICAgICAgdmVjdG9yPCB2ZWN0b3I8aW50Pj4gZHAobSsxLCB2ZWN0b3I8aW50PiAobisxLC0xKSk7ICAgCiAgICAgICAgZm9yKGludCBpID0gMDsgaSA8PSBtOyArK2kpIHsKICAgICAgICAgICAgZm9yKGludCBqID0gMDsgaiA8PSBuOyArK2opIHsKICAgICAgICAgICAgICAgIGlmKGkgPT0gMCB8fCBqID09IDApIHsKICAgICAgICAgICAgICAgICAgICBkcFtpXVtqXSA9IDA7CiAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgIGlmKHRleHQxW2ktMV0gPT0gdGV4dDJbai0xXSkgewogICAgICAgICAgICAgICAgICAgICAgICAgIGRwW2ldW2pdID0gMSArIGRwW2ktMV1bai0xXTsKICAgICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgICAgICAgIGRwW2ldW2pdID0gbWF4KGRwW2ktMV1bal0sIGRwW2ldW2otMV0pOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICByZXR1cm4gZHBbbV1bbl07CiAgICB9Cn07CmludCBtYWluKCkgewpTb2x1dGlvbiBsY3M7CnN0cmluZyB0ZXh0MSA9ICJhYmRyIjsKc3RyaW5nIHRleHQyID0gImFjZHIiOyBpbnQgYW5zID0gbGNzLmxvbmdlc3RDb21tb25TdWJzZXF1ZW5jZSh0ZXh0MSx0ZXh0Mik7Cgpjb3V0PDxhbnM7Cn0K