Codeforces Round 848 (Div. 2)
C
Flexible String
Codeforces
1600
原题
← BD →
翻译中…

You have a string $a$ and a string $b$. Both of the strings have length $n$. There are at most $10$ different characters in the string $a$. You also have a set $Q$. Initially, the set $Q$ is empty. You can apply the following operation on the string $a$ any number of times:

翻译中…
  • Choose an index $i$ ($1\leq i \leq n$) and a lowercase English letter $c$. Add $a_i$ to the set $Q$ and then replace $a_i$ with $c$.
翻译中…

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

翻译中…
  • In the first operation, if you choose $i = 3$ and $c = \tt{x}$, the character $a_3 = \tt{e}$ will be added to the set $Q$. So, the set $Q$ will be $\{\tt{e}\}$, and the string $a$ will be "$\tt{ab\underline{x}cca}$".
  • In the second operation, if you choose $i = 6$ and $c = \tt{s}$, the character $a_6 = \tt{a}$ will be added to the set $Q$. So, the set $Q$ will be $\{\tt{e}, \tt{a}\}$, and the string $a$ will be "$\tt{abxcc\underline{s}}$".
翻译中…

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

翻译中…

Input

翻译中…

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

翻译中…

The first line contains two integers $n$ and $k$ ($1\leq n \leq 10^5$, $0\leq k\leq 10$) — the length of the two strings and the limit on different characters in the set $Q$.

翻译中…

The second line contains the string $a$ of length $n$. There is at most $10$ different characters in the string $a$.

翻译中…

The last line contains the string $b$ of length $n$.

翻译中…

Both of the strings $a$ and $b$ contain only lowercase English letters. The sum of $n$ over all test cases doesn't exceed $10^5$.

翻译中…

Output

翻译中…

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

翻译中…

Note

翻译中…

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

翻译中…

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

翻译中…
  1. $a[1,1] = b[1,1] =$ "$\tt{a}$",
  2. $a[1,2] = b[1,2] =$ "$\tt{ab}$",
  3. $a[2,2] = b[2,2] =$ "$\tt{b}$".
翻译中…

In the third case, we can choose index $2$ and index $3$ and replace them with the characters $\tt{c}$ and $\tt{d}$ respectively. The final set $Q$ will be $\{\tt{b}\}$ having size $1$ that satisfies the value of $k$. All possible pairs $(l,r)$ will be valid.

翻译中…

样例

Input #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
Output #1
6
3
6
6
6
11
InputCopy OutputCopy
题目信息
题号
1778C
时间限制
2 s
内存限制
256 MB
标签
位运算
暴力
字符串

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。