forked from b-chae/AlgorithmStudy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathround656D.cpp
More file actions
51 lines (43 loc) · 954 Bytes
/
Copy pathround656D.cpp
File metadata and controls
51 lines (43 loc) · 954 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
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int T, N;
string str;
vector<int> ans;
int min(int a, int b) {
if (a <= b) return a;
return b;
}
int solve(int left, int right, char currentChar, int currentNum) {
if (currentNum == 1) {
if (str[left] == currentChar) return 0;
else return 1;
}
int middle = (left + right) / 2;
int count1 = 0, count2 = 0;
int i;
for (i = left; i <= middle; i++) {
if (currentChar != str[i]) count1++;
}
for (i = middle + 1; i <= right; i++) {
if (currentChar != str[i])count2++;
}
int res1 = solve(middle + 1, right, currentChar + 1, currentNum / 2) + count1;
int res2 = solve(left, middle, currentChar + 1, currentNum / 2) + count2;
return min(res1, res2);
}
int main()
{
cin >> T;
int i;
for (i = 0; i < T; i++) {
cin >> N >> str;
int a = solve(0, N - 1, 'a', N);
ans.push_back(a);
}
for (i = 0; i < T; i++) {
cout << ans[i] << "\n";
}
return 0;
}