#include #include using namespace std; int lcs(string s, string t) { // We need a grid of s.size() x t.size() vector > grid; grid.resize(s.size() + 1); for (unsigned int i = 0 ; i < grid.size(); i++) { grid[i].resize(t.size() + 1); } // We need to fill in the base cases for (unsigned int i = 0; i < grid.size(); i++) { grid[i][0] = 0; } for (unsigned int i = 0; i < grid[0].size(); i++) { grid[0][i] = 0; } // Now, for the rest of them for (unsigned int i = 1; i < grid.size(); i++) { for (unsigned int j = 1; j < grid[i].size(); j++) { if (s[i - 1] == t[j - 1]) { grid[i][j] = 1 + grid[i - 1][j - 1]; } grid[i][j] = max(grid[i][j], max(grid[i][j - 1], grid[i - 1][j])); } } return grid[s.size()][t.size()]; } int main() { string s; string t; cin >> s >> t; cout << "The longest subsequence of these is of len " << lcs(s, t) << endl; }