class Solution {
public:
int longestCommonSubsequence(string text1, string text2) {
vector<vector<int>> dp(text1.length() + 1, vector<int>(text2.length() + 1, 0));
int res = 0;
for(int i = 1; i <= text1.length(); i++){
for(int j = 1; j <= text2.length(); j++){
if(text1[i - 1] == text2[j - 1]){
dp[i][j] = dp[i - 1][j - 1] + 1;
if(dp[i][j] > res)
res = dp[i][j];
}
else{
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
return res;
}
};
04-18
256
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)