Source Code
#include <bits/stdc++.h>
using namespace std;
    
int main (){

  #ifndef ONLINE_JUDGE
    freopen("SuhaibSawalha1","r",stdin);
  #endif

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  string s, w;
  cin >> s >> w;
  int n = s.size(), m = w.size();
  if ((n + m) & 1) {
    return !(cout << -1);
  }
  int sz = (n + m) / 2;
  for (int i = 0; i <= n; ++i) {
    if (sz - i > m || i > sz) {
      continue;
    }
    string k = w.substr(sz - i) + s.substr(i), t = w.substr(0, sz - i) + s.substr(0, i);
    if (k == t) {
      return !(cout << k);
    }
  }
  cout << -1;

  return 0;
}
Copy
Right into Two SuhaibSawalha1
GNU G++17
2 ms
928 KB
Accepted