Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#include<bits/stdc++.h>
- #include<iostream>
- #include<fstream>
- #include<vector>
- #include<string>
- #include<map>
- #define long long long
- #define nln '\n'
- const long I = 1e9, BASE = 311, MOD = 1e9+3;
- using namespace std;
- // GLobal variables: f1, f2, str
- fstream f1,f2;
- inline void openf()
- {
- f1.open("dtksub.inp", ios:: in);
- f2.open("dtksub.out", ios:: out);
- }
- inline void closef()
- {
- f1.close();
- f2.close();
- }
- long n, k;
- string str;
- void data()
- {
- f1.tie(0)->sync_with_stdio(0);
- f2.tie(0)->sync_with_stdio(0);
- cin >> n >> k;
- cin >> str;
- }
- vector<long> poh, has;
- void inithash()
- {
- // Xay dung BASE mu i
- poh.resize(n, 0);
- poh[0] = 1;
- for (long i = 1; i != n; ++i)
- poh[i] = poh[i-1]*BASE % MOD;
- // Xay dung hash 1 -> i;
- has.resize(n, 0);
- has[0] = str[0] - 'a' + 1;
- for (long i = 1; i != n; ++i)
- has[i] = (has[i-1]*BASE + str[i] - 'a' + 1) % MOD;
- }
- long hashing(long i, long j)
- {
- if (i == 0)
- return has[j];
- return ((has[j] - has[i-1]*poh[j-i+1]%MOD + MOD) % MOD);
- }
- bool enough(long len)
- {
- map<long, long> sav;
- for (long i = 0; i+len-1 != n; ++i)
- {
- long x = hashing(i, i+len-1);
- ++sav[x];
- //cin.get();
- if (sav[x] == k) return 1;
- }
- return 0;
- }
- long ans = -1;
- void process()
- {
- int lef = 1, rig = n;
- while (lef <= rig)
- {
- int len = (lef + rig) / 2;
- if (enough(len))
- {
- ans = len;
- lef = len + 1;
- }
- else
- rig = len - 1;
- }
- }
- void view()
- {
- cout << ans << nln;
- }
- int main()
- {
- openf();
- data();
- inithash();
- process();
- view();
- closef();
- return 0;
- }
Add Comment
Please, Sign In to add comment