Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.78 KB | None | 0 0
  1. //Edit Distance Top-Down
  2. //https://www.spoj.com/problems/EDIST/
  3. #include <bits/stdc++.h>
  4. #define int long long
  5. using namespace std;
  6.  
  7. int dp[2001][2001];
  8. string s1, s2;
  9.  
  10. int lcs(int x, int y){
  11.     if(x == -1 || y == -1)
  12.         return abs(y-x);
  13.     if(dp[x][y] != -1)
  14.         return dp[x][y];
  15.     int resp = LLONG_MAX;
  16.     if(s1[x] == s2[y])
  17.         resp = lcs(x-1, y-1);
  18.     else
  19.         resp = 1 + min(lcs(x-1, y), min(lcs(x, y-1), lcs(x-1, y-1)));
  20.     return dp[x][y] = resp;
  21. }
  22.  
  23. int32_t main(){
  24.     ios::sync_with_stdio(false); cin.tie(0);
  25.     int t; cin >> t;
  26.     while(t--){
  27.         cin >> s1 >> s2;
  28.         for(int i = 0; i < 2001; ++i)
  29.             for(int j = 0; j < 2001; ++j)
  30.                 dp[i][j] = -1;
  31.         cout << lcs((int)s1.size()-1, (int)s2.size()-1) << '\n';
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement