1385D - a-Good String - CodeForces Solution


bitmasks brute force divide and conquer dp implementation *1500

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define omar ios::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
#define ln "\n"
ll n;
string s;
vector<vector<ll>>v(2e5+4,vector<ll>(26));
ll sol (int i=1, int m=n ,char x='a')
{
    if(i==m)
        return s[i]!=x;
    int ind1=(i+m)/2+1,ind2=(i+m)/2;
    ll fir = sol(ind1,m,x+1)+(m-i+1)/2-(v[ind1-1][x-'a']-v[i-1][x-'a']);
    ll sec = sol(i,ind2,x+1)+(m-i+1)/2-(v[m][x-'a']-v[ind2][x-'a']);
    return min(fir,sec);
}
int main() {
    omar;
    ll t=1;
    cin >> t;
    while (t--) {
        cin >> n >> s;
        s='#'+s;
        for(int i=1;i<=n;i++)
        {
            v[i]=v[i-1];
            v[i][s[i]-'a']++;
        }
        cout << sol() << ln;
    }

}

	  		  				  	 		 	     	    	


Comments

Submit
0 Comments
More Questions

1635B - Avoid Local Maximums
20A - BerOS file system
1637A - Sorting Parts
509A - Maximum in Table
1647C - Madoka and Childish Pranks
689B - Mike and Shortcuts
379B - New Year Present
1498A - GCD Sum
1277C - As Simple as One and Two
1301A - Three Strings
460A - Vasya and Socks
1624C - Division by Two and Permutation
1288A - Deadline
1617A - Forbidden Subsequence
914A - Perfect Squares
873D - Merge Sort
1251A - Broken Keyboard
463B - Caisa and Pylons
584A - Olesya and Rodion
799A - Carrot Cakes
1569B - Chess Tournament
1047B - Cover Points
1381B - Unmerge
1256A - Payment Without Change
908B - New Year and Buggy Bot
979A - Pizza Pizza Pizza
731A - Night at the Museum
742A - Arpa’s hard exam and Mehrdad’s naive cheat
1492A - Three swimmers
1360E - Polygon