prithivMLmods/Coder-Stat huggingface dataset:
Problem p03569 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
const ll mod = 1000000007;
const int MAX_N = 1000; // n の最大値

// nCk を取得
double nCk(int n, int k) {
  double res = 1.0;
  for (int i = 0; i < n; i++) {
    res *= 0.5;
  }
  for (int i = 0; i < k; i++) {
    res *= (double)(n - i);
    res /= (double)(k - i);
  }
  return res;
}

int main() {
  string s;
  cin >> s;
  string t = "";
  ll n = s.size();
  for (ll i = 0; i < n; i++) {
    if (s.at(i) != 'x') {
      t += s.at(i);
    }
  }
  string u = t;
  reverse(t.begin(), t.end());
  if (t != u) {
    cout << -1 << endl;
  } else {
    ll ca = 0;
    ll m = 0, h = n - 1;
    ll cm = 0;
    ll ch = 0;
    while (h != m) {
      while (s.at(m) == 'x') {
        cm++;
        m++;
      }
      while (s.at(h) == 'x') {
        ch++;
        h--;
      }
      ca += abs(cm - ch);
      cm = 0;
      ch = 0;
      m++;
      h--;
      if (h - m <= 0) {
        break;
      }
    }
    cout << ca << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
const ll mod = 1000000007;
const int MAX_N = 1000; // n の最大値

// nCk を取得
double nCk(int n, int k) {
  double res = 1.0;
  for (int i = 0; i < n; i++) {
    res *= 0.5;
  }
  for (int i = 0; i < k; i++) {
    res *= (double)(n - i);
    res /= (double)(k - i);
  }
  return res;
}

int main() {
  string s;
  cin >> s;
  string t = "";
  ll n = s.size();
  for (ll i = 0; i < n; i++) {
    if (s.at(i) != 'x') {
      t += s.at(i);
    }
  }
  string u = t;
  reverse(t.begin(), t.end());
  if (t != u) {
    cout << -1 << endl;
  } else if (t.size() == 0) {
    cout << 0 << endl;
  } else {
    ll ca = 0;
    ll m = 0, h = n - 1;
    ll cm = 0;
    ll ch = 0;
    while (h != m) {
      while (s.at(m) == 'x') {
        cm++;
        m++;
      }
      while (s.at(h) == 'x') {
        ch++;
        h--;
      }
      ca += abs(cm - ch);
      cm = 0;
      ch = 0;
      m++;
      h--;
      if (h - m <= 0) {
        break;
      }
    }
    cout << ca << endl;
  }
}
Problem p03569 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define sayyes cout << "Yes" << endl;
#define sayno cout << "No" << endl;
using namespace std;
typedef long long ll;
const double PI = acos(-1);
typedef pair<int, int> P;
const int mod = 1e9 + 7;

int main() {
  string s;
  cin >> s;
  string t;
  int n = s.length();
  for (auto v : s) {
    if (v != 'x') {
      t += v;
    }
  }
  if (t != string(t.rbegin(), t.rend())) {
    cout << -1 << endl;
  } else {
    int ans = 0;
    int front = 0;
    int back = n - 1;
    while (back != front) {
      if (s[front] != s[back]) {
        ans++;
        s[front] == 'x' ? front++ : back--;
      } else {
        front++, back--;
      }
    }
    cout << ans;
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define sayyes cout << "Yes" << endl;
#define sayno cout << "No" << endl;
using namespace std;
typedef long long ll;
const double PI = acos(-1);
typedef pair<int, int> P;
const int mod = 1e9 + 7;

int main() {
  string s;
  cin >> s;
  string t;
  int n = s.length();
  for (auto v : s) {
    if (v != 'x') {
      t += v;
    }
  }
  if (t != string(t.rbegin(), t.rend())) {
    cout << -1 << endl;
  } else {
    int ans = 0;
    int front = 0;
    int back = n - 1;
    while (back - front > 0) {
      if (s[front] != s[back]) {
        ans++;
        s[front] == 'x' ? front++ : back--;
      } else {
        front++, back--;
      }
    }
    cout << ans;
  }
}
Problem p03569 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

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

  string t = "";
  rep(i, s.size()) if (s[i] != 'x') t += s[i];
  bool tp = true;
  rep(i, t.size()) if (t[i] != t[t.size() - 1 - i]) tp = false;
  if (!tp) {
    cout << -1 << endl;
    return 0;
  }

  vector<int> num_x(t.size() + 1, 0);
  int ni = 0;
  rep(i, s.size()) {
    if (s[i] == 'x')
      ++num_x[ni];
    else
      ++ni;
  }

  int ans = 0;
  for (int i = 0; i <= t.size() - i; ++i) {
    ans += abs(num_x[i] - num_x[t.size() - i]);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

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

  string t = "";
  rep(i, s.size()) if (s[i] != 'x') t += s[i];
  bool tp = true;
  rep(i, t.size()) if (t[i] != t[t.size() - 1 - i]) tp = false;
  if (!tp) {
    cout << -1 << endl;
    return 0;
  }

  vector<int> num_x(t.size() + 1, 0);
  int ni = 0;
  rep(i, s.size()) {
    if (s[i] == 'x')
      ++num_x[ni];
    else
      ++ni;
  }

  int ans = 0;
  for (int i = 0; i < (int)t.size() - i; ++i) {
    ans += abs(num_x[i] - num_x[t.size() - i]);
  }
  cout << ans << endl;
  return 0;
}
Problem p03569 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
string str;
void solve() {
  int i, len = str.length(), tag = 0, ans = 0;
  deque<char> deq;
  for (i = 0; i < len; i++)
    deq.push_back(str[i]);
  while (!deq.empty()) {
    char front = deq.front(), back = deq.back();
    if (front == back) {
      deq.pop_front();
      deq.pop_back();
    } else if (front == 'x')
      deq.push_back('x'), ans++;
    else if (back == 'x')
      deq.push_front('x'), ans++;
    else {
      tag = 1;
      break;
    }
  }
  if (tag)
    cout << -1;
  else
    cout << ans;
  cout << endl;
}
int main() {
  while (cin >> str)
    solve();
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
string str;
void solve() {
  int i, len = str.length(), tag = 0, ans = 0;
  deque<char> deq;
  for (i = 0; i < len; i++)
    deq.push_back(str[i]);
  while (!deq.empty()) {
    char front = deq.front(), back = deq.back();
    if (front == back) {
      deq.pop_front();
      if (!deq.empty())
        deq.pop_back();
    } else if (front == 'x')
      deq.push_back('x'), ans++;
    else if (back == 'x')
      deq.push_front('x'), ans++;
    else {
      tag = 1;
      break;
    }
  }
  if (tag)
    cout << -1;
  else
    cout << ans;
  cout << endl;
}
int main() {
  while (cin >> str)
    solve();
}
Problem p03569 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 60;
char s[maxn];
int main() {
  scanf("%s", s + 1);
  int n = strlen(s + 1);
  int l = 1, r = n, ans = 0;
  while (l < r) {
    if (s[l] == s[r]) {
      ++l;
      --r;
      continue;
    } else if (s[l] == 'x') {
      ++ans;
      ++l;
      continue;
    } else if (s[r] == 'x') {
      ++ans;
      --r;
      continue;
    } else {
      ans = -1;
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 60;
char s[maxn];
int main() {
  scanf("%s", s + 1);
  int n = strlen(s + 1);
  int l = 1, r = n, ans = 0;
  while (l < r) {
    if (s[l] == s[r]) {
      ++l;
      --r;
      continue;
    } else if (s[l] == 'x') {
      ++ans;
      ++l;
      continue;
    } else if (s[r] == 'x') {
      ++ans;
      --r;
      continue;
    } else {
      ans = -1;
      break;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03569 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout << #x " = " << ((x)) << endl
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

bool pal(string s) {
  int S = s.size();
  rep(i, S) if (s[i] != s[S - 1 - i]) return false;
  return true;
}

int solve() {
  string s;
  cin >> s;
  int S = s.size();

  string t = "";
  vector<int> idx;
  rep(i, S) if (s[i] != 'x') {
    t += s[i];
    idx.pb(i);
  }

  if (!pal(t))
    return -1;

  int T = t.size();
  int ct = 0;
  int l = idx[(T - 1) / 2], r = idx[T / 2];
  // dbg(l);dbg(r);
  while (l >= 0 || r < S) {
    // printf(" N %d %d\n", l,r);
    if (l == -1) {
      ++ct;
      ++r;
    } else if (r == S) {
      --l;
      ++ct;
    } else {
      if (s[l] == s[r]) {
        --l;
        ++r;
      } else {
        if (s[l] == 'x') {
          --l;
          ++ct;
        } else if (s[r] == 'x') {
          ++r;
          ++ct;
        } else
          assert(false);
      }
    }
  }

  return ct;
}

int main() {
  cout << solve() << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout << #x " = " << ((x)) << endl
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

bool pal(string s) {
  int S = s.size();
  rep(i, S) if (s[i] != s[S - 1 - i]) return false;
  return true;
}

int solve() {
  string s;
  cin >> s;
  int S = s.size();

  string t = "";
  vector<int> idx;
  rep(i, S) if (s[i] != 'x') {
    t += s[i];
    idx.pb(i);
  }

  if (t.size() == 0)
    return 0;
  if (!pal(t))
    return -1;

  int T = t.size();
  int ct = 0;
  int l = idx[(T - 1) / 2], r = idx[T / 2];
  // dbg(l);dbg(r);
  while (l >= 0 || r < S) {
    // printf(" N %d %d\n", l,r);
    if (l == -1) {
      ++ct;
      ++r;
    } else if (r == S) {
      --l;
      ++ct;
    } else {
      if (s[l] == s[r]) {
        --l;
        ++r;
      } else {
        if (s[l] == 'x') {
          --l;
          ++ct;
        } else if (s[r] == 'x') {
          ++r;
          ++ct;
        } else
          assert(false);
      }
    }
  }

  return ct;
}

int main() {
  cout << solve() << endl;
  return 0;
}
Problem p03569 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
char c[10005];
char s[10005];
int num[100005];
long long ans = 0;
int main() {
  scanf("%s", c);
  int l = strlen(c);
  int tot = 0;
  for (int i = 0; i < l; i++) {
    if (c[i] != 'x') {
      s[++tot] = c[i];
    }
  }
  for (int i = 1; i <= tot / 2; i++) {
    if (s[i] != s[tot - i + 1]) {
      printf("-1\n");
      return 0;
    }
  }
  int tot2 = 0;
  for (int i = 0; i < l; i++) {
    if (c[i] != 'x') {
      int kz = i + 1;
      tot2++;
      while (c[kz] == 'x') {
        num[tot2]++;
        kz++;
      }
    }
  }
  for (int i = 1; i <= (tot2 - 1) / 2; i++) {
    ans += abs(num[i] - num[tot2 - i]);
  }
  int xx = 0;
  int num1 = 0, num2 = 0;
  // for(int i=1;i<=tot2-1;i++)cout<<num[i]<<endl;
  while (c[xx] == 'x') {
    num1++;
    xx++;
  }
  xx = l - 1;
  while (c[xx] == 'x') {
    num2++;
    xx--;
  }
  ans += abs(num1 - num2);
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
char c[100005];
char s[100005];
int num[1000005];
long long ans = 0;
int main() {
  scanf("%s", c);
  int l = strlen(c);
  int tot = 0;
  for (int i = 0; i < l; i++) {
    if (c[i] != 'x') {
      s[++tot] = c[i];
    }
  }
  for (int i = 1; i <= tot / 2; i++) {
    if (s[i] != s[tot - i + 1]) {
      printf("-1\n");
      return 0;
    }
  }
  int tot2 = 0;
  for (int i = 0; i < l; i++) {
    if (c[i] != 'x') {
      int kz = i + 1;
      tot2++;
      while (c[kz] == 'x') {
        num[tot2]++;
        kz++;
      }
    }
  }
  for (int i = 1; i <= (tot2 - 1) / 2; i++) {
    ans += abs(num[i] - num[tot2 - i]);
  }
  int xx = 0;
  int num1 = 0, num2 = 0;
  // for(int i=1;i<=tot2-1;i++)cout<<num[i]<<endl;
  while (c[xx] == 'x') {
    num1++;
    xx++;
  }
  xx = l - 1;
  while (c[xx] == 'x') {
    num2++;
    xx--;
  }
  ans += abs(num1 - num2);
  printf("%lld\n", ans);
  return 0;
}
Problem p03569 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  string s, t;
  cin >> s;
  for (size_t i = 0; i < s.size(); i++) {
    if (s.at(i) != 'x') {
      t += s.at(i);
    }
  }
  for (size_t i = 0; i < t.size() / 2; i++) {
    if (t.at(i) != t.at(t.size() - 1 - i)) {
      cout << -1;
      return 0;
    }
  }
  vector<int> spot(t.size() + 1);
  int chking = 0;
  for (size_t i = 0; i < s.size(); i++) {
    if (s.at(i) == t.at(chking)) {
      chking++;
    } else {
      spot.at(chking)++;
    }
  }
  int ans = 0;
  for (size_t i = 0; i < spot.size() / 2; i++) {
    ans += abs(spot.at(i) - spot.at(spot.size() - 1 - i));
  }
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  string s, t;
  cin >> s;
  for (size_t i = 0; i < s.size(); i++) {
    if (s.at(i) != 'x') {
      t += s.at(i);
    }
  }
  for (size_t i = 0; i < t.size() / 2; i++) {
    if (t.at(i) != t.at(t.size() - 1 - i)) {
      cout << -1;
      return 0;
    }
  }
  vector<int> spot(t.size() + 1);
  int chking = 0;
  for (size_t i = 0; i < s.size(); i++) {
    if (chking < t.size() && s.at(i) == t.at(chking)) {
      chking++;
    } else {
      spot.at(chking)++;
    }
  }
  int ans = 0;
  for (size_t i = 0; i < spot.size() / 2; i++) {
    ans += abs(spot.at(i) - spot.at(spot.size() - 1 - i));
  }
  cout << ans;
}
Problem p03569 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>

int main() {
  std::string s;
  std::cin >> s;
  int n = s.length();
  std::vector<char> notx;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'x')
      continue;
    notx.push_back(s[i]);
  }
  bool canreverse = true;
  int notxsize = notx.size();
  for (int i = 0; i <= (notxsize - 1) / 2; i++) {
    if (notx[i] != notx[notxsize - i - 1]) {
      canreverse = false;
      break;
    }
  }
  if (!canreverse) {
    printf("-1\n");
  } else {
    std::vector<int> gap(notxsize + 1, 0);
    int counter = 0;
    for (int i = 0; i < n; i++) {
      if (s[i] != 'x') {
        counter++;
      } else {
        gap[counter]++;
        // printf("gap[%d]++\n",counter);
      }
    }
    int ans = 0;
    for (int i = 0; i <= notxsize / 2; i++) {
      // printf("ans+=|%d-%d|\n", gap[i],gap[notxsize-i]);
      ans += abs(gap[i] - gap[notxsize - i]);
    }
    printf("%d\n", ans);
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>

int main() {
  std::string s;
  std::cin >> s;
  int n = s.length();
  std::vector<char> notx;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'x')
      continue;
    notx.push_back(s[i]);
  }
  bool canreverse = true;
  int notxsize = notx.size();
  if (notxsize == 0) {
    printf("0\n");
    return 0;
  }
  for (int i = 0; i <= (notxsize - 1) / 2; i++) {
    if (notx[i] != notx[notxsize - i - 1]) {
      canreverse = false;
      break;
    }
  }
  if (!canreverse) {
    printf("-1\n");
  } else {
    std::vector<int> gap(notxsize + 1, 0);
    int counter = 0;
    for (int i = 0; i < n; i++) {
      if (s[i] != 'x') {
        counter++;
      } else {
        gap[counter]++;
        // printf("gap[%d]++\n",counter);
      }
    }
    int ans = 0;
    for (int i = 0; i <= notxsize / 2; i++) {
      // printf("ans+=|%d-%d|\n", gap[i],gap[notxsize-i]);
      ans += abs(gap[i] - gap[notxsize - i]);
    }
    printf("%d\n", ans);
  }
  return 0;
}
Problem p03569 (C++)

Buggy submission (status: Time Limit Exceeded):
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define PI 3.14159265359
using namespace std;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
const long long INF = 1e+18 + 1;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> T;
const ll MOD = 1000000007LL;
string abc = "abcdefghijklmnopqrstuvwxyz";
string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int main() {
  string s;
  cin >> s;
  string t = "";
  ll n = s.size();
  rep(i, n) {
    if (s[i] != 'x')
      t += s[i];
  }
  string u = t;
  reverse(u.begin(), u.end());
  if (u != t) {
    cout << -1 << endl;
    return 0;
  }
  ll k = t.size();
  string res = s;
  reverse(res.begin(), res.end());
  ll a = 0;
  ll b = 0;
  ll ans = 0;
  ll j = -1, i = -1;
  ll cnta = 0, cntb = 0;
  while (true) {
    cnta = 0;
    cntb = 0;
    i++;
    j++;
    while (i < n && s[i] != u[a]) {
      cnta++;
      i++;
    }
    while (j < n && res[j] != u[b]) {
      cntb++;
      j++;
    }
    a++;
    b++;
    // cout<<i<<" "<<j<<" "<<cnta<<" "<<cntb<<endl;
    ans += abs(cnta - cntb);
    if (a == (k + 1) / 2)
      break;
  }
  cout << ans << endl;
}


Fixed submission:
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define PI 3.14159265359
using namespace std;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
const long long INF = 1e+18 + 1;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> T;
const ll MOD = 1000000007LL;
string abc = "abcdefghijklmnopqrstuvwxyz";
string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int main() {
  string s;
  cin >> s;
  string t = "";
  ll n = s.size();
  rep(i, n) {
    if (s[i] != 'x')
      t += s[i];
  }
  string u = t;
  reverse(u.begin(), u.end());
  if (u != t) {
    cout << -1 << endl;
    return 0;
  }
  ll k = t.size();
  string res = s;
  reverse(res.begin(), res.end());
  ll a = 0;
  ll b = 0;
  ll ans = 0;
  ll j = -1, i = -1;
  ll cnta = 0, cntb = 0;
  while (true) {
    cnta = 0;
    cntb = 0;
    i++;
    j++;
    while (i < n && s[i] != u[a]) {
      cnta++;
      i++;
    }
    while (j < n && res[j] != u[b]) {
      cntb++;
      j++;
    }
    a++;
    b++;
    // cout<<i<<" "<<j<<" "<<cnta<<" "<<cntb<<endl;
    ans += abs(cnta - cntb);
    if (a >= (k + 1) / 2)
      break;
  }
  cout << ans << endl;
}
Problem p03569 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
using namespace std;

int main() {
  cin.tie(0)->sync_with_stdio(false);

  string s;
  cin >> s;
  int cnt = 0;
  while (s.size() != 1) {
    int n = s.size();
    if (s[0] == s[n - 1]) {
      s.erase(s.begin());
      s.pop_back();
    } else if (s[0] == 'x') {
      s.push_back('x');
      cnt++;
    } else if (s[n - 1] == 'x') {
      s.insert(s.begin(), 'x');
      cnt++;
    } else {
      cnt = -1;
      break;
    }
  }
  cout << cnt << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
using namespace std;

int main() {
  cin.tie(0)->sync_with_stdio(false);

  string s;
  cin >> s;
  int cnt = 0;
  while (s.size() > 1) {
    int n = s.size();
    if (s[0] == s[n - 1]) {
      s.erase(s.begin());
      s.pop_back();
    } else if (s[0] == 'x') {
      s.push_back('x');
      cnt++;
    } else if (s[n - 1] == 'x') {
      s.insert(s.begin(), 'x');
      cnt++;
    } else {
      cnt = -1;
      break;
    }
  }
  cout << cnt << '\n';
  return 0;
}
Problem p03569 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int MOD = 1000000007;
const int iINF = 1001001001;
const long long llINF = 1LL << 60;
int main() {
  string s;
  cin >> s;
  if (s.size() == 1) {
    cout << 0 << endl;
    return 0;
  }
  int ss = s.size();
  vector<pair<char, int>> ext;
  rep(i, ss) {
    if (s[i] != 'x')
      ext.emplace_back(s[i], i);
  }
  string b = "", e = "";
  rep(i, ext.size() / 2) {
    b += ext[i].first;
    e += ext[ext.size() - 1 - i].first;
  }
  if (b != e) {
    cout << -1 << endl;
    return 0;
  }
  int left, right;
  if (ext.size() % 2 == 1) {
    left = right = ext[ext.size() / 2].second;
  } else {
    left = ext[ext.size() / 2].second;
    right = ext[ext.size() / 2 - 1].second;
  }
  vector<int> bn(1, 0), en(1, 0);
  rep(i, left) {
    if (s[i] != 'x')
      bn.push_back(0);
    else
      bn.back()++;
  }
  for (int i = ss - 1; i > right; --i) {
    if (s[i] != 'x')
      en.push_back(0);
    else
      en.back()++;
  }
  int ans = 0;
  rep(i, bn.size()) { ans += abs(bn[i] - en[i]); }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int MOD = 1000000007;
const int iINF = 1001001001;
const long long llINF = 1LL << 60;
int main() {
  string s;
  cin >> s;
  if (s.size() == 1) {
    cout << 0 << endl;
    return 0;
  }
  int ss = s.size();
  vector<pair<char, int>> ext;
  rep(i, ss) {
    if (s[i] != 'x')
      ext.emplace_back(s[i], i);
  }
  if (ext.size() == 0) {
    cout << 0 << endl;
    return 0;
  }
  string b = "", e = "";
  rep(i, ext.size() / 2) {
    b += ext[i].first;
    e += ext[ext.size() - 1 - i].first;
  }
  if (b != e) {
    cout << -1 << endl;
    return 0;
  }
  int left, right;
  if (ext.size() % 2 == 1) {
    left = right = ext[ext.size() / 2].second;
  } else {
    left = ext[ext.size() / 2].second;
    right = ext[ext.size() / 2 - 1].second;
  }
  vector<int> bn(1, 0), en(1, 0);
  rep(i, left) {
    if (s[i] != 'x')
      bn.push_back(0);
    else
      bn.back()++;
  }
  for (int i = ss - 1; i > right; --i) {
    if (s[i] != 'x')
      en.push_back(0);
    else
      en.back()++;
  }
  int ans = 0;
  rep(i, bn.size()) { ans += abs(bn[i] - en[i]); }
  cout << ans << endl;
  return 0;
}
Problem p03569 (C++)

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

int main() {
  string S;
  cin >> S;
  string s1 = "", s2;
  for (int i = 0; i < S.size(); i++) {
    if (S.at(i) != 'x')
      s1 = s1 + S.at(i);
  }
  s2 = s1;
  reverse(s1.begin(), s1.end());
  if (s1 != s2) {
    cout << -1 << endl;
    return 0;
  }
  int64_t l = 0, r = S.size() - 1, cnt = 0;
  while (l >= r) {
    if (S.at(l) == S.at(r)) {
      l++;
      r--;
    } else {
      if (S.at(l) == 'x')
        l++;
      else
        r--;
      cnt++;
    }
  }
  cout << cnt << endl;
}


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

int main() {
  string S;
  cin >> S;
  string s1 = "", s2;
  for (int i = 0; i < S.size(); i++) {
    if (S.at(i) != 'x')
      s1 = s1 + S.at(i);
  }
  s2 = s1;
  reverse(s1.begin(), s1.end());
  if (s1 != s2) {
    cout << -1 << endl;
    return 0;
  }
  int64_t l = 0, r = S.size() - 1, cnt = 0;
  while (l < r) {
    if (S.at(l) == S.at(r)) {
      l++;
      r--;
    } else {
      if (S.at(l) == 'x')
        l++;
      else
        r--;
      cnt++;
    }
  }
  cout << cnt << endl;
}
Problem p03569 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
using namespace std;
using Graph = vector<vector<int>>;
#define MOD 1000000007
#define MOD2 998244353
#define INF ((1 << 30) - 1)
#define LINF (1LL << 60)
#define EPS (1e-10)
typedef long long ll;
typedef pair<ll, ll> P;

int main() {
  string s;
  cin >> s;
  int n = s.size();

  vector<char> v;
  rep(i, n) {
    if (s[i] != 'x')
      v.push_back(s[i]);
  }

  int m = v.size();
  bool flag = true;
  rep(i, (m + 1) / 2) {
    if (v[i] != v[m - i - 1])
      flag = false;
  }

  if (!flag) {
    cout << -1 << endl;
    return 0;
  }

  // rep(i, v.size()) cout << v[i] << " ";
  // cout << endl;

  vector<char> l, r;
  int i = 0, c = 0;
  while (i < n) {
    l.push_back(s[i]);
    if (s[i] == v[c])
      c++;
    if (c == (m + 1) / 2)
      break;
    i++;
  }
  int j = n - 1;
  c = 0;
  while (j >= 0) {
    r.push_back(s[j]);
    if (s[j] == v[c])
      c++;
    if (c == (m + 1) / 2)
      break;
    j--;
  }

  // rep(i, l.size()) cout << l[i] << " ";
  // cout << endl;
  // rep(i, r.size()) cout << r[i] << " ";
  // cout << endl;

  i = 0, j = 0;
  ll ans = 0;
  while (i < l.size() && j < r.size()) {
    if (l[i] == 'x' && r[j] != 'x') {
      ans++;
      i++;
    } else if (l[i] != 'x' && r[j] == 'x') {
      ans++;
      j++;
    } else {
      i++;
      j++;
    }
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
using namespace std;
using Graph = vector<vector<int>>;
#define MOD 1000000007
#define MOD2 998244353
#define INF ((1 << 30) - 1)
#define LINF (1LL << 60)
#define EPS (1e-10)
typedef long long ll;
typedef pair<ll, ll> P;

int main() {
  string s;
  cin >> s;
  int n = s.size();

  vector<char> v;
  rep(i, n) {
    if (s[i] != 'x')
      v.push_back(s[i]);
  }

  int m = v.size();
  if (m == 0) {
    cout << 0 << endl;
    return 0;
  }

  bool flag = true;
  rep(i, (m + 1) / 2) {
    if (v[i] != v[m - i - 1])
      flag = false;
  }

  if (!flag) {
    cout << -1 << endl;
    return 0;
  }

  // rep(i, v.size()) cout << v[i] << " ";
  // cout << endl;

  vector<char> l, r;
  int i = 0, c = 0;
  while (i < n) {
    l.push_back(s[i]);
    if (s[i] == v[c])
      c++;
    if (c == (m + 1) / 2)
      break;
    i++;
  }
  int j = n - 1;
  c = 0;
  while (j >= 0) {
    r.push_back(s[j]);
    if (s[j] == v[c])
      c++;
    if (c == (m + 1) / 2)
      break;
    j--;
  }

  // rep(i, l.size()) cout << l[i] << " ";
  // cout << endl;
  // rep(i, r.size()) cout << r[i] << " ";
  // cout << endl;

  i = 0, j = 0;
  ll ans = 0;
  while (i < l.size() && j < r.size()) {
    if (l[i] == 'x' && r[j] != 'x') {
      ans++;
      i++;
    } else if (l[i] != 'x' && r[j] == 'x') {
      ans++;
      j++;
    } else {
      i++;
      j++;
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p03569 (C++)

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

using namespace std;

int main() {
  string s, trim;
  vector<int> pos;

  cin >> s;
  int n = s.size();
  for (int i = 0; i < n; ++i) {
    if (s[i] != 'x') {
      trim += s[i];
      pos.push_back(i);
    }
  }

  string rtrim = trim;
  reverse(rtrim.begin(), rtrim.end());
  if (trim != rtrim) {
    cout << -1 << endl;
  } else {
    int len = trim.size();
    int p1 = pos[(len - 1) / 2], p2 = pos[len / 2];

    int ans = 0;
    while (p1 >= 0 or p2 < n) {
      if (p1 >= 0 and p2 < n and s[p1] == s[p2]) {
        --p1;
        ++p2;
        continue;
      }
      if (p1 >= 0 and p2 < n and s[p1] != s[p2]) {
        assert(s[p1] == 'x' or s[p2] == 'x');
        ans += 1;
        if (s[p1] == 'x')
          --p1;
        else
          ++p2;
        continue;
      }

      ans += 1;
      if (p1 >= 0)
        --p1;
      else
        ++p2;
    }
    cout << ans << endl;
  }
  return 0;
}


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

using namespace std;

int main() {
  string s, trim;
  vector<int> pos;

  cin >> s;
  int n = s.size();
  for (int i = 0; i < n; ++i) {
    if (s[i] != 'x') {
      trim += s[i];
      pos.push_back(i);
    }
  }
  if (trim.empty()) {
    cout << 0 << endl;
    return 0;
  }

  string rtrim = trim;
  reverse(rtrim.begin(), rtrim.end());
  if (trim != rtrim) {
    cout << -1 << endl;
  } else {
    int len = trim.size();
    int p1 = pos[(len - 1) / 2], p2 = pos[len / 2];

    int ans = 0;
    while (p1 >= 0 or p2 < n) {
      if (p1 >= 0 and p2 < n and s[p1] == s[p2]) {
        --p1;
        ++p2;
        continue;
      }
      if (p1 >= 0 and p2 < n and s[p1] != s[p2]) {
        assert(s[p1] == 'x' or s[p2] == 'x');
        ans += 1;
        if (s[p1] == 'x')
          --p1;
        else
          ++p2;
        continue;
      }

      ans += 1;
      if (p1 >= 0)
        --p1;
      else
        ++p2;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p03569 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = (0); (i) < (int)(n); ++(i))
using ll = long long;
using P = pair<ll, ll>;

int alp[30];

int main() {
  string s;
  cin >> s;
  string t = "";

  rep(i, s.size()) {
    if (s[i] != 'x')
      t += s[i];
    else {
      alp[s[i] - 'a']++;
    }
  }

  if (t.size() == 0) {
    string tt = s;
    reverse(tt.begin(), tt.end());
    if (s != tt) {
      cout << -1 << endl;
      return 0;
    } else {
      cout << 0 << endl;
      return 0;
    }
  }

  string tt = t;
  reverse(tt.begin(), tt.end());
  if (t != tt) {
    cout << -1 << endl;
    return 0;
  }

  // cout << t << " " << tt << endl;

  int l = 0, r = s.size() - 1;
  ll ans = 0;
  while (r - l) {
    if (s[l] == s[r]) {
      l++;
      r--;
    } else if (s[l] == 'x' and s[r] != 'x') {
      ans++;
      l++;
    } else {
      ans++;
      r--;
    }
  }

  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = (0); (i) < (int)(n); ++(i))
using ll = long long;
using P = pair<ll, ll>;

int alp[30];

int main() {
  string s;
  cin >> s;
  string t = "";

  rep(i, s.size()) {
    if (s[i] != 'x')
      t += s[i];
    else {
      alp[s[i] - 'a']++;
    }
  }

  if (t.size() == 0) {
    string tt = s;
    reverse(tt.begin(), tt.end());
    if (s != tt) {
      cout << -1 << endl;
      return 0;
    } else {
      cout << 0 << endl;
      return 0;
    }
  }

  string tt = t;
  reverse(tt.begin(), tt.end());
  if (t != tt) {
    cout << -1 << endl;
    return 0;
  }

  // cout << t << " " << tt << endl;

  int l = 0, r = s.size() - 1;
  ll ans = 0;
  while (r - l > 0) {
    if (s[l] == s[r]) {
      l++;
      r--;
    } else if (s[l] == 'x' and s[r] != 'x') {
      ans++;
      l++;
    } else {
      ans++;
      r--;
    }
  }

  cout << ans << endl;
}
Problem p03569 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
#define ll long long
#define mp make_pair
#define pb push_back
#define lson l, mid, pos << 1
#define rson mid + 1, r, pos << 1 | 1
using namespace std;

const int maxn = 100000 + 10;
// const int maxm=2000+5;
// const ll mod=1e9 + 7;
// const int inf=0x3f3f3f3f;
// const double PI=acos(-1);
// const double eps=1e-8;
// int dir[4][2]={0,1,1,0,0,-1,-1,0};
// const ll INF = 0x3f3f3f3f3f3f3f3fLL;
// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
// ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
// ll inv(ll b){if(b==1) return 1; return (mod-mod/b)*inv(mod%b)%mod;}
// ll fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n%mod;n=n*n%mod;}return
// r;}

char s[maxn];
char str[maxn];

int main() {
  scanf("%s", s);
  int len = strlen(s);
  int cnt = 0;
  for (int i = 0; i < len; i++) {
    if (s[i] != 'x')
      str[cnt++] = s[i];
  }
  if (cnt == 0) {
    cout << 0 << endl;
    return 0;
  }
  for (int i = 0; i < cnt / 2; i++) {
    if (str[i] != str[cnt - i - 1]) {
      cout << "-1" << endl;
      return 0;
    }
  }
  int ans1 = 0, ans2 = len - 1;
  int sum = 0;
  while (ans1 != ans2) {
    if (s[ans1] != s[ans2]) {
      sum++;
      if (s[ans1] == 'x') {
        ans1++;
      } else {
        ans2--;
      }
    } else {
      ans1++;
      ans2--;
    }
  }
  cout << sum << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
#define ll long long
#define mp make_pair
#define pb push_back
#define lson l, mid, pos << 1
#define rson mid + 1, r, pos << 1 | 1
using namespace std;

const int maxn = 100000 + 10;
// const int maxm=2000+5;
// const ll mod=1e9 + 7;
// const int inf=0x3f3f3f3f;
// const double PI=acos(-1);
// const double eps=1e-8;
// int dir[4][2]={0,1,1,0,0,-1,-1,0};
// const ll INF = 0x3f3f3f3f3f3f3f3fLL;
// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
// ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
// ll inv(ll b){if(b==1) return 1; return (mod-mod/b)*inv(mod%b)%mod;}
// ll fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n%mod;n=n*n%mod;}return
// r;}

char s[maxn];
char str[maxn];

int main() {
  scanf("%s", s);
  int len = strlen(s);
  int cnt = 0;
  for (int i = 0; i < len; i++) {
    if (s[i] != 'x')
      str[cnt++] = s[i];
  }
  if (cnt == 0) {
    cout << 0 << endl;
    return 0;
  }
  for (int i = 0; i < cnt / 2; i++) {
    if (str[i] != str[cnt - i - 1]) {
      cout << "-1" << endl;
      return 0;
    }
  }
  int ans1 = 0, ans2 = len - 1;
  int sum = 0;
  while (ans1 < ans2) {
    if (s[ans1] != s[ans2]) {
      sum++;
      if (s[ans1] == 'x') {
        ans1++;
      } else {
        ans2--;
      }
    } else {
      ans1++;
      ans2--;
    }
  }
  cout << sum << endl;
  return 0;
}
Problem p03569 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> V;
typedef map<int, int> M;

constexpr ll INF = 1e18;
constexpr ll MOD = 1e9 + 7;
constexpr double PI = 3.14159265358979323846;
constexpr int di[] = {0, 0, 1, -1};
constexpr int dj[] = {1, -1, 0, 0};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  string s;
  cin >> s;

  vector<char> v;
  REP(i, s.size()) {
    if (s[i] != 'x')
      v.push_back(s[i]);
  }

  bool ok = true;
  for (int i = 0; i < s.size() / 2; i++)
    if (v[i] != v[v.size() - i - 1])
      ok = false;

  if (!ok) {
    cout << -1 << endl;
    return 0;
  }

  V x;

  int i = 0;
  while (i < s.size()) {
    int cnt = 0;
    while (i < s.size() && s[i] == 'x') {
      i++;
      cnt++;
    }
    x.push_back(cnt);
    i++;
  }
  if (s[s.size() - 1] != 'x') {
    int cnt = 0;
    while (i < s.size() && s[i] == 'x') {
      i++;
      cnt++;
    }
    x.push_back(cnt);
  }

  int res = 0;
  REP(i, x.size() / 2)
  res += abs(x[i] - x[x.size() - i - 1]);

  cout << res << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> V;
typedef map<int, int> M;

constexpr ll INF = 1e18;
constexpr ll MOD = 1e9 + 7;
constexpr double PI = 3.14159265358979323846;
constexpr int di[] = {0, 0, 1, -1};
constexpr int dj[] = {1, -1, 0, 0};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  string s;
  cin >> s;

  vector<char> v;
  REP(i, s.size()) {
    if (s[i] != 'x')
      v.push_back(s[i]);
  }

  bool ok = true;
  for (int i = 0; i < v.size() / 2; i++)
    if (v[i] != v[v.size() - i - 1])
      ok = false;

  if (!ok) {
    cout << -1 << endl;
    return 0;
  }

  V x;

  int i = 0;
  while (i < s.size()) {
    int cnt = 0;
    while (i < s.size() && s[i] == 'x') {
      i++;
      cnt++;
    }
    x.push_back(cnt);
    i++;
  }
  if (s[s.size() - 1] != 'x') {
    int cnt = 0;
    while (i < s.size() && s[i] == 'x') {
      i++;
      cnt++;
    }
    x.push_back(cnt);
  }

  int res = 0;
  REP(i, x.size() / 2)
  res += abs(x[i] - x[x.size() - i - 1]);

  cout << res << endl;

  return 0;
}
Problem p03569 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 100010;
int ans = 0, tot = 0, sum = 0, n, l, r;
char s[maxn], a[maxn];
void read(int &k) {
  k = 0;
  int f = 1;
  char c = getchar();
  while (c < '0' || c > '9')
    c == '-' && (f = -1), c = getchar();
  while ('0' <= c && c <= '9')
    k = k * 10 + c - '0', c = getchar();
  k *= f;
}
int main() {
  scanf("%s", s);
  int n = strlen(s);
  l = 0;
  r = n - 1;
  while (l <= r) {
    if (s[l] == s[r])
      l++, r--;
    else {
      if (s[l] == 'x' && s[r] != 'x')
        ans++;
      if (s[l] != 'x' && s[r] == 'x')
        ans++;
      if (s[l] != 'x' && s[r] != 'x') {
        printf("-1");
        return 0;
      }
    }
  }
  printf("%d", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 100010;
int ans = 0, tot = 0, sum = 0, n, l, r;
char s[maxn], a[maxn];
void read(int &k) {
  k = 0;
  int f = 1;
  char c = getchar();
  while (c < '0' || c > '9')
    c == '-' && (f = -1), c = getchar();
  while ('0' <= c && c <= '9')
    k = k * 10 + c - '0', c = getchar();
  k *= f;
}
int main() {
  scanf("%s", s);
  int n = strlen(s);
  l = 0;
  r = n - 1;
  while (l <= r) {
    if (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') {
        printf("-1");
        return 0;
      }
    }
  }
  printf("%d", ans);
  return 0;
}
Problem p03569 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

int main() {
  string s;
  cin >> s;
  int f = 0;
  int b = s.size() - 1;

  int ans = 0;
  while (f < b) {
    if (s[f] != s[b]) {
      if (s[f] == 'x' || s[b] == 'x') {
        ans++;
        if (s[f] == 'x') {
          f++;
        } else {
          b--;
        }
      } else {
        cout << -1 << endl;
        return 9;
      }
    } else {
      f++;
      b--;
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <iostream>
using namespace std;

int main() {
  string s;
  cin >> s;
  int f = 0;
  int b = s.size() - 1;

  int ans = 0;
  while (f < b) {
    if (s[f] != s[b]) {
      if (s[f] == 'x' || s[b] == 'x') {
        ans++;
        if (s[f] == 'x') {
          f++;
        } else {
          b--;
        }
      } else {
        cout << -1 << endl;
        return 0;
      }
    } else {
      f++;
      b--;
    }
  }
  cout << ans << endl;
}
Problem p03569 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
#define N 5000100
char a[N];
char b[N];
ll k = 0;
int deal() {
  for (ll i = 0, j = k - 1; i < k / 2; i++, j--)
    if (b[i] != b[j])
      return 0;
  return 1;
}

int main() {
  int flag = 0;
  scanf("%s", a);
  ll len = strlen(a);
  ll num = 0;
  for (ll i = 0; i < len; i++)
    if (a[i] != 'x')
      b[k++] = a[i];
  if (!deal())
    flag = 1;
  else {
    int j = len - 1;
    for (ll i = 0; i != j;) {
      if (a[i] == a[j])
        i++, j--;
      else if (a[i] == 'x')
        i++, num++;
      else if (a[j] == 'x')
        j--, num++;
    }
  }
  if (flag)
    cout << "-1" << endl;
  else
    cout << num << endl;

  return 0;
}


Fixed submission:
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
#define N 5000100
char a[N];
char b[N];
ll k = 0;
int deal() {
  for (ll i = 0, j = k - 1; i < k / 2; i++, j--)
    if (b[i] != b[j])
      return 0;
  return 1;
}

int main() {
  int flag = 0;
  scanf("%s", a);
  ll len = strlen(a);
  ll num = 0;
  for (ll i = 0; i < len; i++)
    if (a[i] != 'x')
      b[k++] = a[i];
  if (!deal())
    flag = 1;
  else {
    int i = 0, j = len - 1;
    while (i < j) {
      if (a[i] == a[j])
        i++, j--;
      else if (a[i] == 'x')
        i++, num++;
      else if (a[j] == 'x')
        j--, num++;
    }
  }
  if (flag)
    cout << "-1" << endl;
  else
    cout << num << endl;

  return 0;
}
Problem p03569 (C++)

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

int main() {
  string s;
  cin >> s;
  int n = s.size();
  int count = 0;
  vector<pair<char, int>> ss;
  for (int i = 0; i < n; i++) {
    if (s[i] != 'x') {
      count++;
      ss.push_back(make_pair(s[i], i));
    }
  }
  int ans = 0;
  vector<char> l;
  vector<char> r;

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

  for (int i = 0; i < n; i++) {
    if (count % 2 == 0) {
      if (i <= ss[count / 2 - 1].second) {
        l.push_back(s[i]);
      } else if (i >= ss[count / 2].second) {
        r.push_back(s[i]);
      }
    } else {
      if (i < ss[count / 2].second) {
        l.push_back(s[i]);
      } else if (i > ss[count / 2].second) {
        r.push_back(s[i]);
      }
    }
  }

  reverse(r.begin(), r.end());
  int l_index = 0;
  int r_index = 0;
  for (int i = 0; i < 2 * n; i++) {
    if (l_index == l.size() || r_index == r.size()) {
      ans += max(r.size() - r_index, l.size() - l_index);
      break;
    }
    if (l[l_index] == r[r_index]) {
      l_index++;
      r_index++;
    } else if (l[l_index] != r[r_index]) {
      ans++;
      if (l[l_index] == 'x') {
        l_index++;
      } else {
        r_index++;
      }
    }
  }

  cout << ans << endl;

  return 0;
}

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

int main() {
  string s;
  cin >> s;
  int n = s.size();
  int count = 0;
  vector<pair<char, int>> ss;
  for (int i = 0; i < n; i++) {
    if (s[i] != 'x') {
      count++;
      ss.push_back(make_pair(s[i], i));
    }
  }
  int ans = 0;
  vector<char> l;
  vector<char> r;
  if (count == 0) {
    cout << 0 << endl;
    return 0;
  }
  for (int i = 0; i < count / 2; i++) {
    if (ss[i].first != ss[count - 1 - i].first) {
      cout << -1 << endl;
      return 0;
    }
  }

  for (int i = 0; i < n; i++) {
    if (count % 2 == 0) {
      if (i <= ss[count / 2 - 1].second) {
        l.push_back(s[i]);
      } else if (i >= ss[count / 2].second) {
        r.push_back(s[i]);
      }
    } else {
      if (i < ss[count / 2].second) {
        l.push_back(s[i]);
      } else if (i > ss[count / 2].second) {
        r.push_back(s[i]);
      }
    }
  }

  reverse(r.begin(), r.end());
  int l_index = 0;
  int r_index = 0;
  for (int i = 0; i < 2 * n; i++) {
    if (l_index == l.size() || r_index == r.size()) {
      ans += max(r.size() - r_index, l.size() - l_index);
      break;
    }
    if (l[l_index] == r[r_index]) {
      l_index++;
      r_index++;
    } else if (l[l_index] != r[r_index]) {
      ans++;
      if (l[l_index] == 'x') {
        l_index++;
      } else {
        r_index++;
      }
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p03569 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int main() {

  string s;
  cin >> s;

  deque<char> d;
  for (int i = 0; i < s.size(); i++)
    d.push_back(s[i]);

  int res = 0;
  while (!d.empty()) {
    if (d.front() == d.back())
      d.pop_front(), d.pop_back();
    else if (d.front() == 'x')
      d.push_back('x'), res++;
    else if (d.back() == 'x')
      d.push_front('x'), res++;
    else {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << res << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int main() {

  string s;
  cin >> s;

  deque<char> d;
  for (int i = 0; i < s.size(); i++)
    d.push_back(s[i]);

  int res = 0;
  while (!d.empty()) {
    if (d.front() == d.back()) {
      d.pop_front();
      if (!d.empty())
        d.pop_back();
    } else if (d.front() == 'x')
      d.push_back('x'), res++;
    else if (d.back() == 'x')
      d.push_front('x'), res++;
    else {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << res << endl;

  return 0;
}
Problem p03569 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  string s;
  cin >> s;
  int n = s.length();
  int c = 0;
  int k = n - 1;
  int i = 0;
  while (i < n + c) {
    if (s[i] != s[k - i] && s[i] != 'x' && s[k - i] != 'x') {
      cout << -1 << endl;
      return 0;
    } else if (s[i] != s[k - i] && s[i] != 'x') {
      c = c + 1;
      k = k + 1;
      s.insert(i, "x");
      i = i + 1;
    } else if (s[i] != s[k - i]) {
      c = c + 1;
      k = k + 1;
      if (i == 0) {
        s += "x";
      } else {
        s.insert(k - i + 1, "x");
      }
      i = i + 1;
    } else {
      i = i + 1;
    }
  }
  cout << c << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  string s;
  cin >> s;
  int n = s.length();
  int c = 0;
  int k = n - 1;
  int i = 0;
  while (i < (n + c) / 2) {
    if (s[i] != s[k - i] && s[i] != 'x' && s[k - i] != 'x') {
      cout << -1 << endl;
      return 0;
    } else if (s[i] != s[k - i] && s[i] != 'x') {
      c = c + 1;
      k = k + 1;
      s.insert(i, "x");
      i = i + 1;
    } else if (s[i] != s[k - i]) {
      c = c + 1;
      k = k + 1;
      if (i == 0) {
        s += "x";
      } else {
        s.insert(k - i + 1, "x");
      }
      i = i + 1;
    } else {
      i = i + 1;
    }
  }
  cout << c << endl;
}
Problem p03569 (Python)

Buggy submission (status: Runtime Error):
def is_valid():
    x_removed_S = S.replace("x", "")
    M = len(x_removed_S)
    for i in range(M // 2):
        if x_removed_S[i] != x_removed_S[M - i - 1]:
            return False
    return True


S = input()
N = len(S)
if is_valid():
    ans = 0
    left, right = 0, N - 1
    while left != right:
        if S[left] == S[right]:
            left += 1
            right -= 1
            continue
        elif S[left] == "x":
            ans += 1
            left += 1
        else:
            ans += 1
            right -= 1
    print(ans)
else:
    print(-1)


Fixed submission:
def is_valid():
    x_removed_S = S.replace("x", "")
    M = len(x_removed_S)
    for i in range(M // 2):
        if x_removed_S[i] != x_removed_S[M - i - 1]:
            return False
    return True


S = input()
N = len(S)
if is_valid():
    ans = 0
    left, right = 0, N - 1
    while left < right:
        if S[left] == S[right]:
            left += 1
            right -= 1
            continue
        elif S[left] == "x":
            ans += 1
            left += 1
        else:
            ans += 1
            right -= 1
    print(ans)
else:
    print(-1)