Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.91 KB | None | 0 0
  1. /*
  2.  
  3. For example, with strings source = "ABCDEFG"
  4. target = "ABDFFGH" we might return:
  5. ["A", "B", "-C", "D", "-E", "F", "+F", "G", "+H"]
  6.  
  7. .
  8. CCCACDC
  9. BCCBCDC
  10. .
  11.  
  12. // add + remove + 3 operations
  13. // add + keeping + remove + 2 operations
  14.  
  15. // if we are at position i, and we will use the letter in position i+x
  16. // we will have X add operations + keeping
  17.  
  18. //
  19.  
  20.  
  21. // if the current letter is not in the next letters of the target
  22. // you should remove
  23.  
  24. -A B +B C D C // 6 operations
  25.  
  26. // removing
  27. -A B -C -D -C +B +C +D +C // 9 operations
  28.  
  29. // adding
  30. also not optimal
  31.  
  32. input: str source | str target
  33. return: array of operations in the form [-+]?[A-Z]
  34.  
  35. /////////////////////////
  36.  
  37. approach number 2:
  38.  
  39.         .
  40. source: CDEFG
  41. target: DFFGH
  42.         .
  43.  
  44. recursive function(int positionInSource, int positionInTarget) {
  45.   sourceLetter = source[positionInSource]
  46.   targetLetter = target[positionInTarget]
  47.  
  48.   if (targetLetter == sourceLetter) {
  49.     cost = 1
  50.     cost += function(positionInSource+1, positionInTarget+1)
  51.     return
  52.   }
  53.  
  54.   // here they are different
  55.  
  56.   costRemove = 1 // -Letter
  57.   costRemove += function(positionInSource+1, positionInTarget)
  58.  
  59.   costAdd = 1
  60.   costAdd += function(positionSource, positionInTarget+1)
  61.  
  62.   return min(costRemove, costAdd)
  63. }
  64.  
  65. */
  66.  
  67. int solve(int srcIndex, int tgtIndex, const string& source, const string& target, vector<vector<int>>& dp) {
  68.   if (dp[srcIndex][tgtIndex] != -1) {
  69.     return dp[srcIndex][tgtIndex];
  70.   }
  71.  
  72.   if (tgtIndex >= target.size() && srcIndex >= source.size()) {
  73.    
  74.   }
  75.   if (tgtIndex >= target.size()) {
  76.     int costToRemove = 1 + solve(srcIndex + 1, tgtIndex, source, target, dp);
  77.     dp[srcIndex][tgtIndex] = costToRemove;
  78.     return costToRemove;
  79.   }
  80.   if (srcIndex >= source.size()) {
  81.     int costToAdd = 1 + solve(srcIndex, tgtIndex + 1, source, target, dp);
  82.     dp[srcIndex][tgtIndex] = costToAdd;
  83.     return costToAdd;
  84.   }
  85.  
  86.   char srcLetter = source[srcIndex];
  87.   char tgtLetter = target[tgtIndex];
  88.  
  89.   if (srcLetter == tgtLetter) {
  90.     int cost = 1;
  91.     cost += solve(srcIndex + 1, tgtIndex + 1, source, target, dp);
  92.     dp[srcIndex][tgtIndex] = cost;
  93.     return cost;
  94.   }
  95.  
  96.   int costToRemove = 1 + solve(srcIndex + 1, tgtIndex, source, target, dp);
  97.   int costToAdd = 1 + solve(srcIndex, tgtIndex + 1, source, target, dp);
  98.  
  99.   int minimalCost = min(costToRemove, costToAdd);
  100.   dp[srcIndex][tgtIndex] = minimalCost;
  101.   return minimalCost;
  102. }
  103.  
  104. vector<string> getMoves(string source, string target) {
  105.   vector<string> answer;
  106.   vector<vector<int>> dp;
  107.  
  108.   for (int i = 0; i < source.size(); i++) {
  109.     dp[i] = vector<int>(target.size(), -1);
  110.   }
  111.  
  112.   solve(0, 0, source, target, dp);
  113.  
  114.   int minimalCost = dp[0][0];
  115.  
  116.   pair<int,int> nextPos = dpPos[0][0];
  117.  
  118.   return answer;
  119. }
  120.  
  121. //
  122. *
  123.  
  124.  
  125. B
  126.  
  127.  
  128. 1+ -1 -1
  129. -1 -1 -1
  130. -1 -1 -1
  131.  
  132. (1,1) (x,x) (x,x)
  133. (x,x) (2,1) (x,x)
  134. (x,x) (2,2) (-1,-1)
  135.  
  136. *//
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement