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
#include <bits/stdc++.h>
using namespace std;

string S, K;
int n, m, q;
int a, b, c, d;
int T[3003][3003];

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

    cin>>n>>m>>q;
    cin>>S>>K;
    S="#"+S;
    K="#"+K;

    while (q>0){
        cin>>a>>b>>c>>d;

        for (int i=c-1; i<=d; ++i)
            for (int j=a-1; j<=b; ++j)
                T[i][j]=0;

        for (int i=c; i<=d; ++i){
            for (int j=a; j<=b; ++j){
                if (K[i]==S[j])
                    T[i][j]=T[i-1][j-1]+1;
                else
                    T[i][j]=max(T[i-1][j], T[i][j-1]);
            }
        }

        cout<<T[d][b]<<"\n";

        --q;
    }

    return 0;
}