-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCF_1385D.cpp
More file actions
43 lines (36 loc) · 847 Bytes
/
CF_1385D.cpp
File metadata and controls
43 lines (36 loc) · 847 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
#include <iostream>
#include <string>
#include <algorithm>
#include <math.h>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <functional>
#include <cstdlib>
#include <list>
#include <iomanip>
#define ll long long
using namespace std;
int calc(const string& s, char c) {
if (s.size() == 1) {
return s[0] == c ? 0:1;
}
int mid = s.size() / 2;
int cntLeft = calc(string(s.begin(), s.begin() + mid), c + 1)
+ s.size() / 2 - count(s.begin() + mid, s.end(), c);
int cntRight = calc(string(s.begin() + mid, s.end()), c + 1)
+ s.size() / 2 - count(s.begin(), s.begin() + mid, c);
return min(cntLeft, cntRight);
}
int main() {
int T;
cin >> T;
for (int t = 0; t < T; t++) {
int n;
string s;
cin >> n >> s;
cout << calc(s, 'a') << endl;
}
}