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
#pragma GCC optimize("Ofast")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define mp make_pair
#define eb emplace_back
#define pb push_back
#define e1 first
#define e2 second
#define uint unsigned int
#define ll long long
#define ull unsigned long long
#define ld long double
#define float long double
#define size(x) (int)x.size()
#define satori int testCases; cin>>testCases; while(testCases--)
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define all(r) begin(r),end(r)
#define time chrono::high_resolution_clock().now().time_since_epoch().count()
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
mt19937 rng(chrono::high_resolution_clock().now().time_since_epoch().count());
///////////////////
#define debug if(1)
///////////////////

const int MAXQ=1e5+10,MAXN=3e3+10;

string s,t;
int ans[MAXQ],ls[MAXQ],rs[MAXQ],lt[MAXQ],rt[MAXQ],dp[MAXN];
int same[MAXN][MAXN];
int top[MAXN][MAXN],bot[MAXN][MAXN];

void solve3(int lo,int hi,int m,vector<int> queries){
	if(lo==hi){
		for(auto id:queries){
			for(int i=lt[id];i<=rt[id];i++)
				if(same[lo][i]){
					ans[id]=1;
					break;
				}
		}
		return;
	}
	int mid=(lo+hi)/2;
	vector<int> top_queries,bot_queries,mid_queries;
	for(auto id:queries)
		if(max(ls[id],rs[id])<=mid)
			bot_queries.eb(id);
		else
		if(min(ls[id],rs[id])>mid)
			top_queries.eb(id);
		else
			mid_queries.eb(id);
	if(size(bot_queries))
		solve3(lo,mid,m,bot_queries);
	if(size(top_queries))
		solve3(mid+1,hi,m,top_queries);
	if(size(mid_queries)==0)
		return;
	for(int i=0;i<=m;i++){
		if(i!=0){
			for(int row=mid;row>=lo;row--){
				for(int col=i;col>=1;col--){
					bot[row][col]=0;
					if(same[row][col]){
						bot[row][col]=1;
						if(row+1<=mid&&col+1<=i)
							bot[row][col]+=bot[row+1][col+1];
					}
					else{
						if(row+1<=mid)
							bot[row][col]=bot[row+1][col];
						if(col+1<=i)
							bot[row][col]=max(bot[row][col],bot[row][col+1]);
					}
				}
			}
		}
		if(i!=m){
			for(int row=mid+1;row<=hi;row++){
				for(int col=i+1;col<=m;col++){
					top[row][col]=0;
					if(same[row][col]){
						top[row][col]=1;
						if(row-1>mid&&col-1>i)
							top[row][col]+=top[row-1][col-1];
					}
					else{
						if(row-1>mid)
							top[row][col]=top[row-1][col];
						if(col-1>i)
							top[row][col]=max(top[row][col],top[row][col-1]);
					}
				}
			}
		}
		for(auto id:mid_queries){
			if(i>=lt[id]&&i<=rt[id])
				ans[id]=max(ans[id],bot[ls[id]][lt[id]]);
			if(i+1>=lt[id]&&i+1<=rt[id])
				ans[id]=max(ans[id],top[rs[id]][rt[id]]);
			if(i>=lt[id]&&i+1<=rt[id])
				ans[id]=max(ans[id],bot[ls[id]][lt[id]]+top[rs[id]][rt[id]]);
		}
	}
}

void solve2(int n,int m,int q){
	vector<int> queries;
	for(int i=0;i<q;i++)
		queries.eb(i);
	solve3(1,n,m,queries);
}

void solve1(int n,int m,int q){
	int mx,pmx;
	for(int id=0;id<q;id++){
		for(int i=ls[id];i<=rs[id];i++)
			dp[i]=0;
		for(int j=lt[id];j<=rt[id];j++){
			mx=0;
			for(int i=ls[id];i<=rs[id];i++)
				pmx=max(mx,dp[i]),dp[i]=max(dp[i],mx+same[i][j]),mx=pmx;
		}
		mx=0;
		for(int i=ls[id];i<=rs[id];i++)
			mx=max(mx,dp[i]);
		ans[id]=mx;
	}
}

int32_t main()
{
	fastio;
	int n,m,q;
	cin>>n>>m>>q;
	cin>>s>>t;
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
			if(s[i]==t[j])
				same[i+1][j+1]=1;
	for(int i=0;i<q;i++)
		cin>>ls[i]>>rs[i]>>lt[i]>>rt[i];
	if(max({n,m,q})<=600){
		solve1(n,m,q);
	}
	else{
		solve2(n,m,q);
	}
	for(int i=0;i<q;i++)
		cout<<ans[i]<<'\n';
}