CF2164D.Copy String

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Given two strings ss and tt of length nn, you aim to transform ss into tt through a series of the following operations:

  • Construct a new string s′s' of length nn,where s1′=s1s'_1=s_1. For each 1<i≤n1 \lt i \le n, si′s'_i can be either sis_i or si−1s_{i-1}. Then replace ss with s′s'.

Your task is to achieve this transformation using the minimum number of operations. You also need to output the solution by printing the constructed string s′s' after each operation. If the transformation cannot be achieved in less than or equal to kmaxk_{\mathrm{max}} operations, output -1.

给定两个长度为 nn 的字符串 ss 和 tt,你需要通过一系列如下操作将 ss 变换为 tt:

  • 构造一个长度为 nn 的新字符串 s′s',其中 s1′=s1s'_1 = s_1;对每个 1<i≤n1 < i \le n,si′s'_i 可以是 sis_i 或 si−1s_{i-1}。然后将 ss 替换为 s′s'。

你的任务是使用最少的操作次数完成该变换。你还需要输出具体方案:每次操作后打印所构造的字符串 s′s'。若无法在不超过 kmaxk_{\mathrm{max}} 次操作内完成变换,则输出 -1。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains two integers nn, kmaxk_{\mathrm{max}} (1≤n⋅kmax≤1061 \le n \cdot k_{\mathrm{max}} \le 10^6) — the length of two strings and the maximum number of operations that can be used.

The second line of each test case contains one string ss of length nn.

The third line of each test case contains one string tt of length nn.

It is guaranteed that the sum of nkmaxnk_{\mathrm{max}} over all test cases does not exceed 10610^6.

It is guaranteed that both ss and tt consist of lowercase Latin letters.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含两个整数 nn、kmaxk_{\mathrm{max}}(1≤n⋅kmax≤1061 \le n \cdot k_{\mathrm{max}} \le 10^6)——分别表示两个字符串的长度以及最多可执行的操作次数。

每个测试用例的第二行包含一个长度为 nn 的字符串 ss。

每个测试用例的第三行包含一个长度为 nn 的字符串 tt。

保证所有测试用例的 nkmaxn k_{\mathrm{max}} 之和不超过 10610^6。

保证字符串 ss 和 tt 均仅由小写拉丁字母组成。

输出格式

For each test case:

  • If you cannot achieve the transformation in less than or equal to kmaxk_{\mathrm{max}} operations, simply output -1 in one line.
  • Otherwise, on the first line, output one integer k≤kmaxk \le k_{\mathrm{max}} — the minimum number of operations. Then kk lines follow, each line contains one string of length nn — the string after each operation.

If there are multiple solutions, output any.

对于每个测试用例:

  • 如果无法在不超过 kmaxk_{\mathrm{max}} 次操作内完成该变换,则直接在一行中输出 -1。
  • 否则,在第一行输出一个整数 k≤kmaxk \le k_{\mathrm{max}} —— 即所需的最少操作次数。随后输出 kk 行,每行包含一个长度为 nn 的字符串 —— 即每次操作后得到的字符串。

若存在多种可行解,输出任意一种即可。

输入输出样例

  • 输入#1

    7
    4 1
    abcd
    aabd
    2 2
    ab
    ab
    5 3
    abcde
    abbcc
    9 1
    egcnyeluw
    eegccyelw
    10 3
    vzvylxxmsy
    vvvvvllxxx
    4 6
    acba
    aaac
    5 7
    acabb
    aaaca

    输出#1

    1
    aabd
    0
    2
    abbcd
    abbcc
    -1
    3
    vvzvylxxms
    vvvzvllxxm
    vvvvvllxxx
    2
    aacb
    aaac
    2
    aacab
    aaaca

说明/提示

In the first test case, obviously ss can be transformed to tt in one operation.

In the second test case, initially s=ts=t, so no operation is needed.

In the fourth test case, although ss can be transformed to tt in two operations, but kmax=1k_{\mathrm{max}}=1, so the answer is -1.

在第一个测试用例中,显然 ss 可以通过一次操作转换为 tt。

在第二个测试用例中,初始时 s=ts=t,因此无需任何操作。

在第四个测试用例中,尽管 ss 可以通过两次操作转换为 tt,但 kmax=1k_{\mathrm{max}}=1,因此答案为 −1-1。

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

首页