Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <string>
- #include <stdio.h>
- #include <time.h>
- using namespace std;
- int horspool(vector<char> t,vector<char> p ) {
- int m = p.size();
- int n = t.size();
- int d2[256];
- int* d = &d2[128];
- //int d[256];
- int j;
- int pos;
- int count=0;
- //PREPROCESSING
- //standardmaeßig werden vorerst alle d auf die Laenge des Patterns m gesetzt
- for (int i=-128; i<128; i++) {
- d[i] = m;
- }
- //Bei welchem Zeichen kann man wie weit springen?
- for (int i=0; i<m-1; i++) {
- d[(int)p[i]] = m-i-1;
- }
- //Ausgaben zum Testen
- /*cout << "Pattern: " << p << endl;
- cout << "Text: " << t << endl;
- cout << "m=" << m << endl;
- for (int i=0; i<m; i++) {
- cout << "d[" << p[i] << "] = " << d[(int)p[i]] << endl;
- }*/
- //SEARCHING
- pos = 0;
- while (pos < n-m+1) {
- j=m;
- while (j>0 && t[pos+j-1] == p[j-1])
- j--;
- if (j==0) {count++;}
- pos = pos + d[(int) t[pos+m-1]];
- }
- return count;
- }
- bool loadText(vector<char> &text, char* fileName)
- {
- // open file in binary and read-only mode
- // and set file pointer to the end
- ifstream file(fileName, ios::in | ios::binary | ios::ate);
- if (!file.is_open())
- return false;
- // file pointer position is equal to file size
- ifstream::pos_type size = file.tellg();
- // resize string buffer
- text.resize(size);
- // go to file begin and read complete file
- file.seekg(0, ios::beg);
- file.read(text.data(), size);
- file.close();
- return true;
- }
- int main(int argc, char* argp[])
- {
- clock_t timeStart,timeEnd;
- for (int i=0; i<argc; i++){
- cout << i+1 << "ter Paramter: " << argp[i] << endl;
- }
- if (argc < 3)
- {
- cerr << argp[0] << " <TEXTFILE> <PATTERN>" << endl;
- return 0;
- }
- vector<char> text;
- cout<<"Text wird laden...";
- timeStart = clock();
- if (!loadText(text, argp[1]))
- {
- cerr << "Could not open text file." << endl;
- return 1;
- }
- timeEnd = clock();
- cout<<"abgeschlossen nach "<<((float)(timeEnd-timeStart))/CLOCKS_PER_SEC<<"s"<<std::endl;
- vector<char> pattern;
- for (int i=0; i<argp[2][i];i++) {
- pattern.push_back(argp[2][i]);
- cout << pattern[i] << endl;
- }
- cout << horspool(text, pattern) << endl;
- // fill in your code here ...
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement