Source Code
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main() {

    string a, b;
    cin >> a >> b;

    if(a == b) return cout << a, 0;

    int n = a.length(), m = b.length();
    if((n+m)%2) return cout << -1, 0;

    int h = (n+m)/2;

    for(int i = 0; i < n; i++){
        string a1 = a.substr(0, i);
        string a2 = a.substr(i);
        int rem = a2.length();
        int wan = h - rem;
        if(wan > m) break;
        string b2 = b.substr(m-wan);
        string b1 = b.substr(0, m - b2.length());
        string one = b2+a2, tow = b1+a1;
        if(one == tow){
            a = one;
            b = tow;
            break;
        }
    }

    if(a == b) cout << a << endl;
    else cout << -1;

    return 0;
}
Copy
Right into Two أحمد الغضبان
GNU G++17
2 ms
748 KB
Runtime Error