Advertisement
amsavchenko

Untitled

Dec 1st, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. #include <iostream>
  2. #include <string.h>
  3. #include <locale>
  4.  
  5.  
  6. using namespace std;
  7.  
  8. int* countMatrix(char *string, char *target, int leng1, int leng2) {
  9.  
  10. int i = leng1 + 1;
  11. int j = leng2 + 1;
  12. int *D = new int[i*j];
  13. *D = 0;
  14. for (int k = 1; k <= i; k++) {
  15. *(D + j * k) = k;
  16. }
  17.  
  18. for (int g = 1; g <= j; g++) {
  19. *(D + g) = g;
  20.  
  21. for (int k = 1; k <= i; k++) {
  22. if (string[k - 1] == target[g - 1]) {
  23. *(D + k * j + g) = *(D + (k - 1) * j + g - 1);
  24. }
  25. else {
  26. *(D + k * j + g) = *(D + (k - 1) * j + g - 1) + 1;
  27. if (*(D + k * j + g) > *(D + k * j + g - 1) + 1) {
  28. *(D + k * j + g) = *(D + k * j + g - 1) + 1;
  29. }
  30. if (*(D + k * j + g) > *(D + (k - 1) * j + g) + 1) {
  31. *(D + k * j + g) = *(D + (k - 1) * j + g) + 1;
  32. }
  33. }
  34. }
  35. }
  36.  
  37.  
  38. return D;
  39. }
  40.  
  41. int LevenshteinDistance(char *string, char *target, int leng1, int leng2) {
  42.  
  43. int* D = countMatrix(string, target, leng1, leng2);
  44.  
  45. return *(D + (leng2+1) * leng1 + leng2);
  46. }
  47.  
  48. typedef struct _bufer{
  49. int i,j;
  50. }bufer;
  51.  
  52. void editPrescription(char *string, char *target, int leng1, int leng2){
  53.  
  54. int* D = countMatrix(string, target, leng1, leng2);
  55.  
  56. int operation,pred;
  57.  
  58. int i = leng1+1;
  59. int j = leng2+1;
  60.  
  61. bufer myBufer;
  62.  
  63. myBufer.i = leng1;
  64. myBufer.j = leng2;
  65.  
  66. while((myBufer.i != 0) && (myBufer.j != 0)){
  67.  
  68. if(myBufer.i == 0){ // if on the edge
  69. myBufer.j--;
  70. cout << "insert: " << target[j-1] << endl;
  71. }else
  72. if(myBufer.j == 0){
  73. myBufer.i--;
  74. cout << "delete: " << string[i-1] << endl;
  75. }else
  76. {
  77. if(string[myBufer.i-1] == target[myBufer.j-1]){ // if match
  78. cout << "no changes" << endl;
  79. myBufer.i--;
  80. myBufer.j--;
  81.  
  82. }else{
  83.  
  84. // 0 - change
  85. // 1 - insert
  86. // 2 - delete
  87. pred = *(D + j*(myBufer.i-1) + myBufer.j-1);
  88. operation = 0;
  89.  
  90. if (pred > *(D + j*(myBufer.i) + myBufer.j-1)){
  91. operation = 1;
  92. }
  93.  
  94. if (pred > *(D + j*(myBufer.i-1) + myBufer.j)){
  95. operation = 2;
  96. }
  97. //
  98. switch (operation){
  99. case 0:
  100. cout << "change " << string[myBufer.i-1] << " to " << target[myBufer.j-1] << endl;
  101. myBufer.i--;
  102. myBufer.j--;
  103. break;
  104. case 1:
  105. cout << "insert " << target[myBufer.j-1] << endl;
  106. myBufer.j--;
  107. break;
  108. case 2:
  109. cout << "delete " << string[myBufer.i-1] << endl;
  110. myBufer.i--;
  111. break;
  112. }
  113.  
  114. }
  115. }
  116.  
  117. }
  118.  
  119.  
  120. }
  121.  
  122. int main() {
  123.  
  124. //setlocale( LC_ALL,"Russian" );
  125.  
  126.  
  127. char myString[5];
  128. strcpy(myString, "fuck");
  129. char myString2[6];
  130. strcpy(myString2, "lucky");
  131.  
  132. cout << LevenshteinDistance(myString,myString2, 5, 6);
  133.  
  134. editPrescription(myString, myString2, 4, 5);
  135.  
  136. //system("pause");
  137. return 0;
  138.  
  139.  
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement