Created
June 20, 2018 14:19
-
-
Save s4553711/9bccdc442fb5cdfba873687009ae98f0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
vector<vector<int> > C(s1.length() + 1, vector<int>(s2.length() + 1, 0)); | |
// initial | |
for (int i = 1; i < s2.length() + 1; ++i) { | |
C[0][i] = C[0][i - 1] + s2[i - 1]; | |
} | |
for (int i = 1; i < s1.length() + 1; ++i) { | |
C[i][0] = C[i - 1][0] + s1[i - 1]; | |
} | |
for (int i = 1; i < s1.length() + 1; ++i) { | |
for (int j = 1; j < s2.length() + 1; ++j) { | |
if (s1[i - 1] == s2[j - 1]) { | |
C[i][j] = C[i - 1][j - 1]; | |
} else { | |
// delete s1[i-1] or s2[j-1] | |
C[i][j] = min(C[i - 1][j] + s1[i - 1], C[i][j - 1] + s2[j - 1]); | |
} | |
} | |
} | |
return C[s1.length()][s2.length()]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment