Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.92 KB | None | 0 0
  1. // GROUP: 3
  2. // ID: 20150202
  3. // Assign: 11
  4. // Assign: EditDist
  5. // UVA: 526
  6. // Name: Mohamed Ahmed Abdelmonim
  7. // UVA Username: mmn3mm
  8.  
  9. #include <cstdio>
  10. #include <cstdlib>
  11. #include <cstring>
  12. #include <iostream>
  13. using namespace std;
  14.  
  15. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  16.  
  17. char a[82];
  18. char b[82];
  19.  
  20. int D[81][81]; // D[][] is the same for all versions (no memory reduction)
  21.  
  22.  
  23. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  24.  
  25. int T1[81][81];
  26. int EditDist1(int n, int m)
  27. {
  28. if (T1[n][m] != -1)return T1[n][m];
  29. if (n == 0 || m == 0)
  30. {
  31. T1[n][m] = n + m;
  32. return n + m;
  33. }
  34. int dummy = 1;
  35. if (a[n-1] == b[m-1])dummy = 0;
  36. int replace = dummy + EditDist1(n - 1, m - 1);
  37. int remove = 1 + EditDist1(n - 1, m);
  38. int insert = 1 + EditDist1(n, m - 1);
  39. D[n][m] = 2; //That means we assume it's insert unless changed.
  40. cout << "n:" << n << " m:" << m << endl;
  41. int ret = insert;
  42. cout << "Replace " << replace << " ";
  43. cout << "Delete " << remove << " ";
  44.  
  45. cout << "INSERT " << insert << " ";
  46. if (replace < insert)
  47. {
  48. ret = replace;
  49. D[n][m] = 1; //Changed to replace.
  50. }
  51. if (ret > remove){
  52. ret = remove;
  53. D[n][m] = 3;//Changed to remove
  54. }
  55. cout << endl;
  56. cout << "CHOSEN " << ret << " ";
  57. cout << endl;
  58. T1[n][m] = ret;
  59. return ret;
  60. }
  61. int ComputeEditDist1(int N, int M){ // Recursive - memoization - initialize T then call EditDist1(N,M);
  62. for (int i = 0; i <= N; i++)
  63. {
  64. for (int j = 0; j <=M; j++)
  65. T1[i][j] = -1;
  66. }
  67. EditDist1(N, M);
  68. return T1[N][M];
  69. }
  70. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  71.  
  72. int T2[81][81];
  73. int ComputeEditDist2(int N, int M); // Bottom-up, do not save space
  74.  
  75. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  76.  
  77. int T3[2][81];
  78. int ComputeEditDist3(int N, int M); // Bottom-up, use two rows only
  79.  
  80. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  81.  
  82. int T4[81];
  83. int ComputeEditDist4(int N, int M); // Bottom-up, save maximum space
  84.  
  85. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  86.  
  87. struct Oper
  88. {
  89. int n, m; //1: replace , 2: Insert, 3:Remove
  90. int oper;
  91. };
  92.  
  93. Oper opers[81];
  94.  
  95. int EditDistSol(int N, int M){ // Print the solution using D[][]
  96. int n = N, m = M;
  97. int i = 0;
  98. while (n>0 && m>0)
  99. {
  100. if (D[n][m] == 3) //remove
  101. {
  102. Oper oper;
  103. oper.oper = 3;
  104. oper.n = n;
  105. n -= 1;
  106. opers[i++] = oper;
  107. continue;
  108. }
  109. else if (D[n][m] == 2) //insert
  110. {
  111. Oper oper;
  112. oper.oper = 2;
  113. oper.n = n;
  114. oper.m = b[m - 1];
  115. m-= 1;
  116. opers[i++] = oper;
  117. continue;
  118. }
  119. else if (D[n][m] == 1 && a[n - 1] == b[m - 1]) //replace
  120. {
  121. Oper oper;
  122. oper.oper = 3;
  123. oper.n = n;
  124. oper.m = b[m-1];
  125. n -= 1;
  126. m -= 1;
  127. opers[i++] = oper;
  128. continue;
  129. }
  130. n--;
  131. m--;
  132. }
  133. while (true)
  134. {
  135. if (n == 0 && m == 0)break;
  136.  
  137. if (n == 0 && m != 0)
  138. {
  139. Oper oper;
  140. oper.oper = 2;
  141. oper.n = n;
  142. m -= 1;
  143. opers[i++] = oper;
  144. }
  145. if (n != 0 && m == 0)
  146. {
  147. Oper oper;
  148. oper.oper = 3;
  149. oper.n = n;
  150. n -= 1;
  151. opers[i++] = oper;
  152. }
  153. }
  154. int c = 0;
  155. for (int j = i-1; j >= 0; j--)
  156. {
  157. c++;
  158.  
  159. cout << c <<" ";
  160. Oper curr = opers[j];
  161. switch (curr.oper){
  162. case 1:
  163. cout << "Replace " << curr.n << "," << char(curr.m);
  164. break;
  165. case 2:
  166. cout << "Insert " << curr.n << "," << char(curr.m);
  167. break;
  168. case 3:
  169. cout << "Delete " << curr.n;
  170.  
  171. break;
  172.  
  173. }
  174. cout << endl;
  175.  
  176. }
  177. return 0;
  178. }
  179. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  180.  
  181. int ComputeEditDist(int N, int M) // Here we can choose the method
  182. {
  183. return ComputeEditDist1(N,M);
  184. //return ComputeEditDist2(N,M);
  185. //return ComputeEditDist3(N,M);
  186. //return ComputeEditDist4(N, M);
  187. }
  188.  
  189. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  190.  
  191. // This function does not need any modification:
  192. void Compute() // Get input and call ComputeEditDist() whenever needed
  193. {
  194. int cas = 0;
  195. while (true)
  196. {
  197. a[0] = 0; b[0] = 0;
  198. if (!fgets(a, 82, stdin)) break; fgets(b, 82, stdin);
  199. a[strlen(a) - 1] = 0;
  200. b[strlen(b) - 1] = 0;
  201. if (cas) cout << endl; // print an empty line between test cases
  202. int N = strlen(a), M = strlen(b);
  203. ComputeEditDist(N, M);
  204. EditDistSol(N, M);
  205. cas++;
  206. }
  207. }
  208.  
  209. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  210.  
  211. int main()
  212. {
  213. //freopen("input.txt", "r", stdin);
  214. Compute();
  215. return 0;
  216. }
  217.  
  218. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement