#include <bits/stdc++.h> using namespace std; const int nax = 3 * 1e3 + 5; const int DLUGOSC_ALFABETU = 256; const int MAX_W = nax / 32; unsigned int S[DLUGOSC_ALFABETU + 5][MAX_W + 5]; unsigned int L[MAX_W + 5]; void dlugosc_LCS(int n, int m, string s, string p) { if(n == 0 || m == 0) { cout << "0\n"; return; } if(n < m) { swap(s, p); swap(n, m); } int w = (n + 31) >> 5; memset(S, 0, sizeof S); unsigned int set = 1; for(int i = 0, j = 0; i < n; i++) { S[(int) s[i]][j] |= set; set <<= 1; if(!set) { set++; j++; } } memset(L, 0, sizeof L); for(int i = 0; i < m; i++) { unsigned int b1 = 1; unsigned int b2 = 0; for(int j = 0; j < w; j++) { unsigned int u = L[j] | S[(int) p[i]][j]; unsigned int c = L[j] >> 31; unsigned int v = u - (L[j] << 1 | (b1 + b2)); b1 = c; b2 = (v > u); L[j] = u & (~v); } } int ans = 0; for(int i = 0; i < w; i++) { while(L[i]) { L[i] &= L[i] - 1; ans++; } } cout << ans << "\n"; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m, t; cin >> n >> m >> t; string pierwszy, drugi; cin >> pierwszy >> drugi; while(t--) { int L1, R1, L2, R2; cin >> L1 >> R1 >> L2 >> R2; dlugosc_LCS(R1 - L1 + 1, R2 - L2 + 1, pierwszy.substr(L1 - 1, R1 - L1 + 1), drugi.substr(L2 - 1, R2 - L2 + 1)); } return 0; }
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 | #include <bits/stdc++.h> using namespace std; const int nax = 3 * 1e3 + 5; const int DLUGOSC_ALFABETU = 256; const int MAX_W = nax / 32; unsigned int S[DLUGOSC_ALFABETU + 5][MAX_W + 5]; unsigned int L[MAX_W + 5]; void dlugosc_LCS(int n, int m, string s, string p) { if(n == 0 || m == 0) { cout << "0\n"; return; } if(n < m) { swap(s, p); swap(n, m); } int w = (n + 31) >> 5; memset(S, 0, sizeof S); unsigned int set = 1; for(int i = 0, j = 0; i < n; i++) { S[(int) s[i]][j] |= set; set <<= 1; if(!set) { set++; j++; } } memset(L, 0, sizeof L); for(int i = 0; i < m; i++) { unsigned int b1 = 1; unsigned int b2 = 0; for(int j = 0; j < w; j++) { unsigned int u = L[j] | S[(int) p[i]][j]; unsigned int c = L[j] >> 31; unsigned int v = u - (L[j] << 1 | (b1 + b2)); b1 = c; b2 = (v > u); L[j] = u & (~v); } } int ans = 0; for(int i = 0; i < w; i++) { while(L[i]) { L[i] &= L[i] - 1; ans++; } } cout << ans << "\n"; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m, t; cin >> n >> m >> t; string pierwszy, drugi; cin >> pierwszy >> drugi; while(t--) { int L1, R1, L2, R2; cin >> L1 >> R1 >> L2 >> R2; dlugosc_LCS(R1 - L1 + 1, R2 - L2 + 1, pierwszy.substr(L1 - 1, R1 - L1 + 1), drugi.substr(L2 - 1, R2 - L2 + 1)); } return 0; } |