-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBCACM11F.cpp
More file actions
73 lines (70 loc) · 1.32 KB
/
Copy pathBCACM11F.cpp
File metadata and controls
73 lines (70 loc) · 1.32 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include<bits/stdc++.h>
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define alphaa "abcdefghijklmnopqrstuvwxyz"
#define ALPHAA "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#define faster() ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
using namespace std;
typedef double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int,int> II;
const ld pi=2*acos(0);
const ll inf = LLONG_MAX;
const ll ninf = LLONG_MIN;
const int oo = INT_MAX;
const int noo = INT_MIN;
int T, t, n, st[2000];
string s;
void init(){
cin >> s;
}
int tinh(char c){
return min(c - 'A','Z' - c + 1);
}
int ketQua(string s){
n = s.length();
s = "s" + s;
int res = 0;
for (int i = 1; i <= n; i++) res += tinh(s[i]);
t = 0;
for (int i = 1; i <= n; i++)
if (s[i] != 'A'){
t++;
st[t] = i;
}
if (t == 0) return 0;
if (t == 1) return res + min(st[t] - 1, n - st[t] + 1);
int d = min(st[t] - 1, n - st[1] + 1);
for (int i = 1; i <= t - 1; i++) d = min(d, 2 * (st[i] - 1) + n - st[i + 1] + 1);
for (int i = 2; i <= t; i++) d = min(d, 2 * (n - st[i] + 1) + st[i - 1] - 1);
return res + d;
}
void process(){
cout << ketQua(s) << endl;
}
int main(){
faster();
cin >> T;
while (T--){
init();
process();
}
return 0;
}