Q - Vasya and String
Course: Binary search, two sum and strings - chapter 8
Link: https://codeforces.com/problemset/problem/676/C
Solution
Section titled “Solution”We need to find the maximum length substring containing all equal characters, which is also known as the beauty of the string. Let’s call it beauty number
Also, Vasya cannot change more than K characters of the string.
Okay, so let’s assume the beauty number is X. We want to determine what is the minimum number of changes needed to make the string have a beauty number of X.
Let’s say G(X) returns this thing.
So F(X) = G(X) ≤ K as we cannot change more than K characters.
Now is F(X) monotonic. As X increases, i.e. we need a longer substring to have all characters equal, which means more changes.
Similarly, as X decreases, we need to make all characters of a smaller substring equal, which means less changes.
Thus, F(X) is monotonic, so we can binary search on X i.e. the beauty number or the length of substring containing all equal characters.
Now how to calculate G(X)?
We can use a sliding window algorithm. The substring length is fixed, so we can use sliding window on all possible substrings and determine the minimum changes needed for one of them to be all equal. To calculate for a window, we maintain a frequency array of size 26, which gives the frequency of all characters in the current window. To make all of them equal, it is same as determining the one with maximum frequency and changing all the others to this i.e. (X - frequency of char with max frequency).
To slide the window, freq[(int)(s[i] - 'a')] ++ and freq[(int)(s[i] - 'a')] --
Code for G(x)
int calcChanges(vector<int> &freq, int x) { int mx = freq[0]; for(int i=1; i<26; i++) mx = max(mx, freq[i]); return (x-mx);}
int minChanges(string &s, int x) { vector<int> freq(26, 0);
// Calculate frequencies for the first window manually for(int i=0; i<x; i++) freq[(int)(s[i]-'a')]++;
int changes = calcChanges(freq, x);
// Sliding window start for(int i=x; i<n; i++) { // Remove i-x as it goes out of window freq[(int)(s[i-x] - 'a')]--; // Add ith element as it gets added new in window freq[(int)(s[i] - 'a])]++; changes = min(changes, calcChanges(freq, x)); }
return changes;}Now once G(x) is calculated, we can just write F(x) = G(x) ≤ k. And then do a binary search for X. The answer will be the last occurrence of True of F(x).
#include<bits/stdc++.h>
using namespace std;
int calcChanges(vector<int> &freq, int x) { int mx = freq[0]; for(int i=1; i<26; i++) mx = max(mx, freq[i]); return (x-mx);}
int minChanges(string &s, int x) { vector<int> freq(26, 0);
// Calculate frequencies for the first window manually for(int i=0; i<x; i++) freq[(int)(s[i]-'a')]++;
int changes = calcChanges(freq, x);
// Sliding window start for(int i=x; i<s.size(); i++) { freq[(int)(s[i-x] - 'a')]--; freq[(int)(s[i] - 'a')]++;
changes = min(changes, calcChanges(freq, x)); }
return changes;}
int f(string &s, int x, int k) { return minChanges(s, x) <= k;}
int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int n, k; cin>>n>>k; string s; cin>>s;
int lo = 0, hi = n+1;
while(lo < hi-1) { int mid = (lo + hi)/2; if (f(s, mid, k)) { lo = mid; } else hi = mid; }
cout<<lo<<"\n";
return 0;}