#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
string s1,s2;
int dp[3010][3010];
inline int f(int x1,int x2,int y1,int y2)
{
for(int i=x1;i<=x2;i++)
{
for(int j=y1;j<=y2;j++)
{
if(s1[i-1]==s2[j-1])
dp[i][j]=dp[i-1][j-1]+1;
else
dp[i][j]=max(dp[i][j-1],dp[i-1][j]);
}
}
int ans=dp[x2][y2];
for(int i=x1;i<=x2;i++)
{
for(int j=y1;j<=y2;j++)
dp[i][j]=0;
}
return ans;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n,m,q;
cin>>n>>m>>q;
cin>>s1;
cin>>s2;
while(q--)
{
int a,b,c,d;
cin>>a>>b>>c>>d;
cout<<f(a,b,c,d)<<"\n";
}
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 | #include<bits/stdc++.h> #define fi first #define se second using namespace std; string s1,s2; int dp[3010][3010]; inline int f(int x1,int x2,int y1,int y2) { for(int i=x1;i<=x2;i++) { for(int j=y1;j<=y2;j++) { if(s1[i-1]==s2[j-1]) dp[i][j]=dp[i-1][j-1]+1; else dp[i][j]=max(dp[i][j-1],dp[i-1][j]); } } int ans=dp[x2][y2]; for(int i=x1;i<=x2;i++) { for(int j=y1;j<=y2;j++) dp[i][j]=0; } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n,m,q; cin>>n>>m>>q; cin>>s1; cin>>s2; while(q--) { int a,b,c,d; cin>>a>>b>>c>>d; cout<<f(a,b,c,d)<<"\n"; } return 0; } |
English