#P9109. [PA 2020] Tekstówka

[PA 2020] Tekstówka

Problem Description

This problem is translated from PA 2020 Round 4 Tekstówka.

Last year, during the PA held on a fan page of a certain social network, participants loudly asked us: “Where is the problem?”. This year, we decided to meet your expectations.

You are given two strings ss and tt consisting of lowercase English letters. Let si,j (1ijs)s_{i,j}\ (1\le i\le j\le |s|) denote the substring formed by the ii-th through the jj-th characters (inclusive) of ss in order. We define ti,jt_{i,j} in the same way.

Your task is to process qq queries. Each query is given by four integers i,j,k,li, j, k, l, where 1ijs1\le i\le j\le |s| and 1klt1\le k\le l\le |t|. For each query, output the length of the longest common subsequence of the substring si,js_{i,j} and the substring tk,lt_{k,l}.

Note: A subsequence of a string is a string obtained by deleting some (possibly none) characters without changing the order of the remaining characters. For example, subsequences of potyczki\texttt{potyczki} can be tyki\texttt{tyki} or pi\texttt{pi}, but cannot be koty\texttt{koty}.

We call a common subsequence of strings aa and bb a subsequence that is both a subsequence of aa and a subsequence of bb.

We call the longest common subsequence of strings aa and bb the longest one among all common subsequences of aa and bb.

Input Format

The first line contains three integers n,m,qn, m, q, representing the lengths of strings ss and tt, and the number of queries.

The second line contains a string ss of length nn consisting of lowercase English letters.

The third line contains a string tt of length mm consisting of lowercase English letters.

The next qq lines each contain four integers i,j,k,li, j, k, l, with the meaning as described above.

Output Format

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

5 6 7
abaab
babbaa
1 5 1 6
1 3 2 4
2 5 2 5
1 4 2 5
2 5 3 6
2 2 5 6
3 4 2 2
4
2
2
3
3
0
1

Hint

Constraints

This problem uses bundled testdata.

  • For some subtasks, n,m,q600n, m, q\le 600.
  • For some other subtasks, n,m600n, m\le 600.
  • For some other subtasks, q5×103q\le 5\times 10^3.

Among the above cases, at least one subtask is satisfied.

For 100%100\% of the testdata, it is guaranteed that 1n,m3×1031\le n, m\le 3\times 10^3, 1q1051\le q\le 10^5, 1ijn1\le i\le j\le n, and 1klm1\le k\le l\le m.

Translated by ChatGPT 5