Advertisement
a53

String Streak

a53
Dec 5th, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4. const int LGMAX = 1e5;
  5. char s[LGMAX + 5];
  6. int N;
  7. long long C, p2[35];
  8. int distToNextC[LGMAX + 5];
  9.  
  10. int Solve(char ch)
  11. {
  12. distToNextC[N + 1] = 32;
  13.  
  14. for(int i = N; i >= 1; i--)
  15. if(s[i] != ch) distToNextC[i] = 1 + distToNextC[i + 1];
  16. else distToNextC[i] = 0;
  17.  
  18. int ret = 0, l = 1, currDif = 0;
  19. long long currCost = 0LL;
  20.  
  21. for(int r = 1; r <= N; r++)
  22. {
  23. if(s[r] == ch)
  24. {
  25. currDif = 0;
  26. ret = max(ret, r - l + 1);
  27. continue;
  28. }
  29.  
  30. currDif++;
  31. if(r - l > currDif)
  32. {
  33. currCost -= p2[currDif - 1];
  34. currCost += p2[currDif];
  35. }
  36. else
  37. {
  38. currCost -= p2[r - l];
  39. currCost += p2[r - l + 1];
  40. }
  41. while(currCost > C)
  42. {
  43. int mnDist = min(distToNextC[l], r - l + 1);
  44. l++;
  45.  
  46. if(mnDist == 0)
  47. continue;
  48. if(mnDist > 30)
  49. continue;
  50. else
  51. {
  52. currCost -= p2[mnDist];
  53. mnDist--;
  54. currCost += p2[mnDist];
  55. }
  56. }
  57. ret = max(ret, r - l + 1);
  58. }
  59. return ret;
  60. }
  61.  
  62. int main()
  63. {
  64. ios_base::sync_with_stdio(false);
  65. cin.tie(0);
  66. cout.tie(0);
  67. p2[1] = 2;
  68. for(int i = 2; i <= 32; i++)
  69. p2[i] = 2LL * p2[i - 1];
  70. cin >> (s + 1);
  71. N = strlen(s + 1);
  72. cin.get();
  73. cin >> C;
  74. int sol = 1;
  75. for(int i = 1; i <= 32; i++)
  76. if(p2[i] <= C) sol = i;
  77. for(char ch = 'a'; ch <= 'z'; ch++)
  78. sol = max(sol, Solve(ch));
  79. cout << sol << '\n';
  80. return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement