Source Code
#include "bits/stdc++.h"
 
using namespace std;
typedef long long ll;

void solve() {
    string a, b;
    cin >> a >> b;

    int n = a.size();
    int m = b.size();
    int dif = abs(n - m) / 2;
    
    if(a == b) {
        cout << a << '\n';
        return;
    }

    deque<char> prea, preb, sufa, sufb, resta, restb;
    for(auto c : a) {
        resta.push_back(c);
    }
    for(auto c : b) {
        restb.push_back(c);
    }

    int i = 0, j = 0;
    if(n > m) {
        while (i < dif) {
            prea.push_back(a[i]);
            sufa.push_front(a[n - i - 1]);
            resta.pop_front();
            resta.pop_back();
            i++;
        }
    }
    
    if(m > n) {
        while (j < dif) {
            preb.push_back(b[j]);
            sufb.push_front(b[m - j - 1]);
            restb.pop_front();
            restb.pop_back();
            j++;
        }
    }


    if(prea == sufa && preb == sufb && resta == restb) {
        for(auto c : sufb) {
            cout << c;
        }
        for(auto c : resta) {
            cout << c;
        }
        for(auto c : prea) {
            cout << c;
        }
        return;
    }

    for(; j <= m / 2; j++) {
        prea.push_back(a[i]);
        preb.push_back(b[j]);
        sufa.push_front(a[n - i - 1]);
        sufb.push_front(b[m - j - 1]);
        if(resta.size() >= 2) {
            resta.pop_front();
            restb.pop_front();
            resta.pop_back();
            restb.pop_back();
        }
        i++;

        if(prea == sufa && preb == sufb && resta == restb) {
            for(auto c : sufb) {
                cout << c;
            }
            for(auto c : resta) {
                cout << c;
            }
            for(auto c : prea) {
                cout << c;
            }
            return;
        }
    }
    cout << -1 << '\n';
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie();
    int t = 1;
    // cin >> t;
    while(t--) {
        solve();
    }
}
Copy
Right into Two OsamaX01
GNU G++17
0 ms
404 KB
Wrong Answer