Iamtui1010

dtksub

Nov 7th, 2021 (edited)
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.63 KB | None | 0 0
  1. //#include<bits/stdc++.h>
  2. #include<iostream>
  3. #include<fstream>
  4. #include<vector>
  5. #include<string>
  6. #include<map>
  7.  
  8. #define long long long
  9. #define nln '\n'
  10.  
  11. const long I = 1e9, BASE = 311, MOD = 1e9+3;
  12.  
  13. using namespace std;
  14.  
  15. // GLobal variables: f1, f2, str
  16.  
  17. fstream f1,f2;
  18.  
  19. inline void openf()
  20. {
  21.     f1.open("dtksub.inp", ios:: in);
  22.     f2.open("dtksub.out", ios:: out);
  23. }
  24.  
  25. inline void closef()
  26. {
  27.     f1.close();
  28.     f2.close();
  29. }
  30.  
  31. long n, k;
  32. string str;
  33.  
  34. void data()
  35. {
  36.     f1.tie(0)->sync_with_stdio(0);
  37.     f2.tie(0)->sync_with_stdio(0);
  38.     cin >> n >> k;
  39.     cin >> str;
  40. }
  41.  
  42. vector<long> poh, has;
  43.  
  44. void inithash()
  45. {
  46.     // Xay dung BASE mu i
  47.     poh.resize(n, 0);
  48.     poh[0] = 1;
  49.     for (long i = 1; i != n; ++i)
  50.         poh[i] = poh[i-1]*BASE % MOD;
  51.    
  52.     // Xay dung hash 1 -> i;
  53.     has.resize(n, 0);
  54.     has[0] = str[0] - 'a' + 1;
  55.     for (long i = 1; i != n; ++i)
  56.         has[i] = (has[i-1]*BASE + str[i] - 'a' + 1) % MOD;
  57. }
  58.  
  59. long hashing(long i, long j)
  60. {
  61.     if (i == 0)
  62.         return has[j];
  63.     return ((has[j] - has[i-1]*poh[j-i+1]%MOD + MOD) % MOD);
  64. }
  65.  
  66.  
  67.  
  68. bool enough(long len)
  69. {
  70.     map<long, long> sav;
  71.     for (long i = 0; i+len-1 != n; ++i)
  72.     {
  73.         long x = hashing(i, i+len-1);
  74.         ++sav[x];
  75.         //cin.get();
  76.         if (sav[x] == k) return 1;
  77.     }
  78.     return 0;
  79. }
  80.  
  81. long ans = -1;
  82.  
  83. void process()
  84. {
  85.     int lef = 1, rig = n;
  86.     while (lef <= rig)
  87.     {
  88.         int len = (lef + rig) / 2;
  89.         if (enough(len))
  90.         {
  91.             ans = len;
  92.             lef = len + 1;
  93.         }
  94.         else
  95.             rig = len - 1;
  96.     }
  97. }
  98.  
  99. void view()
  100. {
  101.     cout << ans << nln;
  102. }
  103.  
  104. int main()
  105. {
  106.     openf();
  107.     data();
  108.     inithash();
  109.     process();
  110.     view();
  111.     closef();
  112.     return 0;
  113. }
  114.  
Add Comment
Please, Sign In to add comment