-
Notifications
You must be signed in to change notification settings - Fork 0
/
A.cpp
45 lines (45 loc) · 871 Bytes
/
A.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
#include <bits/stdc++.h>
using namespace std;
const int alpha = 20;
int g[26][26];
bool vis[26];
void dfs(int x) {
vis[x] = true;
for(int i = 0; i < alpha; i++) {
if(g[x][i] == 1 && vis[i] == false) {
dfs(i);
}
}
}
int main() {
ios_base :: sync_with_stdio (false);
cin.tie(0);
int t;
cin >> t;
while(t--) {
int n;
string a, b;
cin >> n >> a >> b;
bool bad = false;
memset(g, 0, sizeof g);
memset(vis, 0, sizeof vis);
for(int i = 0; i < n; i++) {
if(a[i] < b[i]) {
g[a[i] - 'a'][b[i] - 'a'] = 1;
g[b[i] - 'a'][a[i] - 'a'] = 1;
} else if (a[i] > b[i]) {
bad = true;
}
}
int ans = alpha;
for(int i = 0; i < alpha; i++) {
if(vis[i] == false) {
dfs(i);
ans -= 1;
}
}
if(bad) ans = -1;
cout << ans << endl;
}
return 0;
}