Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<map>
- using namespace std;
- typedef long long int ll;
- ll const Pr = 98765431;
- ll mpow(ll a,ll b){
- ll ta = a;
- if(b == 0)return 1;
- for(int i = 1 ; i < b ; i ++){
- ta *= a;
- }
- return ta;
- }
- int main()
- {
- int n,m;
- scanf("%d %d",&n,&m);
- char str[n+1];
- scanf("%s",str);
- int L = 1 , R = n;
- int ans = 0;
- while(L <= R){
- int size = (L+R)/2;
- map<ll,int> table;table.clear();
- ll mp = mpow(Pr,size-1);
- ll hs = 0;
- ll maxRe = 0;
- // printf("Test size : %d ",size);
- for(int i = 0 ; i < n && maxRe < m ; i ++){
- hs *= Pr;
- hs += str[i];
- if(i >= size - 1){
- if(table.count(hs)){ //Repeat
- table[hs]++;
- }
- else{ //new
- table.insert({hs,1});
- }
- if(table[hs] > maxRe)maxRe = table[hs];
- hs -= mp*str[i-size+1];
- }
- }
- // printf(", maxRe = %d\n",maxRe);
- if(maxRe >= m){
- ans = size;
- L = size + 1;
- }
- else{
- R = size - 1;
- }
- }
- printf("%d",ans);
- return 0;
- }
Add Comment
Please, Sign In to add comment