Source Code
#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <string>
#include <iomanip>
#include <fstream>
#include <chrono>

using namespace std;

int main()
{
    char a[5000];
    char b[5000];
    int m = 0;
    int j = 0;
    int s = -1, p = -1;
    cin >> a;
    cin >> b;

    for (int i = 1; a[i] != '\0'; i++)
    {
        if (a[i] == a[0])
        {
            j = 1;
            p = i;
            while (a[i + j] != '\0')
            {
                if (a[j] != a[i + j])
                {
                    p = -1;
                }
                j++;
            }
        }
    }

    for (int i = 1; b[i] != '\0'; i++)
    {
        if (b[i] == b[0])
        {
            j = 1;
            s = i;
            while (b[i + j] != '\0')
            {
                if (b[j] != b[i + j])
                {
                    s = -1;
                }
                j++;
            }
        }
    }

    if (s == -1 || p == -1)
    {
        cout << -1 << endl;
    }
    else
    {
        for (int i = s; b[i] != '\0'; i++)
        {
            cout << b[i];
            m++;
        }

        for (int i = m; i < s; i++)
        {
            cout << a[i];
        }

        for (int i = p; a[i] != '\0'; i++)
        {
            cout << a[i];
        }
    }

    return 0;
}
Copy
Right into Two DirtyQWERTY
GNU G++17
1 ms
612 KB
Wrong Answer