Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdlib.h>
- #include <time.h>
- #include <string.h>
- #include <math.h>
- using namespace std;
- const int N = 100; //długość tesktu
- const int M = 3 ; //długość wzorca
- int T[N];
- int P[M];
- int hash(int x[])
- {
- int h = 0;
- for(int i = 0, j = M-1; i < M; i++, j--)
- h += x[i] * pow(2,j);
- return h;
- }
- void init()
- {
- for(int i=0;i<N;i++)
- T[i] = rand()%3;
- }
- void search()
- {
- for(int i=0;i<N;i++)
- cout<<T[i];
- cout<<endl;
- cout<<endl;
- cout<<"Podaj wzorzec: ";
- for(int i = 0; i < M; i++)
- cin>>P[i];
- for(int i = 0; i < N-M+1; i++)
- for(int j = 0; P[j]==T[i+j]; j++)
- if(j == M-1)
- cout<<i<<endl;
- }
- void KRsearch()
- {
- for(int i=0;i<N;i++)
- cout<<T[i];
- cout<<endl;
- cout<<endl;
- cout<<"Podaj wzorzec: ";
- for(int i = 0; i < M; i++)
- cin>>P[i];
- int hT = hash(T);
- int hP = hash(P);
- int i = 0;
- while(i <= N-M+1)
- {
- if(hT == hP)
- {
- for(int j = 0; P[j]==T[i+j]; j++)
- if(j == M-1)
- cout<<i<<endl;
- }
- i++;
- hT = (hT - pow(2,M-1)*T[i-1]) * 2 + T[i+M];
- }
- }
- void BMsearch(){} //zadanie 3
- int main(int argc, char *argv[]){
- srand(time(NULL));
- init();
- //search();
- KRsearch();
- BMsearch();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement