Source Code
#include <iostream>
#include <algorithm>
#include <iterator>
#include <math.h>
#include <vector>
#include <set>
#include <map>
using namespace std;
vector<long long>v;
long long n;
int main() {
	string s1, s2, x1, x2, x3, x4;
	int i, j;
	cin >> s1 >> s2;
	x1 = x2 = x3 = x4 = "";
	bool suf, pre;
	suf = pre = 0;
	i = s1.size() - 1;
	j = s2.size() - 1;
	while (s1!=s2) {
		if (i == -1 || j == -1)break;
		x1 = s2.substr(j, s2.size()) + s1.substr(s1.size()-i,s1.size());
		x2 = s2.substr(0, j) + s1.substr(0, s1.size() - i);
		x3 = s1.substr(0,i) + s2.substr(0, s2.size() - j );
		x4 = s1.substr(i , s1.size()) + s2.substr(s2.size() - j, s2.size());
		i--;
		j--;
		if (x2 == x1) {
			cout << x2;
			return 0;
		}
		if (x3 == x4 ) {
			cout << x3;
			return 0;
		}
	}
	if (s1 != s2)cout << -1;

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