Skip to content

Instantly share code, notes, and snippets.

@viennadd
Created April 5, 2016 16:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save viennadd/ec05d7d5bd220b48256f6f72eece974a to your computer and use it in GitHub Desktop.
Save viennadd/ec05d7d5bd220b48256f6f72eece974a to your computer and use it in GitHub Desktop.
/* DP Execrise */
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
const int MAX_M = 1000;
const int MAX_N = 1000;
int dp[MAX_M][MAX_N];
int main()
{
string a, b;
cin >> a >> b;
const char *pa = a.c_str();
const char *pb = b.c_str();
/* if a[i] == b[j] then max_seq += 1 else max_seq = max(max_seq[i][j-1],max_seq[i-1][j])*/
/* boundary: i == 0 || j == 0 */
for (int i = 0; i < a.size(); ++i) {
for (int j = 0; j < b.size(); ++j) {
if (i == 0 || j == 0) {
dp[i][j] = pa[i] == pb[j] ? 1 : 0;
} else if (pa[i] == pb[j]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]);
}
}
}
cout << dp[a.size() - 1][b.size() - 1] << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment