-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA.cpp
More file actions
80 lines (72 loc) · 1.44 KB
/
A.cpp
File metadata and controls
80 lines (72 loc) · 1.44 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
74
75
76
77
78
79
80
#include <iostream>
#include <unordered_set>
#include <unordered_map>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
int max(int a, int b)
{
return ((a < b) ? b : a);
}
int there_is_solution(string A, string B)
{
int i;
i = -1;
while (++i < A.length())
if (A[i] > B[i])
return (0);
return (1);
}
void solve(string A, string B)
{
int i;
int min_letter;
int moves;
int c;
vector<int> map;
if (!there_is_solution(A, B))
{
cout << "-1" << endl;
return ;
}
c = 'a';
moves = 0;
while (c <= 't')
{
min_letter = 't';
i = -1;
while (++i < A.length())
{
if (A[i] != B[i] && A[i] == c)
{
map.push_back(i);
min_letter = (min_letter < B[i]) ? min_letter : B[i];
}
}
i = 0;
if (map.size())
moves++;
while (i < map.size())
A[map[i++]] = min_letter;
map.clear();
c++;
}
cout << moves << endl;
}
int main()
{
int testCases;
int string_len;
string A;
string B;
cin >> testCases;
while (testCases--)
{
cin >> string_len;
cin >> A;
cin >> B;
solve(A, B);
}
return (0);
}