题目链接
比赛的时候闭包传递出问题了,赛后找了一些ac代码,发现了问题
AC代码:
/*
* @Author: hesorchen
* @Date: 2020-07-03 17:05:01
* @LastEditTime: 2020-07-25 13:08:03
* @Description: https://hesorchen.github.io/
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define endl '\n'
#define PI acos(-1)
#define PB push_back
#define ll long long
#define INF 0x3f3f3f3f
#define mod 1000000007
#define pll pair
#define lowbit(abcd) (abcd & (-abcd))
#define max(a, b) ((a > b) ? (a) : (b))
#define min(a, b) ((a < b) ? (a) : (b))
#define IOS \
ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define FRE \
{ \
freopen("in.txt", "r", stdin); \
freopen("out.txt", "w", stdout); \
}
inline ll read()
{
ll x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9')
{
if (ch == '-')
f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
{
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
return x * f;
}
//head==============================================================================
string a, b;
int mp[20][20];
int main()
{
IOS;
int t;
cin >> t;
while (t--)
{
fill(mp[0], mp[0] + 20 * 20, 0);
int n, flag = 1;
cin >> n >> a >> b;
for (int i = 0; i < n; i++)
if (a[i] > b[i])
{
flag = 0;
break;
}
if (!flag)
{
cout << "-1\n";
continue;
}
for (int i = 0; i < n; i++)
if (a[i] != b[i])
mp[a[i] - 'a'][b[i] - 'a'] = 1;
int ans = 0;
for (int i = 0; i < 20; i++)
for (int j = i + 1; j < 20; j++)
if (mp[i][j])
{
ans++;
for (int k = j + 1; k < 20; k++)
mp[j][k] |= mp[i][k];
break;
}
cout << ans << endl;
}
return 0;
}
/*
*/