Source Code
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define Test int T;cin>>T;while(T--)
#define PI acos(-1)
#define endl "\n"
#define fx(x) fixed<<setprecision(x)
#define sz(s) (int)s.size()
#define all(v) (v).begin(),(v).end()
#define allr(v) (v).rbegin(),(v).rend()
#define mem(a,n) memset((a),n,sizeof (a))
#define INF 1e18
#define ii pair<ll,ll>
ll gcd(ll x,ll y){return(!y?x:gcd(y,x%y));}
ll lcm(ll x,ll y){return x/gcd(x,y)*y;}
void file(){
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#else
    //freopen("conan.in", "r", stdin);
    //freopen("bridges.out", "w", stdout);
#endif
}
void fast(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    file();
}
int dx[]= {0,0,1,-1};
int dy[]= {-1,1,0,0};
const double eps=1e-9;
const int mod=1e9+7;
const int N=1e6+5;
bool valid(string a,string b,string pre,string suf){
    reverse(all(suf));
    a.replace(a.begin(),a.begin()+sz(pre),suf);
    b.replace(b.end()-sz(suf),b.end(),pre);
    if(a==b){
        cout<<a<<endl;
        return 1;
    }
    return 0;
}
int main(){
    fast();
    string a,b;
    cin>>a>>b;
    if((sz(a)+sz(b))%2){
        cout<<-1<<endl;
        return 0;
    }
    string pre,suf;
    int i=0,j=sz(b)-1;
    for(i=0;i<(sz(a)-sz(b))/2;i++)
        pre+=a[i];
    for(int it=0;it<(sz(b)-sz(a))/2;it++,j--)
        suf+=b[j];
    //cout<<pre<<" "<<suf<<endl;
    if(valid(a,b,pre,suf)){
        return 0;
    }
    for(;i<sz(a)&&j>=0;i++,j--){
        pre+=a[i];suf+=b[j];
        if(valid(a,b,pre,suf)){
            return 0;
        }
    }
    cout<<-1<<endl;
    return 0;
}
Copy
Right into Two mahmoud_ashour
GNU G++17
8 ms
972 KB
Accepted