Advertisement
Guest User

Untitled

a guest
May 20th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. bool RideShare::kmpStringAlgorithm(const string total,const string partial) { //Algorithm for exact string search
  2.  
  3. int m = partial.length();
  4. int n = total.length();
  5. int pi[m];
  6.  
  7. computePrefixFunction(partial, pi);
  8.  
  9. int i = 0; // index for total[] string
  10. int j = 0; // index for partial[] string
  11. while (i < n) {
  12.  
  13. if (tolower(partial[j]) == tolower(total[i])){ //case insensitive o codigo ASCII das minusculas é o das maiusculas + 32
  14.  
  15. j++;
  16. i++;
  17. }
  18.  
  19. if (j == m) {
  20.  
  21. return true;
  22. }
  23.  
  24. // mismatch after j matches
  25. else if (i < n && tolower(partial[j]) != tolower(total[i])) {
  26. if (j != 0)
  27. j = pi[j - 1];
  28. else
  29. i = i + 1;
  30. }
  31. }
  32. return false;
  33. }
  34.  
  35. void RideShare::computePrefixFunction(string partial, int *pi) {
  36.  
  37. int m = partial.length();
  38. // length of the previous longest prefix suffix
  39. int len = 0;
  40.  
  41. pi[0] = 0; // lps[0] is always 0
  42.  
  43. // the loop calculates lps[i] for i = 1 to M-1
  44. int i = 1;
  45. while (i < m) {
  46. if (tolower(partial[i]) == tolower(partial[len])) {
  47. len++;
  48. pi[i] = len;
  49. i++;
  50. } else // (pat[i] != pat[len])
  51. {
  52. // This is tricky. Consider the example.
  53. // AAACAAAA and i = 7. The idea is similar
  54. // to search step.
  55. if (len != 0) {
  56. len = pi[len - 1];
  57.  
  58. // Also, note that we do not increment
  59. // i here
  60. } else // if (len == 0)
  61. {
  62. pi[i] = 0;
  63. i++;
  64. }
  65. }
  66. }
  67. }
  68.  
  69.  
  70. bool RideShare::editDistanceAlgorithm(string pat, string txt){
  71.  
  72. int n = txt.length();
  73. int m = pat.length();
  74. int after, before;
  75. int d[n+1];
  76.  
  77. for (int i = 0; i < n + 1; i++) {
  78.  
  79. d[i] = i;
  80. }
  81.  
  82. for (int i = 1; i < m + 1; i++) {
  83.  
  84. before = d[0];
  85. d[0] = i;
  86.  
  87. for(int k = 1; k < n+1; k++){
  88.  
  89. if(pat[i - 1] == txt[k - 1]){
  90.  
  91. after = before;
  92. } else{
  93.  
  94. after = min(before, d[k]);
  95. after = 1 + min(after, d[k-1]);
  96. }
  97.  
  98. before = d[k];
  99. d[k] = after;
  100. }
  101. }
  102.  
  103. //return d[n];
  104. float txtLength = txt.length();
  105. float edalgValue = ((float)d[n]) / txtLength;
  106. if(edalgValue <= 0.70)
  107. return true;
  108. else
  109. return false;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement