Problem:
Score : 400 points

Problem Statement

We have a string s consisting of lowercase English letters.
Snuke can perform the following operation repeatedly:

Insert a letter x to any position in s of his choice, including the beginning and end of s.

Snuke's objective is to turn s into a palindrome.
Determine whether the objective is achievable. If it is achievable, find the minimum number of operations required.

Notes

A palindrome is a string that reads the same forward and backward.
For example, a, aa, abba and abcba are palindromes, while ab, abab and abcda are not.

Constraints

1 \leq |s| \leq 10^5

s consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s

Output

If the objective is achievable, print the number of operations required.
If it is not, print -1 instead.

Sample Input 1

xabxa

Sample Output 1

2

One solution is as follows (newly inserted x are shown in bold):

xabxa → xaxbxa → xaxbxax

Sample Input 2

ab

Sample Output 2

-1

No sequence of operations can turn s into a palindrome.

Sample Input 3

a

Sample Output 3

0

s is a palindrome already at the beginning.

Sample Input 4

oxxx

Sample Output 4

3

One solution is as follows:

oxxx → xoxxx → xxoxxx → xxxoxxx

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<string> vs;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vll = vector<vl>;
using vpll = vector<pll>;
struct Edge {
	ll to;
 	ll cost;
 };
using Graph = vector<vector<Edge>>;

#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
/***** define constant start *******/
const ll MOD = 1000000007;
const double PI = 3.1415926535897932;
const ll INF = LLONG_MAX;
string ch = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
/****** define constant end ********/
/****** define variable start ******/
string s;
/******* define variable end *******/
int solve() {
	cin >> s;
	string serasex = "";
	REP(i, s.size()){
		if(s.substr(i, 1) != "x"){
			serasex += s.substr(i, 1);
		}
	}
	ll hanbun = serasex.size() / 2;
	bool flag = true;
	REP(i, hanbun){
		if(serasex.substr(i, 1) != serasex.substr(serasex.size() - 1 - i,1)){
			flag = false;
		}
	}
	ll count = 0;
	ll ite;
	ll ite2;
	REP(i, s.size()){
		if(s.substr(i, 1) != "x"){
			count++;
			if(count == ceil((long double)serasex.size()/2)){
				ite = i;
			}
		}
	}
	count = 0;
	if(serasex.size() % 2 == 0){
		REP(i, s.size()){
			if(s.substr(i, 1) != "x"){
				count++;
				if(count == serasex.size()/2 + 1){
					ite2 = i;
				}
			}
		}
	}
	if(!flag){
		cout << -1 << endl;
	}else{
		if(serasex.size() % 2 == 0){
			ll ans = 0;
			bool flag2 = true;
			ll i = ite;
			ll j = ite2;
			while(flag2){
				if(i < 0 && j >= s.size()){
					flag2 = false;
					break;
				}
				if(i < 0){
					ans++;
					j++;
					continue;
				}
				if(j >= s.size()){
					ans++;
					i--;
					continue;
				}
				if(s.substr(i, 1) != s.substr(j, 1)){
					ans++;
					if(s.substr(i, 1) == "x"){
						i--;
					}else{
						j++;
					}
				}
				i--;
				j++;
			}
			cout << ans << endl;
		}else{
			ll ans = 0;
			bool flag2 = true;
			ll i = ite-1;
			ll j = ite+1;
			while(flag2){
				if(i < 0 && j >= s.size()){
					flag2 = false;
					break;
				}
				if(i < 0){
					ans++;
					j++;
					continue;
				}
				if(j >= s.size()){
					ans++;
					i--;
					continue;
				}
				if(s.substr(i, 1) != s.substr(j, 1)){
					ans++;
					if(s.substr(i, 1) == "x"){
						i--;
					}else{
						j++;
					}
				}
				i--;
				j++;
			}
			cout << ans << endl;
		}
	}
	return 0;
}

int main() {
	// clock_t start = clock();
	cout << std::fixed << std::setprecision(10);
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int end_solve = solve();
	/*
	clock_t end = clock();
	const double time = static_cast<double>(end - start) / CLOCKS_PER_SEC *
	1000.0; printf("time %lf[ms]\n", time);
	*/

	return end_solve;
}