Problem p03569 (C++)

Buggy submission (status: Runtime Error):
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}

typedef long long int64;
typedef pair<int, int> ii;
const int INF = 1 << 30;
const int MOD = 1e9 + 7;

const int N = 1e5 + 10;
char s[N];
char t[N];

int main() {
  scanf("%s", s);
  int m = 0, n = strlen(s);
  vector<int> A;
  for (int i = 0; s[i]; ++i) {
    if (s[i] != 'x') {
      t[m++] = s[i];
      A.push_back(i);
    }
  }
  bool found = false;
  for (int i = 0; i < m / 2; ++i) {
    if (t[i] != t[m - 1 - i]) {
      found = true;
      break;
    }
  }
  if (found) {
    puts("-1");
    return 0;
  }
  // for (auto& it : A) {
  //   cout << it << " ";
  // }
  // cout << endl;
  int ret = 0;
  if (m % 2 == 1) {
    int mid = m / 2;
    for (int i = 0; i < m / 2; ++i) {
      int x = abs(A[mid + i + 1] - A[mid + i]);
      int y = abs(A[mid - i - 1] - A[mid - i]);
      ret += abs(x - y);
    }
  } else {
    int u = m / 2 - 1, v = u + 1;
    for (int i = 0; i < m / 2 - 1; ++i) {
      int x = abs(A[u - i - 1] - A[u - i]);
      int y = abs(A[v + i + 1] - A[v + i]);
      ret += abs(x - y);
    }
  }
  int x = A[0], y = n - 1 - A[m - 1];
  ret += abs(x - y);
  printf("%d\n", ret);
  return 0;
}


Fixed submission:
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}

typedef long long int64;
typedef pair<int, int> ii;
const int INF = 1 << 30;
const int MOD = 1e9 + 7;

const int N = 1e5 + 10;
char s[N];
char t[N];

int main() {
  scanf("%s", s);
  int m = 0, n = strlen(s);
  vector<int> A;
  for (int i = 0; s[i]; ++i) {
    if (s[i] != 'x') {
      t[m++] = s[i];
      A.push_back(i);
    }
  }
  if (m == 0) {
    puts("0");
    return 0;
  }
  bool found = false;
  for (int i = 0; i < m / 2; ++i) {
    if (t[i] != t[m - 1 - i]) {
      found = true;
      break;
    }
  }
  if (found) {
    puts("-1");
    return 0;
  }
  // for (auto& it : A) {
  //   cout << it << " ";
  // }
  // cout << endl;
  int ret = 0;
  if (m % 2 == 1) {
    int mid = m / 2;
    for (int i = 0; i < m / 2; ++i) {
      int x = abs(A[mid + i + 1] - A[mid + i]);
      int y = abs(A[mid - i - 1] - A[mid - i]);
      ret += abs(x - y);
    }
  } else {
    int u = m / 2 - 1, v = u + 1;
    for (int i = 0; i < m / 2 - 1; ++i) {
      int x = abs(A[u - i - 1] - A[u - i]);
      int y = abs(A[v + i + 1] - A[v + i]);
      ret += abs(x - y);
    }
  }
  int x = A[0], y = n - 1 - A[m - 1];
  ret += abs(x - y);
  printf("%d\n", ret);
  return 0;
}