Pagini recente » Cod sursa (job #2877860) | Cod sursa (job #2523478) | Cod sursa (job #3182685) | 3oredechin | Cod sursa (job #2558068)
#include <bits/stdc++.h>
using namespace std;
const int N = 1009, SIG = 27;
int dp[N][N];
vector < int > poza[SIG], pozb[SIG];
int main()
{
ifstream fin("seif.in");
ofstream fout("seif.out");
ios_base::sync_with_stdio(NULL);
fin.tie(0);
fout.tie(0);
int t, k;
fin >> t;
while (t--) {
string a, b;
fin >> k >> a >> b;
int n = a.size();
int m = b.size();
for (int i = n - 1; i >= 0; --i) {
for (int j = m - 1; j >= 0; --j) {
if (a[i] == b[j])
dp[i][j] = 1 + dp[i + 1][j + 1];
else
dp[i][j] = max(dp[i + 1][j], dp[i][j + 1]);
}
}
string ans("");
for (int i = 0; i < SIG; ++i)
poza[i].clear();
for (int i = 0; i < n; ++i)
poza[a[i] - 'A'].push_back(i);
for (int i = 0; i < SIG; ++i)
pozb[i].clear();
for (int i = 0; i < m; ++i)
pozb[b[i] - 'A'].push_back(i);
int pa(-1), pb(-1);
bool ok(1);
while (ok) {
ok = 0;
for (int i = SIG - 1; i >= 0; --i) {
int pas = 1<<9, ra(-1);
while (pas) {
if (ra + pas < poza[i].size() && pa >= poza[i][ra + pas])
ra += pas;
pas >>= 1;
}
if (ra + 1 >= poza[i].size() || poza[i][ra + 1] <= pa)
continue;
pas = 1<<9;
int rb(-1);
while (pas) {
if (rb + pas < pozb[i].size() && pb >= pozb[i][rb + pas])
rb += pas;
pas >>= 1;
}
if (rb + 1 >= pozb[i].size() || pozb[i][rb + 1] <= pb)
continue;
if (dp[poza[i][ra + 1]][pozb[i][rb + 1]] + ans.size() < k)
continue;
pa = poza[i][ra + 1];
pb = pozb[i][rb + 1];
ans.push_back('A' + i);
ok = 1;
break;
}
}
assert(ans.size() >= k);
fout << ans << '\n';
}
return 0;
}