CF1778C.Flexible String

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You have a string aa and a string bb. Both of the strings have length nn. There are at most 1010 different characters in the string aa. You also have a set QQ. Initially, the set QQ is empty. You can apply the following operation on the string aa any number of times:

  • Choose an index ii (1≤i≤n1\leq i \leq n) and a lowercase English letter cc. Add aia_i to the set QQ and then replace aia_i with cc.

For example, Let the string aa be "abecca\tt{abecca}". We can do the following operations:

  • In the first operation, if you choose i=3i = 3 and c=xc = \tt{x}, the character a3=ea_3 = \tt{e} will be added to the set QQ. So, the set QQ will be e{\tt{e}}, and the string aa will be "abx‾cca\tt{ab\underline{x}cca}".
  • In the second operation, if you choose i=6i = 6 and c=sc = \tt{s}, the character a6=aa_6 = \tt{a} will be added to the set QQ. So, the set QQ will be e,a{\tt{e}, \tt{a}}, and the string aa will be "abxccs‾\tt{abxcc\underline{s}}".

You can apply any number of operations on aa, but in the end, the set QQ should contain at most kk different characters. Under this constraint, you have to maximize the number of integer pairs (l,r)(l, r) (1≤l≤r≤n1\leq l\leq r \leq n) such that a[l,r]=b[l,r]a[l,r] = b[l,r]. Here, s[l,r]s[l,r] means the substring of string ss starting at index ll (inclusively) and ending at index rr (inclusively).

你有两个字符串 aa 和 bb,它们的长度均为 nn。字符串 aa 中至多包含 1010 种不同的字符。你还拥有一个集合 QQ,初始时 QQ 为空。你可以对字符串 aa 执行任意多次如下操作:

  • 选择一个下标 ii(1≤i≤n1 \leq i \leq n)和一个小写英文字母 cc。将字符 aia_i 加入集合 QQ,然后将 aia_i 替换为 cc。

例如,设字符串 aa 为 "abecca\tt{abecca}"。我们可以执行如下操作:

  • 在第一次操作中,若选择 i=3i = 3 和 c=xc = \tt{x},则字符 a3=ea_3 = \tt{e} 将被加入集合 QQ。此时集合 Q={e}Q = \{\tt{e}\},字符串 aa 变为 "abx‾cca\tt{ab\underline{x}cca}"。
  • 在第二次操作中,若选择 i=6i = 6 和 c=sc = \tt{s},则字符 a6=aa_6 = \tt{a} 将被加入集合 QQ。此时集合 Q={e,a}Q = \{\tt{e}, \tt{a}\},字符串 aa 变为 "abxccs‾\tt{abxcc\underline{s}}"。

你可以对 aa 执行任意多次操作,但最终集合 QQ 中至多包含 kk 种不同的字符。在此约束下,你需要最大化满足 a[l,r]=b[l,r]a[l,r] = b[l,r] 的整数对 (l,r)(l, r)(其中 1≤l≤r≤n1 \leq l \leq r \leq n)的个数。此处 s[l,r]s[l,r] 表示字符串 ss 从下标 ll(含)开始、到下标 rr(含)结束的子串。

输入格式

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 contains two integers nn and kk (1≤n≤1051\leq n \leq 10^5, 0≤k≤100\leq k\leq 10) — the length of the two strings and the limit on different characters in the set QQ.

The second line contains the string aa of length nn. There is at most 1010 different characters in the string aa.

The last line contains the string bb of length nn.

Both of the strings aa and bb contain only lowercase English letters. The sum of nn over all test cases doesn't exceed 10510^5.

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

第一行包含两个整数 nn 和 kk(1≤n≤1051\leq n \leq 10^5,0≤k≤100\leq k\leq 10)——分别表示两个字符串的长度以及集合 QQ 中不同字符数量的上限。

第二行包含一个长度为 nn 的字符串 aa。字符串 aa 中至多包含 1010 个不同的字符。

最后一行包含一个长度为 nn 的字符串 bb。

字符串 aa 和 bb 均仅由小写英文字母组成。所有测试用例中 nn 的总和不超过 10510^5。

输出格式

For each test case, print a single integer in a line, the maximum number of pairs (l,r)(l, r) satisfying the constraints.

对于每个测试用例,在一行中输出一个整数,表示满足约束条件的数对 (l,r)(l, r) 的最大数量。

输入输出样例

  • 输入#1

    6
    3 1
    abc
    abd
    3 0
    abc
    abd
    3 1
    xbb
    xcd
    4 1
    abcd
    axcb
    3 10
    abc
    abd
    10 3
    lkwhbahuqa
    qoiujoncjb

    输出#1

    6
    3
    6
    6
    6
    11

说明/提示

In the first case, we can select index i=3i = 3 and replace it with character c=dc = \tt{d}. All possible pairs (l,r)(l,r) will be valid.

In the second case, we can't perform any operation. The 33 valid pairs (l,r)(l,r) are:

  1. a[1,1]=b[1,1]=a[1,1] = b[1,1] = "a\tt{a}",
  2. a[1,2]=b[1,2]=a[1,2] = b[1,2] = "ab\tt{ab}",
  3. a[2,2]=b[2,2]=a[2,2] = b[2,2] = "b\tt{b}".

In the third case, we can choose index 22 and index 33 and replace them with the characters c\tt{c} and d\tt{d} respectively. The final set QQ will be b{\tt{b}} having size 11 that satisfies the value of kk. All possible pairs (l,r)(l,r) will be valid.

在第一种情况下,我们可以选择索引 i=3i = 3 并将其替换为字符 c=dc = \tt{d}。所有可能的数对 (l,r)(l,r) 均有效。

在第二种情况下,我们无法执行任何操作。有效的 33 个数对 (l,r)(l,r) 为:

  1. a[1,1]=b[1,1]=a[1,1] = b[1,1] = "a\tt{a}",
  2. a[1,2]=b[1,2]=a[1,2] = b[1,2] = "ab\tt{ab}",
  3. a[2,2]=b[2,2]=a[2,2] = b[2,2] = "b\tt{b}".

在第三种情况下,我们可以选择索引 22 和索引 33,并分别将它们替换为字符 c\tt{c} 和 d\tt{d}。最终集合 QQ 将为 b{\tt{b}},其大小为 11,满足 kk 的值。所有可能的数对 (l,r)(l,r) 均有效。

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

首页