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
#include <iostream>
#include <string>
#include <vector>

int main() {
    std::ios::sync_with_stdio(false);

    int n, m, q;
    std::string s1, s2;
    std::cin >> n >> m >> q >> s1 >> s2;

    while (q--) {
        int a, b, c, d;
        std::cin >> a >> b >> c >> d;
        --a; --b; --c; --d;

        std::vector<std::vector<int>> D(b - a + 1, std::vector<int>(d - c + 1));
        for (int i = a; i <= b; ++i) D[i - a][0] = s1[i] == s2[c];
        for (int j = c; j <= d; ++j) D[0][j - c] = s1[a] == s2[j];
        for (int i = a + 1; i <= b; ++i) {
            for (int j = c + 1; j <= d; ++j) {
                D[i - a][j - c] = std::max(std::max(D[i - a - 1][j - c], D[i - a][j - c - 1]),
                                           D[i - a - 1][j - c - 1] + (s1[i] == s2[j]));
            }
        }
        std::cout << D.back().back() << "\n";
    }

    return 0;
}