Codeforces Round #659 (Div. 2) C. String Transformation 1

C. String Transformation 1
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Note that the only difference between String Transformation 1 and String Transformation 2 is in the move Koa does. In this version the letter y Koa selects must be strictly greater alphabetically than x (read statement for better understanding). You can make hacks in these problems independently.

Koa the Koala has two strings A and B of the same length n (|A|=|B|=n) consisting of the first 20 lowercase English alphabet letters (ie. from a to t).

In one move Koa:

selects some subset of positions p1,p2,…,pk (k≥1;1≤pi≤n;pi≠pj if i≠j) of A such that Ap1=Ap2=…=Apk=x (ie. all letters on this positions are equal to some letter x).
selects a letter y (from the first 20 lowercase letters in English alphabet) such that y>x (ie. letter y is strictly greater alphabetically than x).
sets each letter in positions p1,p2,…,pk to letter y. More formally: for each i (1≤i≤k) Koa sets Api=y.
Note that you can only modify letters in string A.

Koa wants to know the smallest number of moves she has to do to make strings equal to each other (A=B) or to determine that there is no way to make them equal. Help her!

Input
Each test contains multiple test cases. The first line contains t (1≤t≤10) — the number of test cases. Description of the test cases follows.

The first line of each test case contains one integer n (1≤n≤105) — the length of strings A and B.

The second line of each test case contains string A (|A|=n).

The third line of each test case contains string B (|B|=n).

Both strings consists of the first 20 lowercase English alphabet letters (ie. from a to t).

It is guaranteed that the sum of n over all test cases does not exceed 105.

Output
For each test case:

Print on a single line the smallest number of moves she has to do to make strings equal to each other (A=B) or −1 if there is no way to make them equal.

Example
inputCopy
5
3
aab
bcc
4
cabc
abcb
3
abc
tsr
4
aabd
cccd
5
abcbd
bcdda
outputCopy
2
-1
3
2
-1
Note
In the 1-st test case Koa:
selects positions 1 and 2 and sets A1=A2= b (aab→bbb).
selects positions 2 and 3 and sets A2=A3= c (bbb→bcc).
In the 2-nd test case Koa has no way to make string A equal B.
In the 3-rd test case Koa:
selects position 1 and sets A1= t (abc→tbc).
selects position 2 and sets A2= s (tbc→tsc).
selects position 3 and sets A3= r (tsc→tsr).


int n;
string a, b;
int main() {
    int t=read();
    while (t--) {
        cin >> n >> a >> b;
        int cnt[25][25] = {0};
        bool imp = false;
        for (int x = 0; x < a.length(); x++) {
            if (a[x] > b[x]) {
                imp = true;
                break;
            } else if (a[x] < b[x]) {
                cnt[a[x]-'a'][b[x]-a[x]]++;
            }
        }
        if (imp) {
            cout << -1 <<endl;
            continue;
        }
        int ans = 0;
        for (int x = 'a'; x < 'a'+20; x++) {
            for (int i = 1; i < 20; i++) {
                if (cnt[x-'a'][i] > 0) {
                    ans++;
                    for (int j = i+1; j < 20; j++) {
                        cnt[x+i-'a'][j-i]+= cnt[x-'a'][j];
                    }
                    break;
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值