Problem p03569 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define SORT(name) sort(name.begin(), name.end())
#define ZERO(p) memset(p, 0, sizeof(p))
#define MINUS(p) memset(p, -1, sizeof(p))
#if 1
#define DBG(fmt, ...) printf(fmt, __VA_ARGS__)
#else
#define DBG(fmt, ...)
#endif

#define MOD 1000000007
#define INF 1000000000
#define MAX_N 100010

string S;
string noX;
vector<int> Pos;

// 回文なら true
bool isKai(string str) {
  int r, l;
  if (str.length() % 2 == 0) {
    r = str.length() / 2;
    l = r - 1;
  } else {
    r = str.length() / 2 + 1;
    l = str.length() / 2 - 1;
  }
  while (r < str.length() && l >= 0) {
    if (str[l] != str[r]) {
      return false;
    }
    r++;
    l--;
  }
  return true;
}

int main() {
  cin >> S;
  bool isX = false;
  REP(i, S.length()) {
    if (S[i] == 'x') {
      isX = true;
    } else {
      noX += S[i];
      Pos.push_back(i);
    }
  }
  if (!isX) {
    if (isKai(S)) {
      printf("0\n");
    } else {
      printf("-1\n");
    }
    return 0;
  }
  if (!isKai(noX)) {
    printf("-1\n");
    return 0;
  }
  int r, l, ans = 0;
  if (noX.length() % 2 == 0) {
    r = noX.length() / 2;
    l = Pos[r - 1];
    r = Pos[r];
  } else {
    r = Pos[noX.length() / 2] + 1;
    l = Pos[noX.length() / 2] - 1;
  }
  // DBG("r: %d l:%d\n", r, l);
  while (r < S.length() || l >= 0) {
    if (S[r] == 'x' && S[l] != 'x') {
      ans++;
      r++;
    } else if (S[r] != 'x' && S[l] == 'x') {
      ans++;
      l--;
    } else if (r >= S.length() && l >= 0) {
      ans++;
      l--;
    } else if (r < S.length() && l < 0) {
      ans++;
      r++;
    } else {
      r++;
      l--;
    }
  }
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define SORT(name) sort(name.begin(), name.end())
#define ZERO(p) memset(p, 0, sizeof(p))
#define MINUS(p) memset(p, -1, sizeof(p))
#if 1
#define DBG(fmt, ...) printf(fmt, __VA_ARGS__)
#else
#define DBG(fmt, ...)
#endif

#define MOD 1000000007
#define INF 1000000000
#define MAX_N 100010

string S;
string noX;
vector<int> Pos;

// 回文なら true
bool isKai(string str) {
  int r, l;
  if (str.length() % 2 == 0) {
    r = str.length() / 2;
    l = r - 1;
  } else {
    r = str.length() / 2 + 1;
    l = str.length() / 2 - 1;
  }
  while (r < str.length() && l >= 0) {
    if (str[l] != str[r]) {
      return false;
    }
    r++;
    l--;
  }
  return true;
}

int main() {
  cin >> S;
  bool isX = false;
  REP(i, S.length()) {
    if (S[i] == 'x') {
      isX = true;
    } else {
      noX += S[i];
      Pos.push_back(i);
    }
  }
  if (!isX) {
    if (isKai(S)) {
      printf("0\n");
    } else {
      printf("-1\n");
    }
    return 0;
  }
  if (isX && noX.length() == 0) {
    printf("0\n");
    return 0;
  }
  if (!isKai(noX)) {
    printf("-1\n");
    return 0;
  }
  int r, l, ans = 0;
  if (noX.length() % 2 == 0) {
    r = noX.length() / 2;
    l = Pos[r - 1];
    r = Pos[r];
  } else {
    r = Pos[noX.length() / 2] + 1;
    l = Pos[noX.length() / 2] - 1;
  }
  // DBG("r: %d l:%d\n", r, l);
  while (r < S.length() || l >= 0) {
    if (S[r] == 'x' && S[l] != 'x') {
      ans++;
      r++;
    } else if (S[r] != 'x' && S[l] == 'x') {
      ans++;
      l--;
    } else if (r >= S.length() && l >= 0) {
      ans++;
      l--;
    } else if (r < S.length() && l < 0) {
      ans++;
      r++;
    } else {
      r++;
      l--;
    }
  }
  printf("%d\n", ans);
  return 0;
}