Source Code
#include <bits/stdc++.h>
#define mk make_pair
#define fs first
#define sc second
using namespace std;
typedef long long ll;
typedef long double ld;
// please, read the question correctly (do you need set or multiset)???
const int N=5010; //check the limits, dummy
int a[N];
int n, m;
char s[N], c[N];
bool ch(string &s1, string &s2){
	assert(s1.size()==s2.size());
	for(int i=0; i<s1.size(); ++i){
		if(s1[i]!=s2[i])return 0;
	}
	return 1;
}
pair<int, string> check(int x){
	string st = "";
	for(int i=x; i<m; ++i){
		st +=c[i];
	}
	int y = (n+m)/2-x;
	if(y>n || y<0){
		return mk(0,"");
	}
	string st1 = "";
	for(int i=0; i<x; ++i){
		st1 += c[i];
	}
	for(int i=0; i<y; ++i){
		st1 += s[i];
	}
	for(int i=y; i<n; ++i){
		st += s[i];
	}
	if(ch(st,st1)){
		return mk(1,st);
	}
	return mk(0,"");
}
pair<int, string> solve(){
	for(int i=0; i<m; ++i){
		auto it = check(i+1);
		if(it.fs){
			return mk(1,it.sc);
		}
	}
	return mk(0,"");
}
int main(){
// int t;cin>>t;while(t--){

	scanf("%s%s",s,c);
	n = strlen(s);
	m = strlen(c);
	if((n+m)%2){
		cout<<-1<<endl;
		return 0;
	}
	auto it=solve();
	if(it.fs==0){
		printf("-1\n");
	}
	else{
		cout<<it.sc<<endl;
	}

// }
return 0;
}
Copy
Right into Two Zain
GNU G++17
89 ms
964 KB
Accepted