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 a[26] = { 0 };
		cin >> s1 >> s2;
		for (int i = 0; i < s1.size(); ++i) 
			for (int j = 0; j < s2.size(); ++j) 
				if (s1[i] == s2[j]) 
					a[s1[i] - 'A'] = 1;
		for (int i = 0; i < 26; ++i) {
			if (a[i]) {
				int sum = 0;
				for (int j = 0; j < s1.size(); ++j) {
					if (s1[j] == 'A' + i)
						++sum;
				}
				a[i] = sum;
			}
		}
		for (int i = 0; i < 26; ++i) {
			if (a[i]) {
				int sum = 0;
				for (int j = 0; j < s2.size(); ++j) {
					if (s2[j] == 'A' + i)
						++sum;
				}
				a[i] = sum < a[i]?sum:a[i];
			}
		}
		int flag = 0;
		for (int i = 0; i < 26; ++i) {
			while (a[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