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
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define ST first
#define ND second
#define PB push_back
#define SIZE(a) ((int)a.size())

template<class T, class U>
ostream& operator<<(ostream &stream, pair<T, U> &p) {
    stream << "(" << p.ST << "," << p.ND << ")";
    return stream;
}

template<class T>
ostream& operator<<(ostream &stream, vector<T> &v) {
    stream << "[";
    for(auto elem : v) {
        stream << elem << ", ";
    }
    stream << "]";
    return stream;
}

struct Query {
    int sa, sb, ta, tb, id, ans;
};

vector<Query> res;
vector<vector<int>> dp;

void rec(vector<Query> &queries, string &s, string &t, int sa, int sb, int ta, int tb) {
    // cerr << SIZE(queries) << " " << sa << " " << sb << " " << ta << " " << tb << "\n";
    if(SIZE(queries) == 0) {
        return;
    }
    vector<Query> le, ri, cur;
    int n = sb-sa;
    int m = tb-ta;
    if(n <= m) { //vertical
        int mid = (ta+tb)/2;
        // cerr << mid << "\n";
        for(Query &q: queries) {
            if(q.tb < mid) {
                le.PB(q);
            } else if(mid < q.ta) {
                ri.PB(q);
            } else {
                cur.PB(q);
            }
        }
        for(int k=sa; k <= sb; k++) {
            for(int j=ta; j <= tb; j++) {
                dp[k][j] = 0;
            }
            for(int i=sa; i <= sb; i++) {
                dp[i][mid] = 0;
            }
            for(int i=k+1; i <= sb; i++) {
                for(int j=mid+1; j <= tb; j++) {
                    if(s[i] == t[j]) {
                        dp[i][j] = dp[i-1][j-1]+1;
                    } else {
                        dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
                    }
                }
            }
            for(int i=k-1; i >= sa; i--) {
                for(int j=mid-1; j >= ta; j--) {
                    if(s[i+1] == t[j+1]) {
                        dp[i][j] = dp[i+1][j+1]+1;
                    } else {
                        dp[i][j] = max(dp[i+1][j], dp[i][j+1]);
                    }
                }
            }
            for(Query &q : cur) {
                // cerr << q.id << " " << dp[q.sa][q.ta] + dp[q.sb][q.tb] << "\n";
                if(k < q.sa || q.sb < k) {
                    continue;
                }
                q.ans = max(q.ans, dp[q.sa][q.ta] + dp[q.sb][q.tb]);
            }
        }
        for(Query &q : cur) {
            res.PB(q);
        }
        rec(ri, s, t, sa, sb, mid+1, tb);
        rec(le, s, t, sa, sb, ta, mid-1);
    } else {
        for(Query &q: queries) {
            swap(q.ta, q.sa);
            swap(q.tb, q.sb);
        }
        rec(queries, t, s, ta, tb, sa, sb);
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    int n, m, q;
    string s, t;
    cin >> n >> m >> q >> s >> t;
    s = '0' + s;
    t = '0' + t;
    int d = max(n, m)+1;
    dp.resize(d, vector<int>(d, 0));
    if(q >= 5*min(n, m)) {
        vector<Query> queries(q);
        for(int i=0; i < q; i++) {
            Query query;
            cin >> query.sa >> query.sb >> query.ta >> query.tb;
            query.sa--;
            query.ta--;
            query.id = i;
            query.ans = 0;
            queries[i] = query;
        }
        rec(queries, s, t, 0, n, 0, m);
        sort(res.begin(), res.end(), [](Query &a, Query &b){return a.id < b.id; });
        for(Query &q : res) {
            cout << q.ans << "\n";
        }
    } else {
        while(q--) {
            int sa, sb, ta, tb;
            cin >> sa >> sb >> ta >> tb;
            for(int i=sa-1; i <= sb; i++) {
                dp[i][ta-1] = 0;
            }
            for(int j=ta-1; j <= tb; j++) {
                dp[sa-1][j] = 0;
            }
            for(int i=sa; i <= sb; i++) {
                for(int j=ta; j <= tb; j++) {
                    if(s[i] == t[j]) {
                        dp[i][j] = dp[i-1][j-1]+1;
                    } else {
                        dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
                    }
                }
            }
            cout << dp[sb][tb] << "\n";
        }
    }
}