#include<bits/stdc++.h> using namespace std; #define st first #define nd second struct Q { int a,b,c,d,i; friend bool operator<(const Q&a, const Q&b) { if(a.a == b.a){ if(a.b == b.b) { if(a.c == b.c) { if(a.d == b.d) return a.i < b.i; return a.d > b.d; } return a.c > b.c; } return a.b < b.b; } return a.a < b.a; } }; const int MAXN = 3001; short dp[MAXN][MAXN]; int32_t main(){ ios::sync_with_stdio(false); cin.tie(0); int q; cin >> q >> q >> q; string a,b; cin >> a >> b; vector<Q> que(q); for(int i=0;i<q;i++) { cin >> que[i].a >> que[i].c >> que[i].b >> que[i].d; que[i].a--; que[i].b--; que[i].c--; que[i].d--; que[i].i = i; } sort(que.begin(),que.end()); Q pv = {-1,-1,-1,-1,-1}; int D = 0; vector<short> ans(q); for(auto x:que) { if(x.a != pv.a || x.b != pv.b) D = x.a; for(int i=D;i<=x.c;i++,D++) { for(int j=x.b;j<=x.d;j++) { if(a[i] == b[j]) dp[i][j] = (i == x.a || j == x.b ? 1 : dp[i-1][j-1]+1); else dp[i][j] = max((i == x.a ? 0 : dp[i-1][j]),(j == x.b ? 0 : dp[i][j-1])); } } ans[x.i] = dp[x.c][x.d]; pv = x; } for(auto x:ans) cout<<x<<"\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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | #include<bits/stdc++.h> using namespace std; #define st first #define nd second struct Q { int a,b,c,d,i; friend bool operator<(const Q&a, const Q&b) { if(a.a == b.a){ if(a.b == b.b) { if(a.c == b.c) { if(a.d == b.d) return a.i < b.i; return a.d > b.d; } return a.c > b.c; } return a.b < b.b; } return a.a < b.a; } }; const int MAXN = 3001; short dp[MAXN][MAXN]; int32_t main(){ ios::sync_with_stdio(false); cin.tie(0); int q; cin >> q >> q >> q; string a,b; cin >> a >> b; vector<Q> que(q); for(int i=0;i<q;i++) { cin >> que[i].a >> que[i].c >> que[i].b >> que[i].d; que[i].a--; que[i].b--; que[i].c--; que[i].d--; que[i].i = i; } sort(que.begin(),que.end()); Q pv = {-1,-1,-1,-1,-1}; int D = 0; vector<short> ans(q); for(auto x:que) { if(x.a != pv.a || x.b != pv.b) D = x.a; for(int i=D;i<=x.c;i++,D++) { for(int j=x.b;j<=x.d;j++) { if(a[i] == b[j]) dp[i][j] = (i == x.a || j == x.b ? 1 : dp[i-1][j-1]+1); else dp[i][j] = max((i == x.a ? 0 : dp[i-1][j]),(j == x.b ? 0 : dp[i][j-1])); } } ans[x.i] = dp[x.c][x.d]; pv = x; } for(auto x:ans) cout<<x<<"\n"; } |