点击查看代码
#include<bits/stdc++.h>
using namespace std;
char s[131080];
int _solve(int L,int R,char x)
{
if(L==R) return s[L]!=x;
int M=L+(R-L)/2;
int t1=0,t2=0;
for(int i=L;i<=M;++i) if(s[i]!=x) t1++;
for(int i=M+1;i<=R;++i) if(s[i]!=x) t2++;
return min(t1+_solve(M+1,R,x+1),t2+_solve(L,M,x+1));
return 0;
}
void solve()
{
int n;
scanf("%d",&n);
scanf("%s",s+1);
cout<<_solve(1,n,'a')<<'\n';
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
solve();
}
}