#include <iostream> #include <map> using namespace std; int tab[5007][5007]; int main() { int n, m, q; cin>>n>>m>>q; string s, t; cin>>s>>t; s = " " + s; t = " " + t; for (int l = 0; l < q; l++) { int a, b, c, d; cin>>a>>b>>c>>d; for (int i = a; i <= b; i++) { for (int j = c; j <= d; j++) { tab[i][j] = 0; if (i > a) { tab[i][j] = max(tab[i - 1][j], tab[i][j]); } if (j > c) { tab[i][j] = max(tab[i][j - 1], tab[i][j]); } if (s[i] == t[j]) { if (i > a && j > c) { tab[i][j] = max(tab[i][j], tab[i - 1][j - 1] + 1); } else { tab[i][j] = max(tab[i][j], 1); } } //cout<<tab[i][j]<<" "; } //cout<<endl; } //return 0; cout<<tab[b][d]<<"\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 | #include <iostream> #include <map> using namespace std; int tab[5007][5007]; int main() { int n, m, q; cin>>n>>m>>q; string s, t; cin>>s>>t; s = " " + s; t = " " + t; for (int l = 0; l < q; l++) { int a, b, c, d; cin>>a>>b>>c>>d; for (int i = a; i <= b; i++) { for (int j = c; j <= d; j++) { tab[i][j] = 0; if (i > a) { tab[i][j] = max(tab[i - 1][j], tab[i][j]); } if (j > c) { tab[i][j] = max(tab[i][j - 1], tab[i][j]); } if (s[i] == t[j]) { if (i > a && j > c) { tab[i][j] = max(tab[i][j], tab[i - 1][j - 1] + 1); } else { tab[i][j] = max(tab[i][j], 1); } } //cout<<tab[i][j]<<" "; } //cout<<endl; } //return 0; cout<<tab[b][d]<<"\n"; } } |