Problem p03569 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map> //pair is also included in this library
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

#define REP(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define fst first
#define snd second
#define MP make_pair // incase c++11 or later is not available

using namespace std;

typedef long long int ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;

// prefer define statement
// for reason that const int ver. of INF will raise a silly warning (unused
// variable)
#define INF (INT_MAX / 3)
#define LIM_INF (INT_MAX)
// const int INF = INT_MAX/3;
#define PI = 3.14159265358979323846
/*------------------------------------------------------------------------------*/

int main() {
  string s, s2;
  cin >> s;

  REP(i, 0, s.size()) {
    if (s[i] != 'x') {
      s2.push_back(s[i]);
    }
  }

  string r(s2);
  reverse(r.begin(), r.end());

  REP(i, 0, s2.size()) {
    if (s2[i] != r[i]) {
      cout << "-1" << endl;
      return 0;
    }
  }

  ll ans = 0;

  int h = 0, t = s.size() - 1;

  while (h != s.size() / 2 || t != s.size() / 2) {
    if (s[h] == 'x' && s[t] != 'x') {
      h++;
      ans++;
    } else if (s[h] != 'x' && s[t] == 'x') {
      t--;
      ans++;
    } else {
      h++;
      t--;
    }
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map> //pair is also included in this library
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

#define REP(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define fst first
#define snd second
#define MP make_pair // incase c++11 or later is not available

using namespace std;

typedef long long int ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;

// prefer define statement
// for reason that const int ver. of INF will raise a silly warning (unused
// variable)
#define INF (INT_MAX / 3)
#define LIM_INF (INT_MAX)
// const int INF = INT_MAX/3;
#define PI = 3.14159265358979323846
/*------------------------------------------------------------------------------*/

int main() {
  string s, s2;
  cin >> s;

  REP(i, 0, s.size()) {
    if (s[i] != 'x') {
      s2.push_back(s[i]);
    }
  }

  string r(s2);
  reverse(r.begin(), r.end());

  REP(i, 0, s2.size()) {
    if (s2[i] != r[i]) {
      cout << "-1" << endl;
      return 0;
    }
  }

  ll ans = 0;

  int h = 0, t = s.size() - 1;

  while (h <= t) {
    // cout << "h:" << h << endl;
    // cout << "t:" << t << endl;
    if (s[h] == 'x' && s[t] != 'x') {
      h++;
      ans++;
    } else if (s[h] != 'x' && s[t] == 'x') {
      t--;
      ans++;
    } else {
      h++;
      t--;
    }
  }

  cout << ans << endl;

  return 0;
}