Advertisement
mihaimarcel21

StringStreak

Dec 11th, 2020 (edited)
626
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.98 KB | None | 0 0
  1. //SO
  2. #include<bits/stdc++.h>
  3. #pragma GCC optimize("O3")
  4. #define fi first
  5. #define se second
  6. #define pb push_back
  7. #define pf push_front
  8.  
  9. // #define fisier 1
  10.  
  11. using namespace std;
  12.  
  13. typedef long long ll;
  14.  
  15. const int mod = 1000000007;
  16. const double eps = 1e-9;
  17.  
  18. string s;
  19. int n, L;
  20.  
  21. int nxt[100002][27];
  22.  
  23. int frq[32];
  24. ll cost;
  25.  
  26. int ans;
  27. int main()
  28. {
  29.     #ifdef fisier
  30.         ifstream cin("input.in");
  31.         ofstream cout("output.out");
  32.     #endif
  33.  
  34.     ios_base::sync_with_stdio(false);
  35.     cin.tie(NULL);
  36.  
  37.     cin >> s;
  38.     L = s.size();
  39.     s = ' ' + s;
  40.     for(char lit = 'a'; lit <= 'z'; ++lit)
  41.     {
  42.         nxt[L+1][lit - 'a'] = L+1;
  43.         for(int i = L; i >= 1; --i)
  44.         {
  45.             if(s[i] == lit)
  46.                 nxt[i][lit - 'a'] = i;
  47.             else
  48.                 nxt[i][lit - 'a'] = nxt[i+1][lit - 'a'];
  49.         }
  50.     }
  51.     cin >> n;
  52.     for(char lit = 'a'; lit <= 'z'; ++lit)
  53.     {
  54.         memset(frq, 0, sizeof(frq));
  55.         cost = 0;
  56.         int Rp = 1;
  57.         int str = 0;
  58.         for(int i = 1; i <= L; ++i)
  59.         {
  60.             str = min(str, Rp - i);
  61.             while(Rp <= L && cost <= n)
  62.             {
  63.                 if(s[Rp] == lit)
  64.                 {
  65.                     str = 0;
  66.                 }
  67.                 else
  68.                 {
  69.                     ++str;
  70.                     frq[str]++;
  71.                     if(str == 1)
  72.                         cost += 1;
  73.                     cost += (1LL<<(str-1));
  74.                 }
  75.                 ++Rp;
  76.                 if(cost <= n)
  77.                     ans = max(ans, Rp - i);
  78.             }
  79.             if(s[i] != lit)
  80.             {
  81.                 int nxtp = min(Rp - i, nxt[i][lit - 'a'] - i);
  82.                 cost -= (1LL<<(nxtp-1));
  83.                 if (nxtp == 1)
  84.                     cost--;
  85.                 frq[nxtp]--;
  86.             }
  87.             if(cost <= n)
  88.                 ans = max(ans, Rp - i - 1);
  89.         }
  90.     }
  91.     cout << ans;
  92.     return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement