-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathD.cpp
More file actions
52 lines (42 loc) · 978 Bytes
/
Copy pathD.cpp
File metadata and controls
52 lines (42 loc) · 978 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <bits/stdc++.h>
using namespace std;
#ifndef LOCAL
#define cerr if(0) cerr
#endif
#define all(v) (v).begin(), (v).end()
#define X first
#define Y second
using ll = long long;
using ld = long double;
template<class T = int>
using V = vector<T>;
template<class T = int>
using VV = V<V<T>>;
template<class I, class J>
using P = pair<I, J>;
template<class T = int>
using PP = P<T, T>;
int n;
string s;
inline int solve(int l, int r, char c) {
if (r - l == 0) return 0;
if (r - l == 1) return (s[l] != c);
int m = (1LL * l + 1LL * r) >> 1LL;
int side1 = count(s.begin() + l, s.begin() + m, c);
int side2 = count(s.begin() + m, s.begin() + r, c);
return min(m - l - side1 + solve(m, r, c + 1), r - m - side2 + solve(l, m, c + 1));
}
inline void run_case() {
cin >> n >> s;
cout << solve(0, n, 'a') << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int tc;
cin >> tc;
while (tc--) {
run_case();
}
return 0;
}