mamamaria

Ass3 for calm

Feb 13th, 2021 (edited)
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. using namespace std;
  4. //На вход программы случайная строка, состоящая из n бит.Написать программу, вычисляющую оценку вероятности того,
  5. //что в этой строке есть точно две одинаковых непересекающихся подстроки длины m;
  6.  
  7. const int n = 15; const int m = 4;
  8.  
  9. void genStr(int mas[], int l) {
  10.     for (int i = 0; i < l; i++)
  11.         mas[i] = rand() % 2;
  12. }
  13. void output(int str[], int l) {
  14.     for (int i = 0; i < l; i++) {
  15.         cout << str[i];
  16.     }
  17. }
  18. int main()
  19. {
  20.     srand(time(NULL));
  21.  
  22.     /*int str[] = { 0,1,1,1,0,1,0,1,0,0,1,1,0,0,1 };
  23.     int subStr[] = { 1,0,1,0 };*/
  24.  
  25.     //int str[] = { 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0};
  26.     //int subStr[] = { 1,0,1,0 };
  27.  
  28.    
  29.     bool check = true; int counter = 0, gcounter = 0; double GK = 0; int N = 1000000;
  30.    
  31.     //output(str, n); cout << endl; output(subStr, m);
  32.     int str[n]; int subStr[m];
  33.     for (int k = 0; k < N; k++) {
  34.        
  35.         for (int s = 0; s < n; s++)
  36.             str[s] = rand() % 2; //call rand; and eax, 1 результат будет в eax
  37.         for (int t = 0; t < m; t++)
  38.             subStr[t] = rand() % 2;
  39.         for (int i = 0; i < n - m; ) {
  40.             for (int j = 0; j < m; j++) {
  41.                 if (subStr[j] == str[j + i]) {
  42.  
  43.                     check = true;
  44.                     counter++;
  45.                 }
  46.                 else check = false;
  47.                 if (check == false || counter == m)
  48.                 {
  49.                     if (counter == m) {
  50.                         gcounter++;
  51.                         i += m;
  52.                     }
  53.  
  54.                     counter = 0;
  55.                     break;
  56.                 }
  57.  
  58.             }
  59.             i++;
  60.         }
  61.         if (gcounter == 2)
  62.             GK += 1;
  63.         gcounter = 0;
  64.         counter = 0;
  65.     }
  66.  
  67.     cout << GK/N;
  68.     cout << endl;
  69.  
  70. }
Add Comment
Please, Sign In to add comment