Advertisement
benyeh

Untitled

Apr 9th, 2013
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.18 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstdio>
  4. #include <cstdlib>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. int turnAtoB(int cost, string A, string B, int ind){
  10.     int out, out1, out2, temp;
  11.     string A2 = A, A3 = A;
  12.  
  13.     if(A == B) out = cost;
  14.     else {
  15.         if(ind >= A.length()){
  16.             int toInsert = B[ind]-'a'+1;
  17.             A2.insert(ind, 1, B[ind]);
  18.             out = turnAtoB(cost+toInsert, A2, B, ind);
  19.         } else if(ind >= B.length()){
  20.             int toDelete = A[ind]-'a'+1;
  21.             A3.erase(ind, 1);
  22.             out = turnAtoB(cost+toDelete, A3, B, ind);
  23.         } else {
  24.             if(A[ind] != B[ind]){
  25.                 int toInsert = B[ind]-'a'+1, toDelete = A[ind]-'a'+1;
  26.  
  27.                 A2.insert(ind, 1, B[ind]);
  28.                 out1 = turnAtoB(cost+toInsert, A2, B, ind);
  29.  
  30.                 A3.erase(ind, 1);
  31.                 out2 = turnAtoB(cost+toDelete, A3, B, ind);
  32.  
  33.                 if(out1 > out2) out = out2;
  34.                 else out = out1;
  35.             } else {
  36.                 out = turnAtoB(cost, A, B, ind+1);
  37.             }
  38.         }
  39.     }
  40.     return out;
  41. }
  42.  
  43. int main() {
  44.     string fname, A, B;
  45.     cin >> fname;
  46.     ifstream inputFile(fname.c_str());
  47.     int t, cost = 0;
  48.     inputFile >> t;
  49.     for(int i = 0; i < t; i++){
  50.         inputFile >> A;
  51.         inputFile >> B;
  52.         cost = turnAtoB(0, A, B, 0);
  53.         cout << cost << endl;
  54.     }
  55.  
  56.     return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement