Skip to content

Instantly share code, notes, and snippets.

@nerohoop
Last active July 23, 2017 05:59
Show Gist options
  • Save nerohoop/cefffcad88ebd18ca5e118c9725bfb5a to your computer and use it in GitHub Desktop.
Save nerohoop/cefffcad88ebd18ca5e118c9725bfb5a to your computer and use it in GitHub Desktop.
If X[m-1] == Y[n-1] then
L(X[0..m-1], Y[0..n-1]) = 1 + L(X[0..m-2], Y[0..n-2])
Else
L(X[0..m-1], Y[0..n-1]) = MAX ( L(X[0..m-2], Y[0..n-1]), L(X[0..m-1], Y[0..n-2])
// Implementation
int L[n+1][m+1];
// Start
for(int i=1; i<=n; i++) {
for(int j=1; j<=m; i++) {
if(i == 0 || j== 0) {
L[i][j] = 0;
} else if (A[i-1] == B[j-1]) {
L[i][j] = L[i-1][j-1] + 1;
} else {
L[i][j] = MAX(L[i-1][j], L[i][j-1]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment