Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. #include <iostream>
  2. #include<cmath>
  3. #include<cstdlib>
  4. #include<ctime>
  5. using namespace std;
  6. const int N = 30; //d³ugoœæ tesktu
  7. const int M = 2 ; //d³ugoœæ wzorca
  8. const int a=7;
  9. int T[N];
  10. int P[M];
  11. void init()
  12. {
  13. for(int i=0;i<N;i++)
  14. T[i] = rand()%2;
  15.  
  16. for(int i=0;i<M;i++)
  17. P[i] = rand()%2;
  18. }
  19. int hash1(int i)
  20. {
  21. int has=pow(a,1)*T[i]+pow(a,0)*T[i+1];
  22. return has;
  23. }
  24. void KR(int h)
  25. {
  26. int i=0,temp=pow(a,1)*T[0]+pow(a,0)*T[1];
  27. while(i<=N-M)
  28. {
  29.  
  30. if(h==temp)
  31. {
  32. if(P[0]==T[i]&&P[1]==T[i+1])
  33. {
  34. cout<<T[i]<<T[i+1];
  35. cout<<" "<<i <<endl;
  36. }
  37.  
  38. }
  39. i++;
  40. temp=hash1(i);
  41. }
  42. }
  43. bool compare(int i)
  44. {
  45. if(T[i]==P[0]&& T[i+1]==P[1]) return true;
  46. else return false;
  47. }
  48. void naiwny()
  49. {
  50. int i=0;
  51. while(i<=N-M)
  52. {
  53. if(compare(i))
  54. {
  55. cout<<T[i]<<T[i+1]<<" "<<i<<endl;
  56. }
  57. i++;
  58. }
  59. }
  60. void BM()
  61. {
  62. int i=0;
  63. while(i<=N-M)
  64. {
  65. int j=M,s=0;
  66. while(P[j]==T[i+j-2])
  67. {
  68. j--;
  69. }
  70. if(j==0)
  71. {
  72. cout<<T[i]<<T[i+1]<<" "<<i<<endl;
  73. }
  74. if(j==M) s=j;
  75.  
  76.  
  77. i=i+1;
  78. }
  79.  
  80. }
  81. int main(int argc, char *argv[])
  82. {
  83. srand(time(NULL));
  84.  
  85. init();
  86.  
  87. int h=pow(a,1)*P[0]+pow(a,0)*P[1];
  88.  
  89. for(int i=0;i<M;i++) cout<<P[i];
  90.  
  91. cout<<endl;
  92.  
  93. for(int i=0;i<N;i++)cout<<T[i];
  94.  
  95. cout<<endl;
  96. KR(h);
  97.  
  98. cout<<endl;
  99. naiwny();
  100. cout<<endl;
  101. BM();
  102.  
  103. /*
  104. for(int i =0; i < N;i++)
  105. {
  106. char *s = init();
  107. int temp=hash1(s);
  108.  
  109. dodaj(s,temp,0);
  110. }
  111.  
  112. drukuj();
  113.  
  114. */
  115.  
  116.  
  117.  
  118.  
  119. return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement