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