View Code of Problem 2591

#include<iostream>
#include<algorithm>
using namespace std;
int main(void){
	int t;
	cin>>t;
	while(t--){
		string s1,s2,b;
		cin>>s1>>s2;
		for(int i=0;i<s1.length();i++){
			for(int j=0;j<s2.length();j++){
				if(s1[i]==s2[j]&&s2[j]!='*'){
					b+=s1[i];
					s2[j]='*';
					break;
				}
			}
		}
		if(b.length()==0) cout<<"-1";
		else{
			int l=b.length();
			for(int i=0;i<l;i++){
				for(int j=0;j<l;j++){
					if(b[i]<b[j]){
						char t=b[i];
						b[i]=b[j];
						b[j]=t;
					}
				}
			}
			for(int i=0;i<l;i++)
				cout<<b[i];
		}
		cout<<endl;
	}
}

Double click to view unformatted code.


Back to problem 2591