View Code of Problem 2591

#include<stdio.h>
#include<string.h> 
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		char s1[1000],s2[1000],a[1000];
		char c;
		int m,n,k=0,i,j,t;
		scanf("%s",s1);
		scanf("%s",s2);
		n=strlen(s1);
		m=strlen(s2);
		for(i=0;i<n;i++)
		{
			for(j=0;j<m;j++)
			{
				if(s1[i]==s2[j]) 
				{
					a[k++]=s2[j];
					s2[j]='*';
					break;
				}
			}
		}
		a[k]='\0';
		if(k==0) printf("-1\n");
		else{
	    	for(i=0;i<k-1;i++)
		   {
		     	for(j=0;j<k-1;j++)
		  	   {
		    	  if(a[j]>a[j+1]) 
			     {
				   c=a[j];
				   a[j]=a[j+1];
				   a[j+1]=c;
			     }
		       }
	       }
	        printf("%s\n",a);
	   }
	}
	return 0;
}

Double click to view unformatted code.


Back to problem 2591