Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include<cmath>
- #include<cstdlib>
- #include<ctime>
- using namespace std;
- const int N = 30; //d³ugoœæ tesktu
- const int M = 2 ; //d³ugoœæ wzorca
- const int a=7;
- int T[N];
- int P[M];
- void init()
- {
- for(int i=0;i<N;i++)
- T[i] = rand()%2;
- for(int i=0;i<M;i++)
- P[i] = rand()%2;
- }
- int hash1(int i)
- {
- int has=pow(a,1)*T[i]+pow(a,0)*T[i+1];
- return has;
- }
- void KR(int h)
- {
- int i=0,temp=pow(a,1)*T[0]+pow(a,0)*T[1];
- while(i<=N-M)
- {
- if(h==temp)
- {
- if(P[0]==T[i]&&P[1]==T[i+1])
- {
- cout<<T[i]<<T[i+1];
- cout<<" "<<i <<endl;
- }
- }
- i++;
- temp=hash1(i);
- }
- }
- bool compare(int i)
- {
- if(T[i]==P[0]&& T[i+1]==P[1]) return true;
- else return false;
- }
- void naiwny()
- {
- int i=0;
- while(i<=N-M)
- {
- if(compare(i))
- {
- cout<<T[i]<<T[i+1]<<" "<<i<<endl;
- }
- i++;
- }
- }
- void BM()
- {
- int i=0;
- while(i<=N-M)
- {
- int j=M,s=0;
- while(P[j]==T[i+j-2])
- {
- j--;
- }
- if(j==0)
- {
- cout<<T[i]<<T[i+1]<<" "<<i<<endl;
- }
- if(j==M) s=j;
- i=i+1;
- }
- }
- int main(int argc, char *argv[])
- {
- srand(time(NULL));
- init();
- int h=pow(a,1)*P[0]+pow(a,0)*P[1];
- for(int i=0;i<M;i++) cout<<P[i];
- cout<<endl;
- for(int i=0;i<N;i++)cout<<T[i];
- cout<<endl;
- KR(h);
- cout<<endl;
- naiwny();
- cout<<endl;
- BM();
- /*
- for(int i =0; i < N;i++)
- {
- char *s = init();
- int temp=hash1(s);
- dodaj(s,temp,0);
- }
- drukuj();
- */
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement