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 <iostream>
#include <vector>
using namespace std;

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

  int n1, m1, k; cin >> n1 >> m1 >> k;
  string s, t; cin >> s >> t;
  s = '-' + s;
  t = '-' + t;

  while(k--) {
    int a, b, c, d; cin >> a >> b >> c >> d;

    int n = b - a + 1;
    int m = d - c + 1;

    vector<vector<int>> dp(2, vector<int>(m + 1, 0));
    for(int i = 0; i < m; i++) {
      dp[0][i] = 0;
    }
    dp[1][0] = 0;

    for(int i = 1; i <= n; i++) {
      for(int j = 1; j <= m; j++) {
        if(s[a + i - 1] == t[c + j - 1]) {
          dp[i % 2][j] = dp[(i - 1) % 2][j - 1] + 1;
        }
        else {
          dp[i % 2][j] = max(dp[(i - 1) % 2][j], dp[i % 2][j - 1]);
        }
      }
    }

    cout << dp[n % 2][m] << "\n";
  }

  return 0;
}