#include <bits/stdc++.h> using namespace std; int L[3001][3001]; int n,m,q; string s,t; void LCS(int s1,int s2,int t1,int t2){ int x = s2-s1+1, y = t2-t1+1; for(int i = 0; i<=x; i++){ for(int j = 0; j<=y; j++){ if(!j || !i) L[i][j] = 0; else if(s[s1+i-1] == t[t1+j-1]) L[i][j] = L[i-1][j-1] + 1; else L[i][j] = max(L[i][j-1], L[i-1][j]); } } cout<<L[x][y]<<"\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin>>n>>m>>q; cin>>s; cin>>t; int s1,s2,t1,t2; while(q--){ cin>>s1>>s2>>t1>>t2; LCS(s1-1,s2-1,t1-1,t2-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 | #include <bits/stdc++.h> using namespace std; int L[3001][3001]; int n,m,q; string s,t; void LCS(int s1,int s2,int t1,int t2){ int x = s2-s1+1, y = t2-t1+1; for(int i = 0; i<=x; i++){ for(int j = 0; j<=y; j++){ if(!j || !i) L[i][j] = 0; else if(s[s1+i-1] == t[t1+j-1]) L[i][j] = L[i-1][j-1] + 1; else L[i][j] = max(L[i][j-1], L[i-1][j]); } } cout<<L[x][y]<<"\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin>>n>>m>>q; cin>>s; cin>>t; int s1,s2,t1,t2; while(q--){ cin>>s1>>s2>>t1>>t2; LCS(s1-1,s2-1,t1-1,t2-1); } return 0; } |