#P4493. [HAOI2018] 字串覆盖

[HAOI2018] 字串覆盖

Description

Xiao C is quite into strings. He finds traditional string matching too boring, so he came up with the following problem.

For two strings A,BA, B of length nn, each time he gives four parameters s,t,l,rs, t, l, r. Let TT be the substring of AA from ss to tt (1-indexed), and let PP be the substring of BB from ll to rr. Then he performs the following operation:

If some substring of TT equals PP, we can cover this substring of TT and gain KiK - i, where ii is the starting position of this substring in AA at the beginning (note: not in TT), and KK is a given parameter. A position cannot be covered more than once. The covering operation can be performed any number of times. You need to output the maximum total gain.

Note that each query is independent, i.e., after finishing a query, the covered positions are restored.

Input Format

The first line contains two integers n,Kn, K, denoting the string length and the parameter.

The next line contains a string AA.

The next line contains a string BB.

The next line contains an integer qq, denoting the number of queries.

Each of the next qq lines contains four integers s,t,l,rs, t, l, r, describing one query.

Output Format

Output qq lines, each containing one integer, the answer for each query.

10 11
abcbababab
ababcbabab
5
1 9 7 9
3 10 8 10
1 10 1 2
5 7 2 3
1 5 3 6
6
10
22
5
10

Hint

Explanation for Sample 1:

For all testdata, 1n,q1051 \le n, q \le 10^5, A,BA, B consist only of lowercase English letters, 1stn1 \le s \le t \le n, 1lrn1 \le l \le r \le n, n<K109n < K \le 10^9. HAOI2018 Round 1 T3.

For the testpoint with n=105n = 10^5, the number of queries satisfying 51rl2×10351 \le r - l \le 2 \times 10^3 does not exceed 1100011000, and l,rl, r are uniformly random.

Constraints

Translated by ChatGPT 5