### FameOfTheLegends's blog

By FameOfTheLegends, 12 days ago,

Question 1 — N boxes are Arrranged in a line. Each box is colored either with white or black. You can select any number of boxes from the starting and put it behind the last box in the same sequence as it was placed initially. you can perform this operation atmost once. For example string s="wbbb" . In one operation you can select "wb" and from the start and put it behind the last box "b".

Note: you cannot select all the n boxes.

You are required to find the minimum number of boxes whose colors must be reversed so that all the boxes in the sequence are of an alternate color, that is any two adjacent boxes are of different colors.

Input :  5
wwwwwbbww
wwwb
bwww
wwww
bbbbb

Output :
3
1
1
2
2


I was thinking of brute forcing the solution, that is doing operation for each length and then calculating for that configuration. Can anyobody tell the correct approach

• +7

 » 12 days ago, # | ← Rev. 3 →   +4 Append string to itself and use sliding window of size N , and calculate each window the transition required to convert, now while moving the window you could also manage the dp states , dp will be minimum operation required to convert current window that is ending at index i and such that ith character is W or B, and before that there is alternating sequence for the range [i-N+1,i].
 » 12 days ago, # |   +6 Let pref[i][0] show the minimum number of flips to make the characters [0, i) alternating while the first character itself being W. pref[i][1] shows the same thing but the first character needs to be a B there. Do this calculation for all suffix too, where suff[i][0] shows the characters [i, n) alternating while the last one is W and suff[i][1] for the last one being B. Those calculations are O(n). Then the answer is minimum of min(pref[i][0] + suff[i+1][0] + 1, pref[i][1] + suff[i+1][0], pref[i][0] + suff[i+1][1], pref[i][1] + suff[i+1][1] + 1) for all i. This is also O(n).
•  » » 12 days ago, # ^ | ← Rev. 2 →   0 I want to ask you 2 things:- pref[i] contains [0,i) while suff[i] contains [i,n) and in the end you're checking for pref[i]+suff[i+1], which means you're not considering ith character at all. When we're appending an alternating prefix which starts with 'W' to an alternating suffix which ends with 'W' (for ex ...BWBWBW + WBWBWB...) then I believe the characters of at least one of the string should be flipped (i.e....BWBWBW + BWBWBW... or ...WBWBWB + WBWBWB...) which may cost more than 1 depending on the length and values of pref[i] and suff[i]. Please correct me If I'm wrong or If I missed something.
•  » » » 12 days ago, # ^ |   +1 Correct, my bad. Should be pref[i]+suff[i] Now I think about it, I believe you don't need to check for +1 cases (do you? I might look into it later).
•  » » » » 11 days ago, # ^ |   0 I think we need to consider them. And the cost should depend on the relation between two numbers X and Y where X is number of operations required to form an alternating string S while Y is the number of operations required to from the counter alternating string of S (Sorry couldn't find a better word) and If I'm not wrong, the relation should be Y = len(S)-X.
 » 12 days ago, # | ← Rev. 2 →   0 Can anyone verify if my code is right? Or missing some edge cases?My idea: lets take wwwb as e.g. Moving prefix back is essentially duplicating the front part back which gives me the string wwwbwww. The target strings are wbwbwbw or bwbwbwb Using sliding widown we calculate the min of changes. Codeint solve(string& s) { vector> dp(2); dp[0].pb(0); dp[1].pb(0); int opt = 0; for (int i = 0;i<2*s.size()-1;i++){ dp[0].pb(dp[0].back()+(s[i%s.size()]!=((opt)?'w':'b'))); dp[1].pb(dp[1].back()+(s[i%s.size()]!=((opt)?'b':'w'))); opt = 1 - opt; } int ans = INT_MAX; int n = s.size(); for (int i = n;i>n; while(n--){ string s; cin >> s; cout << solve(s) << endl; } return 0; } 
 » 11 days ago, # | ← Rev. 6 →   0 it's very easy 1. pre-compute suffix array cost making wbwbwb and bwbwbw. iterate 1 to n, also simultanously compute cost for prefix wbwbw and bwbwbw. -> ans = min(ans ,prefix cost of starting with w and prefix end with b ) -> ans = min(ans ,prefix cost of starting with b and prefix end with w ) codevoid solve(){ string s; cin >> s; int n = s.size(); s = '@' + s; // wb ,bw int flip = 1; vi wb(n+2 ,0) ,bw(n+2 ,0); for(int i=n ;i>=1 ;i-- ){ bw[i] += bw[i+1]; wb[i] += wb[i+1]; if(flip){ // required w if( s[i] == 'b' ) wb[i]++; else bw[i]++; }else{ if( s[i] == 'w' ) wb[i]++; else bw[i]++; } flip^=1; } int w=0 ,b=0 ,ans=INT_MAX; flip = 1; for(int i=1 ;i<=n ;i++){ ans = min(ans ,b + wb[i] ); ans = min(ans ,w + bw[i] ); if(flip){ if( s[i] == 'b' ) w++; else b++; }else{ if( s[i] == 'w' ) w++; else b++; } flip^=1; } cout << ans << endl;}
 » 11 days ago, # |   0 What we can do is brute force the solution. We can one by one remove from the i length substring from the beginning and add it to the end, not in the literal sense but we can simulate so by keeping count of the number of black and white balls present in the even and odd positions. And suppose the original string is bwwwb. (consider 1 indexing) odd => b = 2 , w = 1 even => b = 0, w = 2 If we remove substring of length 1, then the string will become wwwbb and even=> b = 1 , w = 1 odd=> b = 1, w = 2 . We can calculate them easily by keeping prefix sums of black and white respecitvely. And for each configuration, we can calculate the answer for that configuration by min(even) + min(odd)And we take minimum for all possible configutationstime complexity : O(n)