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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//#pragma comment(linker, "/stack:200000000")
#ifndef LOCAL
#pragma GCC optimize("O3")
#endif
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>

using namespace std;

#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
	*this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define var(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

#define pb push_back
#define mp make_pair
#define st first
#define nd second
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define un(x) x.erase(unique(ALL(x)),x.end())
#define REP(i,x,v)for(int i=x;i<=v;i++)
#define REPD(i,x,v)for(int i=x;i>=v;i--)
#define FOR(i,v)for(int i=0;i<v;i++)
#define TRAV(i,v)for(auto &i:(v))
#define REMIN(x,v) x=min(x,v);
#define REMAX(x,v) x=max(x,v);

mt19937_64 rng(134569);
int rd(int l, int r) {return uniform_int_distribution<int>(l, r)(rng);}

using ll=long long;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
using vi=vector<int>;
using vll=vector<ll>;
using lint=unsigned long long;
const int MOD=1e9+7;//998244353;

#define B 64

#define N 3002
int len[N/(B-1)+2];
lint M[26][N/(B-1)+2];
lint Mneg[26][N/(B-1)+2];
lint dp[N/(B-1)+2],tp[N/(B-1)+2];
lint lenbit[N/(B-1)+2];

// ultrafast LCS by DamianS
int go(string s, string t) {
    int n=s.size();
    int m=t.size();
    memset(M,0,sizeof(M));

    int bl=0;
    for(int i=0;i<m;i+=B-1)
    {
                len[bl]=min(m-i,B-1);
                for(int j=i;j<min(m,i+B-1);j++)
                {
                        t[j]-='a';
                        M[(int)t[j]][bl]|=(1LL<<(j-i));
                }
                FOR(c,26) Mneg[c][bl]=(~M[c][bl])&((1LL<<len[bl])-1);
                lenbit[bl]=(1LL<<len[bl]);
                bl++;
        }
        FOR(i,bl) dp[i]=(1LL<<len[i])-1;
        int wyn=0;

        FOR(ii,n)
        {
                char c=s[ii]-'a';
                int carry=0;
                FOR(i,bl)
                {
                        tp[i]=(dp[i]+(dp[i]&M[(int)c][i])+carry);
                        if (tp[i]&lenbit[i])
                        {
                                carry=1;
                                tp[i]^=lenbit[i];
                        }
                        else carry=0;
                }
                FOR(i,bl) dp[i]=tp[i]|(dp[i]&Mneg[(int)c][i]);
                wyn+=carry;
        }
       return wyn;
}
//code from https://github.com/phoemur/competitive-programming/blob/master/SPOJ/LCS0%20-%20Longest%20Common%20Subsequence.cpp
int LCS(const std::string& X, const std::string& Y)
{
    int m = X.size();
    int n = Y.size();

    // Return on empty string
    if (m == 0 || n == 0) return 0;

    int w = (m + 63) >> 6; // ceil[m/32]

    uint64_t S[26][w];  //  Sigma {all  char}
    memset(S, 0, sizeof(std::uint64_t) * 26 * w);

    int64_t set = 1;

    for (int i = 0, j = 0; i < m; ++i)
    {
        S[X[i]][j] |= set;
        set <<= 1;
        if (!set) {set++;j++;}
    }

    uint64_t L[w]; //  Vetor L(i)
    memset(L, 0, sizeof(std::uint64_t) * w);

    for (int i = 0; i < n; ++i)
    {
        uint64_t b1 = 1; // Borrows
        uint64_t b2 = 0;

        for (int j = 0; j < w; ++j)
        {
            uint64_t U  = L[j] | S[Y[i]][j];
            uint64_t c  = L[j] >> 63; // Carry
            uint64_t V  = U - (L[j] << 1 | b1+b2);
            b1 = c;
            b2 = (V > U);
            L[j] = U & (~V);
        }
    }

    int res = 0;
    for (int i = 0; i < w; ++i)
        for (; L[i]; ++res)
            L[i] &= L[i] - 1;

    return res;
}



int main()
{
	ios_base::sync_with_stdio(0);cin.tie(0);
	int n,m,q;cin>>n>>m>>q;
  string S;cin>>S;
  string T;cin>>T;
  FOR(te, q) {
    int a1,b1, a2,b2;
    cin>>a1>>b1>>a2>>b2;
    string s=S.substr(a1-1, b1-a1+1);
    string t=T.substr(a2-1, b2-a2+1);
    FOR(i, SZ(s)) s[i]-='a';
    FOR(i, SZ(t)) t[i]-='a';
    if (SZ(s)<SZ(t)) swap(s,t);
    //debug()<<var(s)<<var(t);
    cout<<LCS(s,t)<<"\n";
  }


    return 0;
}