Back
Boyer_Moore_Horspool.cpp
Save
// Boyer_Moore_Horspool.cpp #include
using namespace std; int char_in_string(char T, char *P) { int i = 0; while (P[i] != '\0') { if (P[i] == T) return i; i++; } return -1; } int Boyer_Moore_Horspool(char *T, char *P) { int dem = 0, i = strlen(P) - 1, v = strlen(P); while (i < strlen(T)) { int x = v - 1, j = i; while (T[j] == P[x] && x > -1) { j--; x--; } if (x < 0) { dem++; i = i + v; } else { x = char_in_string(T[j], P); if (x < 0) i = i + v; else i = j + v - x - 1; } } return dem; } int main() { char *t = (char*)"toi co 1000, Nam co 21000, Dong co 1000 va 500"; char *p = (char*)"1000"; int count = 0; count = Boyer_Moore_Horspool(t, p); cout << "Tu " << p << " xuat hien " << count << " lan" << endl; return 0; }