Advertisement
Mirbek

Detection of secret messages

Jun 4th, 2022
1,333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.60 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int N = 1e6 + 5;
  6. const int M = 1002;
  7. const int prime1 = 13;
  8. const int mod1 = 1e9 + 7;
  9.  
  10. int n;
  11. char text[N];
  12. char s[M];
  13.  
  14. int binpow(int a, int n, int mod) {
  15.     int res = 1;
  16.     while (n) {
  17.         if (n & 1)
  18.             res = (1ll * res * a) % mod;
  19.         a = (1ll * a * a) % mod;
  20.         n >>= 1;
  21.     }
  22.     return res;
  23. }
  24.  
  25. int main() {
  26.     gets(text);
  27.  
  28.     int len = strlen(text);
  29.  
  30.     int n, l;
  31.     scanf("%d%d", &n, &l);
  32.  
  33.     vector <int> vec;
  34.     int hash1 = 0;
  35.     for (int i = 0; i < l; i++) {
  36.         hash1 = (1ll * text[i] * binpow(prime1, i + 1, mod1) + hash1) % mod1;
  37.     }
  38.     vec.push_back(hash1);
  39.  
  40.     for (int i = l; i <= len; i++) {
  41.         hash1 = (1ll * hash1 * binpow(prime1, mod1 - 2, mod1)) % mod1;
  42.  
  43.         hash1 -= text[i - l];
  44.  
  45.         if (hash1 < 0) hash1 += mod1;
  46.  
  47.         hash1 = (1ll * text[i] * binpow(prime1, l, mod1) + hash1) % mod1;
  48.         vec.push_back(hash1);
  49.     }
  50.  
  51.     sort(vec.begin(), vec.end());
  52.  
  53.     for (int i = 0; i < n; i++) {
  54.         getchar();
  55.         int hs1 = 0;
  56.         for (int j = 0; j < l; j++) {
  57.             scanf("%c", &s[j]);
  58.             hs1 = (1ll * s[j] * binpow(prime1, j + 1, mod1) + hs1) % mod1;
  59.         }
  60.  
  61.         int lo = 0, hi = (int)vec.size() - 1;
  62.  
  63.         while (lo <= hi) {
  64.             int md = (lo + hi) >> 1;
  65.             if (vec[md] < hs1)
  66.                 lo = md + 1;
  67.             else
  68.                 hi = md - 1;
  69.         }
  70.  
  71.         if (vec[lo] == hs1) {
  72.             puts("Yes");
  73.         } else {
  74.             puts("No");
  75.         }
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement