Problem p03569 (C++)

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

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

  string string_without_x;
  vector<int> index_without_x;
  for (int i = 0; i < s.size(); i++) {
    char current = s[i];
    if (current != 'x') {
      string_without_x.push_back(current);
      index_without_x.push_back(i);
    }
  }

  for (int i = 0; i < string_without_x.size() / 2; i++) {
    if (string_without_x[i] !=
        string_without_x[string_without_x.size() - 1 - i]) {
      cout << -1 << endl;
      return 0;
    }
  }

  int right, left;
  if (index_without_x.size() % 2 == 0) {
    right = index_without_x.size() / 2;
    left = right - 1;
  } else {
    right = index_without_x.size() / 2;
    left = right;
  }

  right = index_without_x[right];
  left = index_without_x[left];
  int ans = 0;
  // while (right != s.size() - 1 || left != 0)
  // {
  //     if (s[left] == s[right])
  //     {
  //         left--;
  //         right++;
  //     }
  //     else
  //     {
  //         ans++;

  //         if (s[left] == 'x')
  //         {
  //             left--;
  //         }
  //         if (s[right] == 'x')
  //         {
  //             right++;
  //         }
  //     }
  //     left = max(0, left);
  //     right = min((int)s.size() - 1, right);
  // }
  // if (s[left] != s[right])
  // {
  //     ans++;
  // }
  while (right < s.size() && left >= 0) {
    // cout << left << " " << right << endl;
    if (s[left] == s[right]) {
      left--;
      right++;
    } else {
      ans++;
      if (s[left] == 'x') {
        left--;
      }
      if (s[right] == 'x') {
        right++;
      }
    }
  }
  // cout << left << " " << right << endl;
  // cout << left << " " << (int)(s.size() - 1) - right << endl;
  ans += left + 1;
  ans += s.size() - right;

  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

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

  string string_without_x;
  vector<int> index_without_x;
  for (int i = 0; i < s.size(); i++) {
    char current = s[i];
    if (current != 'x') {
      string_without_x.push_back(current);
      index_without_x.push_back(i);
    }
  }

  for (int i = 0; i < string_without_x.size() / 2; i++) {
    if (string_without_x[i] !=
        string_without_x[string_without_x.size() - 1 - i]) {
      cout << -1 << endl;
      return 0;
    }
  }

  if (index_without_x.size() == 0) {
    cout << 0 << endl;
    return 0;
  }

  int right, left;
  if (index_without_x.size() % 2 == 0) {
    right = index_without_x.size() / 2;
    left = right - 1;
  } else {
    right = index_without_x.size() / 2;
    left = right;
  }

  right = index_without_x[right];
  left = index_without_x[left];
  int ans = 0;
  // while (right != s.size() - 1 || left != 0)
  // {
  //     if (s[left] == s[right])
  //     {
  //         left--;
  //         right++;
  //     }
  //     else
  //     {
  //         ans++;

  //         if (s[left] == 'x')
  //         {
  //             left--;
  //         }
  //         if (s[right] == 'x')
  //         {
  //             right++;
  //         }
  //     }
  //     left = max(0, left);
  //     right = min((int)s.size() - 1, right);
  // }
  // if (s[left] != s[right])
  // {
  //     ans++;
  // }
  while (right < s.size() && left >= 0) {
    // cout << left << " " << right << endl;
    if (s[left] == s[right]) {
      left--;
      right++;
    } else {
      ans++;
      if (s[left] == 'x') {
        left--;
      }
      if (s[right] == 'x') {
        right++;
      }
    }
  }
  // cout << left << " " << right << endl;
  // cout << left << " " << (int)(s.size() - 1) - right << endl;
  ans += left + 1;
  ans += s.size() - right;

  cout << ans << endl;
}