Source Code
#include <bits/stdc++.h>
using namespace std;

int main() {
	string a,b;
    cin>>a>>b;
    cin>>a>>b;
    if((a.size()+b.size()) % 2 != 0) {
        cout<<-1<<endl;
        return 0;
    }

    int cutA = (a.size() + b.size()) / 2 - b.size();
    int cutB = 0;
    if(a.size() < b.size()) {
        cutA = 0;
        cutB = (a.size() + b.size())/2 - a.size();
    }

    while(cutA <= a.size() && cutB <= b.size()) {
        string prefixA = a.substr(0, cutA);
        string restA = a.substr(cutA);
        string suffixB = b.substr(b.size() - cutB);
        string restB = b.substr(0, b.size() - cutB);
        if(restB + prefixA == suffixB + restA) {
            cout<<(restB + prefixA)<<endl;
            return 0;
        }
        cutA++;
        cutB++;
    }
	
    cout<<-1<<endl;
	
	return 0;
}
Copy
Right into Two Mr-Spy
GNU G++17
3 ms
1.0 MB
Accepted