Cod sursa(job #138541)

Utilizator mariussMarius Telespan mariuss Data 18 februarie 2008 20:09:11
Problema Seif Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.2 kb
#include<stdio.h>
#include<string.h>
#define nmax 1010
int t,q,i,j,aux,pp1,pp2,k,v[nmax][nmax],l1,l2;
char a[nmax],b[nmax];
inline int max(int p,int q)
{
    if(p>q)
        return p;
    else
        return q;

}

int main()
{
	freopen("seif.in","r",stdin);
    freopen("seif.out","w",stdout);
    
    scanf("%d",&t);
    
    for(;t>0;t--)
    {
        scanf("%d\n",&k);
        
        scanf("%s\n",&a);
        scanf("%s\n",&b);
    
        l1=strlen(a);
        l2=strlen(b);
    
        memset(v,0,sizeof(v));

		for(i=l1-1;i>=0;i--)
            for(j=l2-1;j>=0;j--)
            {
                if(a[i]==b[j])
                    v[i][j]=v[i+1][j+1]+1;
                else
                    v[i][j]=max(v[i+1][j],v[i][j+1]);
			}

    
        pp1=0;
		pp2=0;

		i='Z';

		aux=1;
		while(k>0)
		{
			for(j=pp1;j<l1;j++) if(int (a[j])==i) break;
			for(q=pp2;q<l2;q++) if(int (b[q])==i) break;

			if(j==l1||q==l2) i--;
			else
			{

			if(v[j][q]>=k)
			{

				printf("%c",i);
				k=v[j][q]-1;
				pp1=j+1;
				pp2=q+1;
			}
			else i--;
			}
		}
		

        printf("\n");                
    }
    return 0;
    
}