Codeforces Round 1079 (Div. 1)
E2
Fuzzy Concatenation (Hard version)
Codeforces
3000
原题
← E1F →
翻译中…

This is the hard version of the problem. The difference between the versions is that in this version, $n \le 5 \cdot 10^{5}, m \le 5 \cdot 10^{4}$. You can hack only if you solved all versions of this problem.

翻译中…

There are two strings $s$ and $t$, both consisting of lowercase Latin letters. You also have an empty string $p$.

翻译中…

You can perform the following operation, which consists of several stages:

翻译中…
  • choose any two integers $l$ and $r$ ($1 \le l \le r \le |s|$);
  • copy the substring $s_{l},\ldots, s_{r}$ and append it to the end of string $p$;
  • among the last $r - l + 1$ characters of string $p$, change at most one character to any lowercase Latin letter.
翻译中…

For example, if the string $s = $ "dhhtyhwbsl" and $p = $ "", you can choose $l = 3, r = 6$ and add "htyh" to the end of $p$, and then change the character "y" to "a", resulting in $p = $ "htah".

翻译中…

Your task is to determine the minimum number of operations required for string $p$ to become equal to $t$.

翻译中…

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 of each test case contains two integers $n$ and $m$ ($1 \le n \le 5 \cdot 10^{5}, 1 \le m \le 5 \cdot 10^{4}$) — the lengths of strings $s$ and $t$.

翻译中…

The second line of each test case contains the string $s$ of length $n$, consisting of lowercase Latin letters.

翻译中…

The third line of each test case contains the string $t$ of length $m$, consisting of lowercase Latin letters.

翻译中…

It is guaranteed that the sum of $n$ across all test cases does not exceed $5 \cdot 10^{5}$, and the sum of $m$ across all test cases does not exceed $5 \cdot 10^{4}$.

翻译中…

Output

翻译中…

For each test case, output a single integer — the answer to the problem.

翻译中…

Note

翻译中…

In the first test case, you can take the substring $«\mathtt{a}»$ from the string $s$, change a single letter in this string to $«\mathtt{b}»$, and append it to the end of the string $p$, resulting in $p$ becoming equal to $t$.

翻译中…

In the second test case, $t$ can be obtained in $3$ operations as follows:

翻译中…

$$«\mathtt{a\color{red}{b}}» + «\mathtt{\color{red}{z}}» + «\mathtt{\color{red}{b}a}»$$

翻译中…

The modified characters are highlighted in red.

翻译中…

In the third test case, the string $t$ can be obtained as follows:

翻译中…

$$«\mathtt{ht\color{red}{a}h}» + «\mathtt{bs\color{red}{e}}» + «\mathtt{htyh\color{red}{z}b}»$$

翻译中…

样例

Input #1
4
1 1
a
b
5 5
aaaaa
abzba
10 13
dhhtyhwbsl
htahbsehtyhzb
7 2
contest
on
Output #1
1
3
3
1
InputCopy OutputCopy
题目信息
题号
2196E2
时间限制
3 s
内存限制
256 MB
标签
二分
位运算
数据结构
动态规划
贪心
后缀结构

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