#include <bits/stdc++.h> using namespace std; const int S = 10000; char buf[S]; size_t pos, len; inline char next_char() { if(pos == len) pos = 0, len = fread(buf, 1, S, stdin); return buf[pos++]; } inline int rd() { int c = next_char(), x = 0; while(c < 33) c = next_char(); for(; 48 <= c && c <= 57; c = next_char()) x = x * 10 + c - 48; return x; } inline void rd(char *s) { int c = next_char(); while(c < 33) c = next_char(); for(; 'a' <= c && c <= 'z'; c = next_char()) *(s++) = char(c); } int n, m, q, dp[3005][3005]; char t[3005], s[3005]; int main() { n = rd(), m = rd(), q = rd(); rd(s + 2); rd(t + 2); while(q--) { int I = rd(), J = rd(), K = rd(), L = rd(); for(int i = 1; i <= J - I + 1; i++) { for(int j = 1; j <= L - K + 1; j++) { if(s[i + I] == t[j + K]) dp[i][j] = dp[i - 1][j - 1] + 1; else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } printf("%d\n", dp[J - I + 1][L - K + 1]); } }
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 | #include <bits/stdc++.h> using namespace std; const int S = 10000; char buf[S]; size_t pos, len; inline char next_char() { if(pos == len) pos = 0, len = fread(buf, 1, S, stdin); return buf[pos++]; } inline int rd() { int c = next_char(), x = 0; while(c < 33) c = next_char(); for(; 48 <= c && c <= 57; c = next_char()) x = x * 10 + c - 48; return x; } inline void rd(char *s) { int c = next_char(); while(c < 33) c = next_char(); for(; 'a' <= c && c <= 'z'; c = next_char()) *(s++) = char(c); } int n, m, q, dp[3005][3005]; char t[3005], s[3005]; int main() { n = rd(), m = rd(), q = rd(); rd(s + 2); rd(t + 2); while(q--) { int I = rd(), J = rd(), K = rd(), L = rd(); for(int i = 1; i <= J - I + 1; i++) { for(int j = 1; j <= L - K + 1; j++) { if(s[i + I] == t[j + K]) dp[i][j] = dp[i - 1][j - 1] + 1; else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } printf("%d\n", dp[J - I + 1][L - K + 1]); } } |