MAGCARI

Edit Distance

Jan 9th, 2023
1,009
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.70 KB | None | 0 0
  1. /*
  2.     Task    : _example
  3.     Author  : Phumipat C. [MAGCARI]
  4.     Language: C++
  5.     Created : 09 January 2023 [19:54]
  6. */
  7. #include<bits/stdc++.h>
  8. using namespace std;
  9. char a[1010],b[1010];
  10. int dp[1010][1010];
  11. int main(){
  12.     cin.tie(0)->sync_with_stdio(0);
  13.     cin.exceptions(cin.failbit);
  14.     int c1,c2,c3;   //c1 = cost of adding, c2 = cost of removing, c3 = cost of replacing
  15.     cin >> a+1 >> b+1 >> c1 >> c2 >> c3;
  16.     int n = strlen(a+1),m = strlen(b+1);
  17.     for(int i=1;i<=n;i++)   dp[i][0] = i;
  18.     for(int j=1;j<=m;j++)   dp[0][j] = j;
  19.     for(int i=1;i<=n;i++){
  20.         for(int j=1;j<=m;j++){
  21.             if(a[i] == b[j])
  22.                 dp[i][j] = dp[i-1][j-1];
  23.             else
  24.                 dp[i][j] = min({dp[i-1][j]+c1,dp[i][j-1]+c2,dp[i-1][j-1]+c3});
  25.         }
  26.     }
  27.     cout << dp[n][m] << '\n';
  28.     return 0;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment