View Code of Problem 2591

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int cmp(const void* a,const void *b)
{
	return *(char *)a-*(char *)b;
}
int main()
{
	int t;
	scanf("%d ",&t);
		
				for(int i=0;i<t;i++)
				{
					char a[1000],b[1000];
					gets(a);gets(b);
					int j=0,k=0;
					int flag=0;
					qsort(a,strlen(a),sizeof(char),cmp);
					qsort(b,strlen(b),sizeof(char),cmp);
	//				printf("%s\n%s\n",a,b);
					while(j<strlen(a)&&k<strlen(b))
					{
						if(a[j]==b[k])
						{
							printf("%c",a[j]);
							j++;k++;
							flag++;
						}
						else if(a[j]<b[k])
						{
							j++;
						}
						else if(a[j]>b[k])
						{
							k++;
						}
					}
						if(flag==0)
						{
							printf("-1\n");	
						}
						else
							{
								printf("\n");
							}
					}
 } 

Double click to view unformatted code.


Back to problem 2591