Advertisement
majczel23000

AISD zajecia 12.01.2018

Jan 12th, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <string.h>
  5. #include <math.h>
  6. using namespace std;
  7.  
  8. const int N = 100; //długość tesktu
  9. const int M = 3 ; //długość wzorca
  10. int T[N];
  11. int P[M];
  12.  
  13. int hash(int x[])
  14. {
  15. int h = 0;
  16. for(int i = 0, j = M-1; i < M; i++, j--)
  17. h += x[i] * pow(2,j);
  18. return h;
  19. }
  20.  
  21. void init()
  22. {
  23. for(int i=0;i<N;i++)
  24. T[i] = rand()%3;
  25. }
  26.  
  27. void search()
  28. {
  29. for(int i=0;i<N;i++)
  30. cout<<T[i];
  31. cout<<endl;
  32. cout<<endl;
  33. cout<<"Podaj wzorzec: ";
  34. for(int i = 0; i < M; i++)
  35. cin>>P[i];
  36.  
  37. for(int i = 0; i < N-M+1; i++)
  38. for(int j = 0; P[j]==T[i+j]; j++)
  39. if(j == M-1)
  40. cout<<i<<endl;
  41. }
  42.  
  43. void KRsearch()
  44. {
  45.  
  46. for(int i=0;i<N;i++)
  47. cout<<T[i];
  48. cout<<endl;
  49. cout<<endl;
  50. cout<<"Podaj wzorzec: ";
  51. for(int i = 0; i < M; i++)
  52. cin>>P[i];
  53.  
  54. int hT = hash(T);
  55. int hP = hash(P);
  56.  
  57. int i = 0;
  58. while(i <= N-M+1)
  59. {
  60. if(hT == hP)
  61. {
  62. for(int j = 0; P[j]==T[i+j]; j++)
  63. if(j == M-1)
  64. cout<<i<<endl;
  65. }
  66. i++;
  67. hT = (hT - pow(2,M-1)*T[i-1]) * 2 + T[i+M];
  68. }
  69. }
  70.  
  71. void BMsearch(){} //zadanie 3
  72.  
  73. int main(int argc, char *argv[]){
  74.  
  75. srand(time(NULL));
  76. init();
  77. //search();
  78. KRsearch();
  79. BMsearch();
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement