Source Code
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define PI 3.14159265
#define fast ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
typedef pair<ll,ll> pli;
typedef pair<int,int> pii;
typedef pair<ld,ld> pld;
ll MOD=1000000007 ;
ll MX=1000000000000000;

string a,b;
int main()
{
    fast;
    //freopen("input.txt","r",stdin);
    cin>>a;
    cin>>b;
    ll n=a.size();
    ll m=b.size();
    if((n+m)%2==1)
    {
        cout<<"-1\n";
    }
    else
    {
        string res="";
        string pref="";
        string suff="";
        ll stx,sty;
        if(n>m)
        {
            stx=(n-m)/2;
            sty=0;
        }
        else
        {
            stx=0;
            sty=(m-n)/2;
        }
        for(int i=0;i<stx;i++)
        {
            pref+=a[i];
        }
        for(int j=m-1;j>=m-sty;j--)
        {
            suff=b[j]+suff;
        }
        string a1=suff;
        for(int i=stx;i<n;i++)
            a1+=a[i];
        string b1="";
        for(int j=0;j<m-sty;j++)
            b1+=b[j];
        b1+=pref;
        res=a1;
        for(int i=0;i<a1.size();i++)
        {
            if(a1[i]!=b1[i])
                res="";
        }
        if(res!="")
        {
            cout<<res<<endl;
        }
        else
        {
            for(int i=0;i<n;i++)
            {
                if(i+stx>=n)
                    continue;
                if(m-1-i-sty<0)
                    continue;
                pref+=a[i+stx];
                suff=b[m-1-i-sty]+suff;
                a1=suff;
                for(int j=i+stx+1;j<n;j++)
                    a1+=a[j];
                b1="";
                for(int j=0;j<m-i-1-sty;j++)
                    b1+=b[j];
                b1+=pref;
                res=a1;
                for(int j=0;j<a1.size();j++)
                {
                    if(a1[j]!=b1[j])
                        res="";
                }
                if(res!="")
                    break;
            }
            if(res!="")
                cout<<res<<endl;
            else
                cout<<"-1\n";
        }


    }

    return 0;
}
Copy
Right into Two Mr_Programmer_98
GNU G++17
198 ms
1.1 MB
Accepted