Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <string>
  5. #include <stdio.h>
  6. #include <time.h>
  7.  
  8. using namespace std;
  9.  
  10.  
  11. int horspool(vector<char> t,vector<char> p ) {
  12. int m = p.size();
  13. int n = t.size();
  14. int d2[256];
  15. int* d = &d2[128];
  16. //int d[256];
  17. int j;
  18. int pos;
  19. int count=0;
  20.  
  21. //PREPROCESSING
  22. //standardmaeßig werden vorerst alle d auf die Laenge des Patterns m gesetzt
  23. for (int i=-128; i<128; i++) {
  24. d[i] = m;
  25. }
  26. //Bei welchem Zeichen kann man wie weit springen?
  27. for (int i=0; i<m-1; i++) {
  28. d[(int)p[i]] = m-i-1;
  29. }
  30.  
  31. //Ausgaben zum Testen
  32. /*cout << "Pattern: " << p << endl;
  33. cout << "Text: " << t << endl;
  34. cout << "m=" << m << endl;
  35. for (int i=0; i<m; i++) {
  36. cout << "d[" << p[i] << "] = " << d[(int)p[i]] << endl;
  37. }*/
  38.  
  39. //SEARCHING
  40. pos = 0;
  41. while (pos < n-m+1) {
  42. j=m;
  43. while (j>0 && t[pos+j-1] == p[j-1])
  44. j--;
  45. if (j==0) {count++;}
  46. pos = pos + d[(int) t[pos+m-1]];
  47.  
  48. }
  49.  
  50. return count;
  51.  
  52. }
  53. bool loadText(vector<char> &text, char* fileName)
  54. {
  55. // open file in binary and read-only mode
  56. // and set file pointer to the end
  57. ifstream file(fileName, ios::in | ios::binary | ios::ate);
  58. if (!file.is_open())
  59. return false;
  60.  
  61. // file pointer position is equal to file size
  62. ifstream::pos_type size = file.tellg();
  63. // resize string buffer
  64. text.resize(size);
  65. // go to file begin and read complete file
  66. file.seekg(0, ios::beg);
  67. file.read(text.data(), size);
  68. file.close();
  69.  
  70. return true;
  71. }
  72.  
  73. int main(int argc, char* argp[])
  74. {
  75. clock_t timeStart,timeEnd;
  76.  
  77. for (int i=0; i<argc; i++){
  78. cout << i+1 << "ter Paramter: " << argp[i] << endl;
  79. }
  80. if (argc < 3)
  81. {
  82. cerr << argp[0] << " <TEXTFILE> <PATTERN>" << endl;
  83. return 0;
  84. }
  85.  
  86. vector<char> text;
  87.  
  88. cout<<"Text wird laden...";
  89. timeStart = clock();
  90.  
  91. if (!loadText(text, argp[1]))
  92. {
  93. cerr << "Could not open text file." << endl;
  94. return 1;
  95. }
  96. timeEnd = clock();
  97. cout<<"abgeschlossen nach "<<((float)(timeEnd-timeStart))/CLOCKS_PER_SEC<<"s"<<std::endl;
  98.  
  99. vector<char> pattern;
  100. for (int i=0; i<argp[2][i];i++) {
  101. pattern.push_back(argp[2][i]);
  102. cout << pattern[i] << endl;
  103. }
  104.  
  105.  
  106.  
  107.  
  108.  
  109. cout << horspool(text, pattern) << endl;
  110. // fill in your code here ...
  111.  
  112. return 0;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement