Problem p03569 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <assert.h>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <vector>
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define MOD 1000000007
#define int long long
#ifdef int
const long long INF = LLONG_MAX / 10;
#else
const int INF = 1010101010;
#endif
using namespace std;
typedef long long ll;
typedef vector<vector<ll>> mat;
typedef pair<int, int> P;
// typedef pair<double, double> P;

#ifdef LOCAL
#define dump(...)                                                              \
  do {                                                                         \
    std::ostringstream os;                                                     \
    os << __LINE__ << ":\t" << #__VA_ARGS__ << " = ";                          \
    print_to(os, ", ", "\n", __VA_ARGS__);                                     \
    std::cerr << os.str();                                                     \
  } while (0)
#define dump_(a)                                                               \
  do {                                                                         \
    std::ostringstream os;                                                     \
    os << __LINE__ << ":\t" << #a << " = [";                                   \
    print_to_(os, ", ", "]\n", all(a));                                        \
    std::cerr << os.str();                                                     \
  } while (0)
#else
#define dump(...)
#define dump_(...)
#endif

template <typename T>
void print_to(std::ostream &os, const char *, const char *tail, const T &fst) {
  os << fst << tail;
}
template <typename Fst, typename... Rst>
void print_to(std::ostream &os, const char *del, const char *tail,
              const Fst &fst, const Rst &...rst) {
  os << fst << del;
  print_to(os, del, tail, rst...);
}
template <typename Iter>
void print_to_(std::ostream &os, const char *del, const char *tail, Iter bgn,
               Iter end) {
  for (Iter it = bgn; it != end;) {
    os << *it;
    if (++it != end) {
      os << del;
    } else {
      os << tail;
    }
  }
}
template <typename Fst, typename... Rst>
void println(const Fst &fst, const Rst &...rst) {
  print_to(std::cout, "\n", "\n", fst, rst...);
}
template <typename Fst, typename... Rst>
void print(const Fst &fst, const Rst &...rst) {
  print_to(std::cout, " ", "\n", fst, rst...);
}
template <typename Iter> void println_(Iter bgn, Iter end) {
  print_to_(std::cout, "\n", "\n", bgn, end);
}
template <typename Iter> void print_(Iter bgn, Iter end) {
  print_to_(std::cout, " ", "\n", bgn, end);
}

// const int MOD = 1000000007;
namespace trush {
int _ = (std::cout.precision(10), std::cout.setf(std::ios::fixed),
         std::cin.tie(0), std::ios::sync_with_stdio(0), 0);
}

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

string s;
int ans = 0;

signed main() {
  cin >> s;

  int n = s.length();
  int l = 0, r = n - 1;

  while (l < r) {
    if (s[l] != 'x' && s[r] != 'x' && s[l] != s[r]) {
      cout << -1 << endl;
      return 0;
    } else if (s[l] != 'x' && s[r] != 'x' && s[l] == s[r]) {
      l++;
      r--;
    } else if (s[l] == 'x' && s[r] != 'x') {
      ans++;
      l++;
    } else if (s[l] != 'x' && s[r] == 'x') {
      ans++;
      r--;
    }
  }

  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <vector>
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define MOD 1000000007
#define int long long
#ifdef int
const long long INF = LLONG_MAX / 10;
#else
const int INF = 1010101010;
#endif
using namespace std;
typedef long long ll;
typedef vector<vector<ll>> mat;
typedef pair<int, int> P;
// typedef pair<double, double> P;

#ifdef LOCAL
#define dump(...)                                                              \
  do {                                                                         \
    std::ostringstream os;                                                     \
    os << __LINE__ << ":\t" << #__VA_ARGS__ << " = ";                          \
    print_to(os, ", ", "\n", __VA_ARGS__);                                     \
    std::cerr << os.str();                                                     \
  } while (0)
#define dump_(a)                                                               \
  do {                                                                         \
    std::ostringstream os;                                                     \
    os << __LINE__ << ":\t" << #a << " = [";                                   \
    print_to_(os, ", ", "]\n", all(a));                                        \
    std::cerr << os.str();                                                     \
  } while (0)
#else
#define dump(...)
#define dump_(...)
#endif

template <typename T>
void print_to(std::ostream &os, const char *, const char *tail, const T &fst) {
  os << fst << tail;
}
template <typename Fst, typename... Rst>
void print_to(std::ostream &os, const char *del, const char *tail,
              const Fst &fst, const Rst &...rst) {
  os << fst << del;
  print_to(os, del, tail, rst...);
}
template <typename Iter>
void print_to_(std::ostream &os, const char *del, const char *tail, Iter bgn,
               Iter end) {
  for (Iter it = bgn; it != end;) {
    os << *it;
    if (++it != end) {
      os << del;
    } else {
      os << tail;
    }
  }
}
template <typename Fst, typename... Rst>
void println(const Fst &fst, const Rst &...rst) {
  print_to(std::cout, "\n", "\n", fst, rst...);
}
template <typename Fst, typename... Rst>
void print(const Fst &fst, const Rst &...rst) {
  print_to(std::cout, " ", "\n", fst, rst...);
}
template <typename Iter> void println_(Iter bgn, Iter end) {
  print_to_(std::cout, "\n", "\n", bgn, end);
}
template <typename Iter> void print_(Iter bgn, Iter end) {
  print_to_(std::cout, " ", "\n", bgn, end);
}

// const int MOD = 1000000007;
namespace trush {
int _ = (std::cout.precision(10), std::cout.setf(std::ios::fixed),
         std::cin.tie(0), std::ios::sync_with_stdio(0), 0);
}

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

string s;
int ans = 0;

signed main() {
  cin >> s;

  int n = s.length();
  int l = 0, r = n - 1;

  while (l < r) {
    if (s[l] != 'x' && s[r] != 'x' && s[l] != s[r]) {
      cout << -1 << endl;
      return 0;
    } else if (s[l] != 'x' && s[r] != 'x' && s[l] == s[r]) {
      l++;
      r--;
    } else if (s[l] == 'x' && s[r] != 'x') {
      ans++;
      l++;
    } else if (s[l] != 'x' && s[r] == 'x') {
      ans++;
      r--;
    } else if (s[l] == 'x' && s[r] == 'x') {
      l++;
      r--;
    }
  }

  cout << ans << endl;
}