CF1383A.String Transformation 1

普及+/提高

通过率:0%

时间限制:1.00s

内存限制:256MB

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 (k≥1;1≤pi≤n;pi≠pjk \ge 1; 1 \le p_i \le n; p_i \neq p_j if i≠ji \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 \gt 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 (1≤i≤k1 \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!

注意,字符串变换 1 与字符串变换 2 的唯一区别在于 Koa 所执行的操作:在本题中,Koa 所选择的字母 yy 必须在字典序上严格大于 xx(请阅读题目描述以获得更清晰的理解)。你可独立地对这两道题进行 hack。

考拉 Koa 拥有两个长度均为 nn 的字符串 AA 和 BB(即 ∣A∣=∣B∣=n|A|=|B|=n),它们均由英文字母表前 2020 个小写字母组成(即从 a 到 t)。

在一次操作中,Koa 执行以下步骤:

  1. 选取 AA 中若干位置组成的子集 p1,p2,…,pkp_1, p_2, \ldots, p_k(其中 k≥1k \ge 1;1≤pi≤n1 \le p_i \le n;当 i≠ji \neq j 时 pi≠pjp_i \neq p_j),满足 Ap1=Ap2=…=Apk=xA_{p_1} = A_{p_2} = \ldots = A_{p_k} = x(即这些位置上的所有字母均等于某个字母 xx)。

  2. 选取一个字母 yy(来自英文字母表前 2020 个小写字母),使得 y>xy \gt x(即字母 yy 在字典序上严格大于 xx)。

  3. 将位置 p1,p2,…,pkp_1, p_2, \ldots, p_k 上的所有字母均设为字母 yy。更准确地说:对每个 ii(1≤i≤k1 \le i \le k),Koa 将 ApiA_{p_i} 设为 yy。

    注意:你只能修改字符串 AA 中的字母。

Koa 想知道,使两个字符串相等(即 A=BA = B)所需的最少操作次数,或者判断是否无法使它们相等。请帮助她!

输入格式

Each test contains multiple test cases. The first line contains tt (1≤t≤101 \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 (1≤n≤1051 \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.

每个测试包含多个测试用例。第一行包含一个整数 tt(1≤t≤101 \le t \le 10),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤1051 \le n \le 10^5),表示字符串 AA 和 BB 的长度。

每个测试用例的第二行包含字符串 AA(∣A∣=n|A|=n)。

每个测试用例的第三行包含字符串 BB(∣B∣=n|B|=n)。

两个字符串均由前 2020 个小写英文字母组成(即从 a 到 t)。

保证所有测试用例的 nn 值之和不超过 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.

对于每个测试用例:
在一行中输出使两个字符串相等(即 A=BA = B)所需的最少操作次数;若无法使其相等,则输出 −1-1。

输入输出样例

  • 输入#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 (aab→bbb\color{red}{aa}b \rightarrow \color{blue}{bb}b).
    2. selects positions 22 and 33 and sets $A_2 = A_3 = $ c (bbb→bccb\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 (abc→tbc\color{red}{a}bc \rightarrow \color{blue}{t}bc).
    2. selects position 22 and sets $A_2 = $ s (tbc→tsct\color{red}{b}c \rightarrow t\color{blue}{s}c).
    3. selects position 33 and sets $A_3 = $ r (tsc→tsrts\color{red}{c} \rightarrow ts\color{blue}{r}).
  • 在第 11 个测试用例中,Koa 执行以下操作:

    1. 选择位置 11 和 22,并将 $A_1 = A_2 = $ b(aab→bbb\color{red}{aa}b \rightarrow \color{blue}{bb}b)。
    2. 选择位置 22 和 33,并将 $A_2 = A_3 = $ c(bbb→bccb\color{red}{bb} \rightarrow b\color{blue}{cc})。
  • 在第 22 个测试用例中,Koa 无法使字符串 AA 变为与 BB 相等。

  • 在第 33 个测试用例中,Koa 执行以下操作:

    1. 选择位置 11,并将 $A_1 = $ t(abc→tbc\color{red}{a}bc \rightarrow \color{blue}{t}bc)。
    2. 选择位置 22,并将 $A_2 = $ s(tbc→tsct\color{red}{b}c \rightarrow t\color{blue}{s}c)。
    3. 选择位置 33,并将 $A_3 = $ r(tsc→tsrts\color{red}{c} \rightarrow ts\color{blue}{r})。

输入解题思路,AI测评打分。不知道怎么写?

首页