Source Code
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
#define all(v) v.begin(), v.end()
#define pb push_back    
#define sz(x) (int)(x).size()
const int N = 3e5 + 5;
void solve() {
    string a, b;
    cin >> a >> b;
    if(a == b) { 
        cout << a;
        return;
    }
    int n = sz(a), m = sz(b);
    for(int i = n; i > 0; --i) { 
        for(int j = m; j > 0; --j) { 
            string aa = b.substr(j, m) + a.substr(i, n);
            string bb = b.substr(0, j) + a.substr(0, i);
            if(aa == bb) {
                cout << aa;
                return;
            }
        }
    }
    cout << -1;
}
int main() {
    cin.tie(0)->sync_with_stdio(0);
    int t = 1;
    // scanf("%d",&t);
    while(t--)
        solve();
}
Copy
Right into Two noomaK
GNU G++17
1100 ms
628 KB
Time Limit Exceeded