#include<bits/stdc++.h>
using namespace std;
int NWP[3003][3003];
void lcs(string s1, string s2)
{
int n=s1.length();
int m=s2.length();
for(int i = 0; i <n; i++ )
for(int j = 0; j <m; j++ )
if(s1 [ i ]==s2 [ j ])
NWP[ i + 1 ][ j + 1 ]=NWP[i][j]+1;
else
NWP[i+1][j+1]=max(NWP[i+1][j], NWP[i][j+1]);
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m, q;
cin>>n>>m>>q;
string A, B;
cin>>A>>B;
while(q--)
{
int a, b, c, d;
cin>>a>>b>>c>>d;
string s1=A.substr(a-1, b-a+1);
string s2=B.substr(c-1, d-c+1);
lcs(s1, s2);
cout<<NWP[s1.length()][s2.length()]<<"\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 | #include<bits/stdc++.h> using namespace std; int NWP[3003][3003]; void lcs(string s1, string s2) { int n=s1.length(); int m=s2.length(); for(int i = 0; i <n; i++ ) for(int j = 0; j <m; j++ ) if(s1 [ i ]==s2 [ j ]) NWP[ i + 1 ][ j + 1 ]=NWP[i][j]+1; else NWP[i+1][j+1]=max(NWP[i+1][j], NWP[i][j+1]); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m, q; cin>>n>>m>>q; string A, B; cin>>A>>B; while(q--) { int a, b, c, d; cin>>a>>b>>c>>d; string s1=A.substr(a-1, b-a+1); string s2=B.substr(c-1, d-c+1); lcs(s1, s2); cout<<NWP[s1.length()][s2.length()]<<"\n"; } return 0; } |
English