-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC.cpp
83 lines (73 loc) · 2.36 KB
/
C.cpp
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
74
75
76
77
78
79
80
81
82
83
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define ff first
#define ss second
#define pob pop_back
#define read(x) long long x; cin >> x;
#define reads(s) string s; cin >> s;
#define rep(i,a,b) for(ll i=a; i<b; i++)
#define repr(i,a,b) for(ll i=a; i>=b; i--)
#define repj(i,a,b,c) for(ll i=a; i<b; i+=c)
#define input(v,n) vector<ll> v(n); rep(i,0,n) cin >> v[i];
#define all(v) v.begin(),v.end()
#define allr(v) v.begin(),v.end(),greater<ll>()
#define deb(x) cout<< #x <<" "; _print(x); cout <<"\n"
#define fastIO ios_base::sync_with_stdio(false);cin.tie(NULL)
void _print(ll t) {cout << t;}
void _print(int t) {cout << t;}
void _print(string t) {cout << t;}
void _print(char t) {cout << t;}
void _print(double t) {cout << t;}
template <class T, class V> void _print(pair <T, V> p) {cout << "{"; _print(p.ff); cout << ","; _print(p.ss); cout << "}";}
template <class T> void _print(vector <T> v) {cout << "[ "; for (T i : v) {_print(i); cout << " ";} cout << "]";}
template <class T> void _print(set <T> v) {cout << "[ "; for (T i : v) {_print(i); cout << " ";} cout << "]";}
template <class T> void _print(unordered_set <T> v) {cout << "[ "; for (T i : v) {_print(i); cout << " ";} cout << "]";}
template <class T> void _print(multiset <T> v) {cout << "[ "; for (T i : v) {_print(i); cout << " ";} cout << "]";}
template <class T, class V> void _print(map <T, V> v) {cout << "[ "; for (auto i : v) {_print(i); cout << " ";} cout << "]";}
template <class T, class V> void _print(unordered_map <T, V> v) {cout << "[ "; for (auto i : v) {_print(i); cout << " ";} cout << "]";}
const int N = 1e9 +7;
void solve(){
read(n);
reads(s);
vector<ll> v(n);
v[n - 1] = n - 1;
for(ll i = n - 2; i >= 0; i--) {
if(s[i] >= s[v[i + 1]]) v[i] = i;
else v[i] = v[i + 1];
}
string largest = "";
rep(i, 0, n) {
if(v[i] == i) largest.pb(s[i]);
}
// deb(largest);
string cpy = largest;
sort(cpy.begin(), cpy.end());
ll j = 0;
rep(i, 0, n) {
if(v[i] == i) s[i] = cpy[j++];
}
if(is_sorted(s.begin(), s.end())) {
if(is_sorted(largest.begin(), largest.end())) {
cout << 0 << "\n";
}
else {
ll k = 0;
while(k < largest.size() - 1) {
if(largest[k] == largest[k + 1]) k++;
else break;
}
cout << largest.size() - k - 1<< "\n";
}
}
else cout << -1 << "\n";
}
int main(){
fastIO;
ll t = 1;
cin >> t;
while(t--){
solve();
}
}