https://leetcode-cn.com/problems/longest-common-subsequence/
Given two strings text1 and text2, return the longest common subsequence of the two strings.
A sub-sequence of a character string refers to a new character string: it is a new character string formed by deleting some characters (or not deleting any characters) without changing the relative order of characters.
For example, "ace" is a subsequence of "abcde", but "aec" is not a subsequence of "abcde". The "common subsequence" of the two character strings is the subsequence shared by the two character strings.
If there is no common subsequence for these two strings, 0 is returned.
class Solution {
public:
int longestCommonSubsequence(string text1, string text2) {
int size1 = text1.size(), size2 = text2.size();
int c[size1 + 1][size2 + 1];
for (int k = 0; k <= size1; ++k) {
c[k][0] = 0;
}
for (int k = 0; k <= size2; ++k) {
c[0][k] = 0;
}
for (int i = 1; i <= size1; ++i) {
for (int j = 1; j <= size2; ++j) {
if(text1[i - 1] == text2[j - 1]) {
c[i][j] = c[i - 1][j - 1] + 1;
} else if(c[i - 1][j] < c[i][j - 1]) {
c[i][j] = c[i][j - 1];
} else {
c[i][j] = c[i - 1][j];
}
}
}
return c[size1][size2];
}
};
int main() {
Solution s;
cout << s.longestCommonSubsequence("abcde", "ace");
}