1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <bits/stdc++.h>
using namespace std;
#define inf 1000000000000000007
using ll = long long;

int n, m, q, p1, k1, p2, k2;
string s, t;
int dp[3007][3007];

int lcs (string a, string b)
{
    int m = a.size() - 1;
    int n = b.size() - 1;
    int m_kon = m;
    int n_kon = n;
    int start = 1;
    //cout << "(" << a << ") (" << b << ") " << m << " " << n << "\n";

    while (start <= m_kon && start <= n_kon && a[start] == b[start])
        start++;
    while (start <= m_kon && start <= n_kon && a[m_kon] == b[n_kon])
    {
        m_kon--;
        n_kon--;
    }
    for (int i=start-1; i<=m_kon; i++)
        dp[i][start-1] = start-1;
    for (int i=start-1; i<=n_kon; i++)
        dp[start-1][i] = start-1;
    for (int i=start; i<=m_kon; i++)
    {
        for (int j=start; j<=n_kon; j++)
        {
            if (a[i] == b[j])
                dp[i][j] = dp[i-1][j-1] + 1;
            else
                dp[i][j] = max(dp[i][j-1], dp[i-1][j]);
        }
    }
    //cout << start << " " << m_kon << " " << n_kon << " " << dp[m_kon][n_kon] << " " << m << " " << n << " " << "\n";
    return dp[m_kon][n_kon] + m - m_kon;
}

int main ()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> m >> q;
    cin >> s;
    cin >> t;
    while (q--)
    {
        cin >> p1 >> k1 >> p2 >> k2;
        string a = " ";
        string b = " ";
        for (int i=p1; i<=k1; i++)
            a += s[i-1];
        for (int i=p2; i<=k2; i++)
            b += t[i-1];
        cout << lcs(a, b) << "\n";
    }

    return 0;
}