CF1383A.String Transformation 1

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Note that the only difference between String Transformation 1 and String Transformation 2 is in the move Koa does. In this version the letter yy Koa selects must be strictly greater alphabetically than xx (read statement for better understanding). You can make hacks in these problems independently.

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

In one move Koa:

  1. selects some subset of positions p1,p2,,pkp_1, p_2, \ldots, p_k ( k1;1pin;pipjk \ge 1; 1 \le p_i \le n; p_i \neq p_j if iji \neq j ) of AA such that Ap1=Ap2==Apk=xA_{p_1} = A_{p_2} = \ldots = A_{p_k} = x (ie. all letters on this positions are equal to some letter xx ).
  2. selects a letter yy (from the first 2020 lowercase letters in English alphabet) such that y>xy>x (ie. letter yy is strictly greater alphabetically than xx ).
  3. sets each letter in positions p1,p2,,pkp_1, p_2, \ldots, p_k to letter yy . More formally: for each ii ( 1ik1 \le i \le k ) Koa sets Api=yA_{p_i} = y . Note that you can only modify letters in string AA .

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

输入格式

Each test contains multiple test cases. The first line contains tt ( 1t101 \le t \le 10 ) — the number of test cases. Description of the test cases follows.

The first line of each test case contains one integer nn ( 1n1051 \le n \le 10^5 ) — the length of strings AA and BB .

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

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

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

It is guaranteed that the sum of nn over all test cases does not exceed 10510^5 .

输出格式

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=BA = B ) or 1-1 if there is no way to make them equal.

输入输出样例

  • 输入#1

    5
    3
    aab
    bcc
    4
    cabc
    abcb
    3
    abc
    tsr
    4
    aabd
    cccd
    5
    abcbd
    bcdda

    输出#1

    2
    -1
    3
    2
    -1

说明/提示

  • In the 11 -st test case Koa:
    1. selects positions 11 and 22 and sets $A_1 = A_2 = $ b ( aabbbb\color{red}{aa}b \rightarrow \color{blue}{bb}b ).
    2. selects positions 22 and 33 and sets $A_2 = A_3 = $ c ( bbbbccb\color{red}{bb} \rightarrow b\color{blue}{cc} ).
  • In the 22 -nd test case Koa has no way to make string AA equal BB .
  • In the 33 -rd test case Koa:
    1. selects position 11 and sets $A_1 = $ t ( abctbc\color{red}{a}bc \rightarrow \color{blue}{t}bc ).
    2. selects position 22 and sets $A_2 = $ s ( tbctsct\color{red}{b}c \rightarrow t\color{blue}{s}c ).
    3. selects position 33 and sets $A_3 = $ r ( tsctsrts\color{red}{c} \rightarrow ts\color{blue}{r} ).
首页