Pagini recente » Cod sursa (job #1611561) | Cod sursa (job #572141) | Cod sursa (job #1080030) | Cod sursa (job #689206) | Cod sursa (job #2051813)
#include <bits/stdc++.h>
#define maxN 1002
#define maxA 26
using namespace std;
FILE *fin = freopen("seif.in", "r", stdin);
FILE *fout = freopen("seif.out", "w", stdout);
/* --------------------------- */
int t, k, N[2];
char a[2][maxN];
/* --------------------------- */
int dp[maxN][maxN], pos[2][maxN][maxA];
/* --------------------------- */
int len;
char ans[maxN];
void compLCS()
{
memset(dp, 0, sizeof(dp));
for (int i = N[0]; i >= 1; -- i)
for (int j = N[1]; j >= 1; -- j)
if (a[0][i] == a[1][j])
dp[i][j] = dp[i + 1][j + 1] + 1;
else
dp[i][j] = max(dp[i + 1][j], dp[i][j + 1]);
}
void compPos()
{
memset(pos, 0, sizeof(pos));
for (int t = 0; t < 2; ++ t)
{
for (int i = N[t]; i >= 1; -- i)
for (int ch = 0; ch < 26; ++ ch)
if (a[t][i] - 'A' == ch)
pos[t][i][ch] = i;
else
pos[t][i][ch] = pos[t][i + 1][ch];
}
}
void compAns()
{
int i = 1, j = 1;
for (len = 1; len <= min(N[0], N[1]) && i <= N[0] && j <= N[1]; ++ len)
{
for (int ch = 25; ch >= 0; -- ch)
if (i <= N[0] && j <= N[1] && (pos[0][i][ch] && pos[1][j][ch]) &&
(len - 1 + dp[pos[0][i][ch]][pos[1][j][ch]] >= k))
{
ans[len] = ch + 'A';
i = pos[0][i][ch] + 1;
j = pos[1][j][ch] + 1;
break;
}
}
}
int main()
{
scanf("%d", &t);
while (t --)
{
scanf("%d", &k);
for (int t = 0; t < 2; ++ t)
{
scanf("\n%s", a[t] + 1);
N[t] = strlen(a[t] + 1);
}
compLCS();
compPos();
compAns();
for (int i = 1; i < len; ++ i)
printf("%c", ans[i]);
printf("\n");
}
return 0;
}