#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5+10;
int n;
char s[maxn];
// getans 代表将[l,r] 变为一个 c-好串 所需最小改动
int getans(int l,int r,char c){
if(l==r) return s[l]!=c; // 如果该串已经符合要求,则不需要改动,返回 0
int tot1 = 0,tot2 = 0;
int mid = (l+r)>>1;
for(int i = l;i <= mid;i++) if(s[i]!=c) tot1++;
for(int i = mid+1;i <= r;i++) if(s[i]!=c) tot2++;
tot1 += getans(mid+1,r,c+1);
tot2 += getans(l,mid,c+1);
return min(tot1,tot2);
// tot1 代表将左区间全变为 c, 右区间变为一个c+1 好串所需最小改动
// tot2 代表将右区间全变为 c, 左区间变为一个c+1 好串所需最小改动
}
int main () {
int _;
scanf("%d", &_);
while (_--) {
scanf("%d", &n);
scanf("%s", s + 1);
printf("%d\n", getans(1, n, 'a'));
}
return 0;
}
a-Good String
猜你喜欢
转载自blog.csdn.net/weixin_43601103/article/details/112486345
今日推荐
周排行