longest-common-subsequence 1.0.0
Longest Common Subsequence
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1#include <bits/stdc++.h>
2
3using namespace std;
4
6{
7public:
8 int longestCommonSubsequence(string text1, string text2)
9 {
10 vector<vector<int>> dp((int)text1.size() + 1, vector<int>((int)text2.size() + 1, 0));
11 for (int i = (int)text1.size() - 1; i >= 0; --i)
12 for (int j = (int)text2.size() - 1; j >= 0; --j)
13 if (text1[i] == text2[j])
14 dp[i][j] = 1 + dp[i + 1][j + 1];
15 else
16 dp[i][j] = max(dp[i][j + 1], dp[i + 1][j]);
17 return dp[0][0];
18 }
19};
20
21int main()
22{
23 // string text1 = "bl";
24 // string text2 = "yby";
25 string text1 = "abcde";
26 string text2 = "ace";
27 cout << "output: " << Solution().longestCommonSubsequence(text1, text2) << endl;
28 return 0;
29}
int longestCommonSubsequence(string text1, string text2)
Definition main.cpp:8
int main()
Definition main.cpp:21