View Code of Problem 2591

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int cmp(int a, int b) {
	return a > b ? 1 : 0;
}
int main() {
	int t;
	string s1, s2;
	cin >> t;
	while (t--) {
		int a1[26] = { 0 };
		int a2[26] = { 0 };
		cin >> s1 >> s2;
		for (int i = 0; i < s1.size(); ++i)
			++a1[s1[i] - 'A'];
		for (int i = 0; i < s2.size(); ++i)
			++a2[s2[i] - 'A'];
		for (int i = 0; i < 26; ++i) {
			if (a1[i] > a2[i])
				a1[i] = a2[i];
		}
		int flag = 0;
		for (int i = 0; i < 26; ++i) {
			while (a1[i]--) {
				cout << (char)('A' + i);
				flag = 1;
			}
		}
		if (!flag) cout << -1 << endl;
		else cout << endl;
	}
	return 0;
}

Double click to view unformatted code.


Back to problem 2591