// Marcin Knapik // Potyczki Algorytmiczne - zadanie A dzień 4 #pragma GCC optimize "O3" #include<bits/stdc++.h> using namespace std; #define f first #define s second #define pb push_back #define all(s) s.begin(), s.end() #define sz(s) (int)s.size() #define vi vector<int> #define vvi vector<vi> #define ii pair<int, int> #define vii vector<ii> #define vvii vector<vii> #define ll long long const int N = 1e6 + 7; const int INF = 1e9 + 7; const int mod = 1e9 + 7; int dp[6007][6007]; string s, t; int main(){ ios::sync_with_stdio(0); cin.tie(0); int n, m, q; cin >> n >> m >> q; cin >> s >> t; while(q--){ int a, b, c, d; cin >> a >> b >> c >> d; for(int i = a - 1; i <= b; i++) dp[i][c - 1] = 0; for(int i = c - 1; i <= d; i++) dp[a - 1][i] = 0; for(int i = a; i <= b; i++) for(int j = c; j <= d; j++){ if(s[i - 1] == t[j - 1]) dp[i][j] = dp[i - 1][j - 1] + 1; else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } cout << dp[b][d] << '\n'; } }
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 | // Marcin Knapik // Potyczki Algorytmiczne - zadanie A dzień 4 #pragma GCC optimize "O3" #include<bits/stdc++.h> using namespace std; #define f first #define s second #define pb push_back #define all(s) s.begin(), s.end() #define sz(s) (int)s.size() #define vi vector<int> #define vvi vector<vi> #define ii pair<int, int> #define vii vector<ii> #define vvii vector<vii> #define ll long long const int N = 1e6 + 7; const int INF = 1e9 + 7; const int mod = 1e9 + 7; int dp[6007][6007]; string s, t; int main(){ ios::sync_with_stdio(0); cin.tie(0); int n, m, q; cin >> n >> m >> q; cin >> s >> t; while(q--){ int a, b, c, d; cin >> a >> b >> c >> d; for(int i = a - 1; i <= b; i++) dp[i][c - 1] = 0; for(int i = c - 1; i <= d; i++) dp[a - 1][i] = 0; for(int i = a; i <= b; i++) for(int j = c; j <= d; j++){ if(s[i - 1] == t[j - 1]) dp[i][j] = dp[i - 1][j - 1] + 1; else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } cout << dp[b][d] << '\n'; } } |