Pagini recente » Cod sursa (job #1772444) | Cod sursa (job #1308445) | Cod sursa (job #1783417) | Cod sursa (job #69814) | Cod sursa (job #134400)
Cod sursa(job #134400)
using namespace std;
#include <stdio.h>
#include <string.h>
#define alfa 26
#define Lmax 1024
#define infinit 0x3f3f3f3f
int T,K,N,M;
int a[Lmax][Lmax];
int p1[Lmax][alfa+1],p2[Lmax][alfa+1];
char s1[Lmax],s2[Lmax];
inline int maxim(int a,int b) { return a>b?a:b; }
void solve()
{
int i,j,x,y,lx,ly;
for (i=0;i<=N;++i)
a[i][M]=0;
for (i=0;i<=M;++i)
a[N][i]=0;
for (i=N-1;i>=0;--i)
for (j=M-1;j>=0;--j)
if (s1[i]==s2[j]) a[i][j]=a[i+1][j+1]+1;
else a[i][j]=maxim(a[i][j+1],a[i+1][j]);
memset(p1[N],0x3f,sizeof(p1[N]));
memset(p2[M],0x3f,sizeof(p2[M]));
for (i=N-1;i>=0;--i)
{
memcpy(p1[i],p1[i+1],sizeof(p1[i+1]));
p1[i][s1[i]-'A']=i;
}
for (i=M-1;i>=0;--i)
{
memcpy(p2[i],p2[i+1],sizeof(p2[i+1]));
p2[i][s2[i]-'A']=i;
}
lx=ly=0;
for (i=1;i<=K;++i)
{
for (j=alfa;j>=0;--j)
{
x=p1[lx][j];
y=p2[ly][j];
if (x+y>=infinit) continue;
if (a[x+1][y+1]+i>=K)
{
lx=x+1;
ly=y+1;
printf("%c",j+'A');
break;
}
}
}
while (x+y<infinit)
{
for (j=alfa;j>=0;--j)
{
x=p1[lx][j];
y=p2[ly][j];
if (x+y>=infinit) continue;
lx=x+1;
ly=y+1;
printf("%c",j+'A');
}
}
printf("\n");
}
int main()
{
freopen("seif.in","r",stdin);
freopen("seif.out","w",stdout);
scanf("%d\n",&T);
while (T)
{
--T;
scanf("%d\n",&K);
scanf("%s",&s1);
N=strlen(s1);
scanf("%s",&s2);
M=strlen(s2);
solve();
}
fclose(stdin);
fclose(stdout);
return 0;
}