#include <iostream>
#include <vector>
using namespace std;
void podciagi(vector<vector<int>>& tabela, string napis1, string napis2, int dlugosc1, int dlugosc2)
{
for (int i = 1; i <= dlugosc2; i++)
{
for (int j = 1; j <= dlugosc1; j++)
{
if(napis1[j - 1] == napis2[i - 1])
tabela[i][j] = tabela[i - 1][j - 1] + 1;
else if(tabela[i - 1][j] >= tabela[i][j - 1])
tabela[i][j] = tabela[i - 1][j];
else
tabela[i][j] = tabela[i][j - 1];
}
}
}
int main()
{
int dlugosc1, dlugosc2, liczbaZapytan;
cin >> dlugosc1 >> dlugosc2 >> liczbaZapytan;
string napis1, napis2;
cin >> napis1 >> napis2;
vector<vector<int>> tabela(dlugosc2 + 1, vector<int>(dlugosc1 + 1));
podciagi(tabela, napis1, napis2, dlugosc1, dlugosc2);
int a, b, c, d;
for (int i = 0; i < liczbaZapytan; i++)
{
cin >> a >> b >> c >> d;
if(napis1[a - 1] == napis2[c - 1])
cout << tabela[d][b] - tabela[c - 1][a - 1] << '\n';
else
cout << tabela[d][b] - tabela[c][a] << '\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 44 45 | #include <iostream> #include <vector> using namespace std; void podciagi(vector<vector<int>>& tabela, string napis1, string napis2, int dlugosc1, int dlugosc2) { for (int i = 1; i <= dlugosc2; i++) { for (int j = 1; j <= dlugosc1; j++) { if(napis1[j - 1] == napis2[i - 1]) tabela[i][j] = tabela[i - 1][j - 1] + 1; else if(tabela[i - 1][j] >= tabela[i][j - 1]) tabela[i][j] = tabela[i - 1][j]; else tabela[i][j] = tabela[i][j - 1]; } } } int main() { int dlugosc1, dlugosc2, liczbaZapytan; cin >> dlugosc1 >> dlugosc2 >> liczbaZapytan; string napis1, napis2; cin >> napis1 >> napis2; vector<vector<int>> tabela(dlugosc2 + 1, vector<int>(dlugosc1 + 1)); podciagi(tabela, napis1, napis2, dlugosc1, dlugosc2); int a, b, c, d; for (int i = 0; i < liczbaZapytan; i++) { cin >> a >> b >> c >> d; if(napis1[a - 1] == napis2[c - 1]) cout << tabela[d][b] - tabela[c - 1][a - 1] << '\n'; else cout << tabela[d][b] - tabela[c][a] << '\n'; } return 0; } |
English