Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- int main()
- {
- string a, b;
- cin >> a >> b;
- vector<vector<int> > dp(a.size() + 1, vector<int>(b.size() + 1, -2e9));
- dp[0][0] = 0;
- for(int i = 0; i < a.size(); i++) {
- for(int j = 0; j < b.size(); j++) {
- if(a[i] == b[j]) {
- dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + 1);
- }
- dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
- dp[i][j + 1] = max(dp[i][j + 1], dp[i][j]);
- }
- }
- int lcs = 0;
- for(int i = 0; i <= a.size(); i++) {
- for(int j = 0; j <= b.size(); j++) {
- lcs = max(lcs, dp[i][j]);
- }
- }
- cout << lcs << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement