#include<cstdio> #include<algorithm> #include<iostream> #define S 3007 using namespace std; int dp[S][S]; int main(void){ int n,m,q; scanf("%d %d %d",&n,&m,&q); string s,s2; cin >> s >> s2; int a,b,c,d; while(q--){ scanf("%d %d %d %d",&a,&b,&c,&d); for(int i = a; i <= b;i++){ dp[i][c-1] = 0; } for(int j = c; j <= d;j++){ dp[a-1][j] = 0; } dp[a-1][c-1] = 0; for(int i = a; i <= b;i++){ for(int j = c; j <= d;j++){ dp[i][j] = max(dp[i-1][j],dp[i][j-1]); if(s[i-1] == s2[j-1]){ dp[i][j] = max(dp[i][j],dp[i-1][j-1]+1); } } } printf("%d\n",dp[b][d]); } 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 | #include<cstdio> #include<algorithm> #include<iostream> #define S 3007 using namespace std; int dp[S][S]; int main(void){ int n,m,q; scanf("%d %d %d",&n,&m,&q); string s,s2; cin >> s >> s2; int a,b,c,d; while(q--){ scanf("%d %d %d %d",&a,&b,&c,&d); for(int i = a; i <= b;i++){ dp[i][c-1] = 0; } for(int j = c; j <= d;j++){ dp[a-1][j] = 0; } dp[a-1][c-1] = 0; for(int i = a; i <= b;i++){ for(int j = c; j <= d;j++){ dp[i][j] = max(dp[i-1][j],dp[i][j-1]); if(s[i-1] == s2[j-1]){ dp[i][j] = max(dp[i][j],dp[i-1][j-1]+1); } } } printf("%d\n",dp[b][d]); } return 0; } |