alexjercan/bugnet huggingface dataset:
Problem p00109 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <iostream>
#include <string>
using namespace std;

void select_cal(char c, int *tmp, int val) {

  if (c == '+')
    *tmp += val;
  else if (c == '-')
    *tmp -= val;
  else if (c == '*')
    *tmp = floor(*tmp * val);
  else if (c == '/' && val != 0)
    *tmp = floor(*tmp / val);
}

int calculation(string str, int len, int begin) {

  int ans, tmp, cnt, val;
  char c;

  ans = 0;
  tmp = 0;
  c = '+';

  for (int i = begin; i < len; i++) {
    if (str[i] == '+' || str[i] == '-') {
      ans += tmp;
      tmp = 0;
      c = str[i];
    } else if (str[i] == '*' || str[i] == '/') {
      c = str[i];
    } else if (str[i] == '(') {
      select_cal(c, &tmp, calculation(str, len, i + 1));
      cnt = 1;
      do {
        i++;
        if (str[i] == '(')
          cnt++;
        else if (str[i] == ')')
          cnt--;
      } while (cnt != 0);
    } else if (str[i] == ')' || str[i] == '=') {
      break;
    } else {
      cnt = 1;
      while (str[i + cnt] >= '0' && str[i + cnt] <= '9') {
        cnt++;
      }
      val = 0;
      for (int j = 0; j < cnt; j++) {
        val += (str[i + cnt - j - 1] - '0') * pow(10, j);
      }
      select_cal(c, &tmp, val);
      i += cnt - 1;
    }
  }
  ans += tmp;

  return ans;
}

int main() {

  int n;
  string str;

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> str;
    cout << calculation(str, str.length(), 0) << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <string>
using namespace std;

void select_cal(char c, int *tmp, int val) {

  if (c == '+')
    *tmp += val;
  else if (c == '-')
    *tmp -= val;
  else if (c == '*')
    *tmp *= val;
  else if (c == '/')
    *tmp /= val;
}

int calculation(string str, int len, int begin) {

  int ans, tmp, cnt;
  char c;

  ans = 0;
  tmp = 0;
  c = '+';

  for (int i = begin; i < len; i++) {
    if (str[i] == '+' || str[i] == '-') {
      ans += tmp;
      tmp = 0;
      c = str[i];
    } else if (str[i] == '*' || str[i] == '/') {
      c = str[i];
    } else if (str[i] == '(') {
      select_cal(c, &tmp, calculation(str, len, i + 1));
      cnt = 1;
      do {
        i++;
        if (str[i] == '(')
          cnt++;
        else if (str[i] == ')')
          cnt--;
      } while (cnt != 0);
    } else if (str[i] == ')' || str[i] == '=') {
      break;
    } else {
      select_cal(c, &tmp, str[i] - '0');
    }
  }
  ans += tmp;

  return ans;
}

int main() {

  int n;
  string str;

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> str;
    cout << calculation(str, str.length(), 0) << endl;
  }

  return 0;
}
Problem p00109 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>

using namespace std;

string S;
size_t cur;

int digit() { return S[cur++] - '0'; }

int number() {
  int n = digit();
  while (cur < S.size() && isdigit(S[cur])) {
    n = n * 10 + digit();
  }
  return n;
}

int expression();

int factor() {
  if (isdigit(S[cur]))
    return number();
  cur++;
  int n = expression();
  cur++;
  return n;
}

int term() {
  int t = factor();
  for (; cur < S.size() && (S[cur] == '*' || S[cur] == '/');) {
    if (S[cur++] == '*')
      t *= factor();
    else
      t /= factor();
  }
  return t;
}

int expression() {
  int t = term();
  for (; cur < S.size() && (S[cur] == '+' || S[cur] == '-');) {
    if (S[cur++] == '+')
      t += term();
    else
      t -= term();
  }
  return t;
}

int main() {
  int n;
  cin >> n;
  for (; n--;) {
    cur = 0;
    cin >> S;
    cout << expression() << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
#include <string>

using namespace std;

string S;
size_t cur;

int digit() { return S[cur++] - '0'; }

int number() {
  int n = digit();
  while (cur < S.size() && isdigit(S[cur])) {
    n = n * 10 + digit();
  }
  return n;
}

int expression();

int factor() {
  if (isdigit(S[cur]))
    return number();
  cur++;
  int n = expression();
  cur++;
  return n;
}

int term() {
  int t = factor();
  for (; cur < S.size() && (S[cur] == '*' || S[cur] == '/');) {
    if (S[cur++] == '*')
      t *= factor();
    else
      t /= factor();
  }
  return t;
}

int expression() {
  int t = term();
  for (; cur < S.size() && (S[cur] == '+' || S[cur] == '-');) {
    if (S[cur++] == '+')
      t += term();
    else
      t -= term();
  }
  return t;
}

int main() {
  int n;
  cin >> n;
  for (; n--;) {
    cin >> S;
    cout << expression() << endl;
  }
  return 0;
}
Problem p00109 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <cstdio>
using namespace std;

char *pt;
int parseAtom();
int parseExpr();
int parseTerm();
int parseAtom() {
  if ('0' <= *pt && *pt <= '9') {
    int x = *pt - '0';
    pt++;
    while ('0' <= *pt && *pt <= '9') {
      x = x * 10 + *pt - '0';
      pt++;
    }
    return x;
  }
  if ('(' == *pt) {
    pt++;
    int x = parseExpr();
    assert(*pt == ')');
    pt++;
    return x;
  }
  assert(false);
  return -1;
}
int parseExpr() {
  int x = parseTerm();
  while (*pt == '+' || *pt == '-') {
    if (*pt == '+') {
      pt++;
      x += parseTerm();
    } else {
      pt++;
      x -= parseTerm();
    }
  }
  return x;
}
int parseTerm() {
  int x = parseAtom();
  while (*pt == '*' || *pt == '/') {
    if (*pt == '*') {
      pt++;
      x *= parseAtom();
    } else {
      pt++;
      x /= parseAtom();
    }
  }
  return x;
}

int main() {
  int tc;
  scanf("%d", &tc);
  for (int tci = 0; tci < tc; tci++) {
    static char expr[200];
    scanf(" %s", expr);
    pt = expr;
    int x = parseExpr();
    assert(*pt == '=');
    printf("%d\\n", x);
  }
  return 0;
}

Error: -6
dd8a19a1-9216-493f-b140-8358d5817924.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p00109/C++/s251358907.cpp:30: int parseAtom(): Assertion `false' failed.


Fixed submission:

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <iostream>
using namespace std;

char *pt;
int parseAtom();
int parseExpr();
int parseTerm();

int parseAtom() {
  if ('0' <= *pt && *pt <= '9') {
    int x = *pt - '0';
    pt++;
    while ('0' <= *pt && *pt <= '9') {
      x = x * 10 + *pt - '0';
      pt++;
    }
    return x;
  }
  if ('(' == *pt) {
    pt++;
    int x = parseExpr();
    assert(*pt == ')');
    pt++;
    return x;
  }
  assert(false);
  return -1;
}
int parseExpr() {
  int x = parseTerm();
  while (*pt == '+' || *pt == '-') {
    if (*pt == '+') {
      pt++;
      x += parseTerm();
    } else {
      pt++;
      x -= parseTerm();
    }
  }
  return x;
}
int parseTerm() {
  int x = parseAtom();
  while (*pt == '*' || *pt == '/') {
    if (*pt == '*') {
      pt++;
      x *= parseAtom();
    } else {
      x /= parseAtom();
    }
  }
  return x;
}
int main(void) {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    static char expr[200];
    scanf("%d", expr);
    pt = expr;
    int x = parseExpr();
    assert(*pt == '=');
    cout << x << endl;
  }
  return 0;
}
Problem p00109 (C++) — buggy submission, verdict: Runtime Error

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

int Num();
int Fact();
int Term();
int Exp();

int ite;
string s;

int Num() {
  int res = 0;
  while (isdigit(s[ite])) {
    res = res * 10 + s[ite] - '0';
    ite++;
  }
  return res;
}

int Fact() {
  if (s[ite] == '(') {
    ite++;
    int res = Exp();
    ite++;
    return res;
  } else {
    return Num();
  }
}

int Term() {
  int res = Fact();
  while (true) {
    if (s[ite] == '*') {
      ite++;
      res *= Fact();
    } else if (s[ite] == '/') {
      ite++;
      res /= Fact();
    } else {
      break;
    }
  }
  return res;
}

int Exp() {
  int res = Term();
  while (s[ite] != '=' && s[ite] != ')') {
    if (s[ite] == '+') {
      ite++;
      res += Term();
    } else if (s[ite] == '-') {
      ite++;
      res -= Term();
    }
  }
  return res;
}

int main() {
  int n;
  cin >> n;
  while (n--) {
    cin >> s;
    ite = 0;
    cout << Exp() << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

int Num();
int Term();
int Exp();

int ite;
string s;

int Num() {
  int res = 0;
  while (isdigit(s[ite])) {
    res = res * 10 + s[ite] - '0';
    ite++;
  }
  return res;
}

int Term() {
  int res = Num();
  while (true) {
    if (s[ite] == '*') {
      ite++;
      if (s[ite] == '(') {
        ite++;
        res *= Exp();
        assert(s[ite] == ')');
        ite++;
      } else {
        res *= Num();
      }
    } else if (s[ite] == '/') {
      ite++;
      if (s[ite] == '(') {
        ite++;
        res /= Exp();
        assert(s[ite] == ')');
        ite++;
      } else {
        res /= Num();
      }
    } else {
      break;
    }
  }
  return res;
}

int Exp() {
  int res = Term();
  while (s[ite] != '=' && s[ite] != ')') {
    if (s[ite] == '+') {
      ite++;
      res += Term();
    } else if (s[ite] == '-') {
      ite++;
      res -= Term();
    } else {
      cout << "abyaaaa" << endl;
      assert(false);
    }
  }
  return res;
}

int main() {
  int n;
  cin >> n;
  while (n--) {
    cin >> s;
    ite = 0;
    cout << Exp() << endl;
  }
  return 0;
}
Problem p00110 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

typedef vector<int> vi;

int x;

vi str2vi(string s) {
  vi ret(127);

  reverse(s.begin(), s.end());

  for (int i = 0; i < 126; i++) {
    if (i < s.size())
      ret[i] = s[i] == 'X' ? x : s[i] - '0';
  }

  return ret;
}

vi pls(string s1, string s2) {
  vi ret(127);
  vi v1, v2;

  v1 = str2vi(s1), v2 = str2vi(s2);

  for (int i = 0; i < 126; i++) {
    ret[i] += v1[i] + v2[i];

    while (ret[i] > 9) {
      ret[i + 1]++;
      ret[i] -= 10;
    }
  }
  return ret;
}

bool check(string s) { return !(s.size() > 1 && s[0] == 'X'); }

int main() {
  string str;
  string s1, s2, rhs;
  while (cin >> str) {
    int ppos = str.find("+");
    int epos = str.find("=");
    rhs = str.substr(epos + 1);
    s1 = str.substr(0, ppos);
    s2 = str.substr(ppos + 1, epos - ppos - 1);

    // cout << s1 << " " << s2 << " " << rhs << endl;

    int ans = -1;
    for (x = 0; x < 10; x++) {
      if ((x || (check(s1) && check(s2) && check(rhs))) &&
          pls(s1, s2) == str2vi(rhs)) {
        ans = x;
        break;
      }
    }

    if (ans == -1)
      cout << "NA";
    else
      cout << ans;
    cout << endl;
  }

  return 0;
}

Error: -6
munmap_chunk(): invalid pointer


Fixed submission:

#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

string tonum(string s) {
  // tsukareta
}

string tosum(string s, string t) {
  // tsukareta
}

int main() {
  string str;

  while (getline(cin, str)) {
    bool fin = false;

    string::size_type pos = str.find("+");
    string::size_type pos2 = str.find("=");
    string s = str.substr(0, pos);
    string t = str.substr(pos + 1, pos2 - pos - 1);
    string r = str.substr(pos2 + 1);

    int st = 0;
    if ((s.size() > 1 && s[0] == 'X') || (t.size() > 1 && t[0] == 'X') ||
        (r.size() > 1 && r[0] == 'X'))
      st = 1;

    for (int i = st; i < 10; i++) {

      string ss;
      for (int j = 0; j < s.size(); j++) {
        if (s[j] == 'X')
          ss.push_back('0' + i);
        else
          ss.push_back(s[j]);
      }

      string tt;
      for (int j = 0; j < t.size(); j++) {
        if (t[j] == 'X')
          tt.push_back('0' + i);
        else
          tt.push_back(t[j]);
      }

      string rr;
      for (int j = 0; j < r.size(); j++) {
        if (r[j] == 'X')
          rr.push_back('0' + i);
        else
          rr.push_back(r[j]);
      }

      if (tosum(ss, tt) == tonum(rr)) {
        cout << i << endl;
        fin = true;
      }
      if (fin)
        break;
    }
    if (!fin)
      cout << "NA" << endl;
  }

  return 0;
}
Problem p00110 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string>

using namespace std;

string hissan(string a, string b) {
  if (a.size() < b.size())
    a.swap(b);
  int bi = b.size() - 1;
  int c = 0;
  string ans;

  for (int i = a.size() - 1; i >= 0; i--) {
    int _a = a[i] - '0';
    int _b = (bi >= 0) ? b[bi--] - '0' : 0;
    int x = _a + _b + c;

    if (x < 10) {
      c = 0;
    } else {
      x -= 10;
      c = 1;
    }
    ans.insert(0, to_string(x));
  }
  if (c)
    ans.insert(0, "1");

  return ans;
}

string replaceX(string s, int x) {
  replace(s.begin(), s.end(), 'X', (char)(x + '0'));
  return s;
}

bool c(string s) { return s[0] == 'X' && s.size() > 1; }

int main() {
  string s, x, y, ans;

  while (cin >> s) {
    int pos = -1;
    int eq = s.find("=");
    int plus = s.find("+");

    x = s.substr(0, plus);
    y = s.substr(plus + 1, eq - plus - 1);
    ans = s.substr(eq + 1);

    for (int i = c(x) || c(y) || c(ans); i <= 9; i++) {
      string _x = replaceX(x, i);
      string _y = replaceX(y, i);
      string _ans = replaceX(ans, i);
      // cout << _x << " + " << _y << " = " << _ans << endl;
      if (hissan(_x, _y) == _ans) {
        pos = i;
        break;
      }
    }
    cout << ((pos < 0) ? "NA" : to_string(pos)) << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

int assignX(string s, int x) {
  replace(s.begin(), s.end(), 'X', (char)(x + '0'));
  return stoi(s);
}

int main() {
  string s;

  while (cin >> s) {
    int eq = s.find("=");
    int plus = s.find("+");
    int ans = -1;

    string x = s.substr(0, plus);
    string y = s.substr(plus + 1, eq - plus - 1);
    string r = s.substr(eq + 1);

    for (int i = 0; i <= 9; i++) {
      if (i == 0 && (x.at(0) == 'X' || y.at(0) == 'X' || r.at(0) == 'X')) {
        continue;
      }
      if (assignX(x, i) + assignX(y, i) == assignX(r, i)) {
        ans = i;
        break;
      }
    }
    cout << ((ans < 0) ? "NA" : to_string(ans)) << endl;
  }
  return 0;
}
Problem p00110 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <sstream>
using namespace std;
bool f(string a, string b, string c, int x) {
  int crr = 0;
  for (int i = 0; i < (int)c.length(); i++) {
    int n, m, l;
    n = (i >= (int)a.length()) ? 0 : (a[i] == 'X') ? x : a[i] - '0';
    m = (i >= (int)b.length()) ? 0 : (b[i] == 'X') ? x : b[i] - '0';
    l = (i >= (int)c.length()) ? 0 : (c[i] == 'X') ? x : c[i] - '0';
    if ((n + m + crr) % 10 != l)
      return false;
    crr = (n + m + crr) / 10;
  }
  return true;
}
int main() {
  string str;
  while (cin >> str) {
    if (str == "END")
      break;
    string a, b, c;
    int flg = 0;
    for (int i = 0; i < (int)str.length(); i++) {
      if (str[i] == '+') {
        a = str.substr(0, i);
        reverse(a.begin(), a.end());
      } else if (str[i] == '=') {
        b = str.substr(a.length() + 1, i - a.length() - 1);
        c = str.substr(i + 1, str.length() - i);
        reverse(b.begin(), b.end());
        reverse(c.begin(), c.end());
      }
    }
    if (!((a.length() != 1 && (*(a.end() - 1) == 'X')) ||
          (b.length() != 1 && (*(b.end() - 1) == 'X')) ||
          (c.length() != 1 && (*(c.end() - 1) == 'X')))) {
      if (f(a, b, c, 0)) {
        cout << 0 << endl;
        flg = 1;
      }
    }
    for (int i = 1; i <= 9 && !flg; i++) {
      if (f(a, b, c, i)) {
        cout << i << endl;
        flg = 1;
      }
    }
    if (!flg)
      cout << "NA" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <math.h>
#include <string.h>
using namespace std;
int main() {
  while (1) {
    char e[130];
    int n, i, j = 0, a = 0, b = 0, c = 0, x, pl, eq, k;
    cin >> e;
    if (e[0] == '0')
      break;
    n = strlen(e);
    for (i = 0; i < n; i++) {
      if (e[i] == '+') {
        pl = i;
      } else if (e[i] == '=') {
        eq = i;
      }
    }
    if (eq - pl > 2 && e[pl + 1] == '0')
      break;
    if (e[eq + 1] == '0')
      break;
    for (i = 0; i <= 9; i++) {
      a = 0;
      b = 0;
      c = 0;
      for (k = 0, j = pl - 1; j >= 0; j--, k++) { // aに数値代入
        if (e[j] == 'X') {
          a += i * (int)pow(10, (double)k);
        } else {
          a += (e[j] - '0') * (int)pow(10, (double)k);
        }
      }
      for (k = 0, j = eq - 1; j > pl; j--, k++) { // bに数値代入
        if (e[j] == 'X') {
          b += i * (int)pow(10, (double)k);
        } else {
          b += (e[j] - '0') * (int)pow(10, (double)k);
        }
      }
      for (k = 0, j = n - 1; j > eq; j--, k++) { // cに数値代入
        if (e[j] == 'X') {
          c += i * (int)pow(10, (double)k);
        } else {
          c += (e[j] - '0') * (int)pow(10, (double)k);
        }
      }
      if (a + b == c) {
        x = i;
        break;
      }
      if (i == 9) {
        x = 100;
      }
    }
    if (x == 100) {
      cout << "NA" << endl;
    } else {
      cout << x << endl;
    }
  }
  return 0;
}
Problem p00110 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) REP(i, 0, n)

using namespace std;

char buf[128], a[128], b[128], c[128];
char p[128], q[128], r[128];
int main() {
  while (scanf("%s", buf) != EOF) {
    int al = strlen(strcpy(a, strtok(buf, "+=\\n")));
    int bl = strlen(strcpy(b, strtok(NULL, "+=\\n")));
    int cl = strlen(strcpy(c, strtok(NULL, "+=\\n")));

    int d = 0;
    if (al > 1 && a[0] == 'X' || bl > 1 && b[0] == 'X' || cl > 1 && c[0] == 'X')
      d = 1;
    int len = max(al, max(bl, cl)) + 1;
    reverse(a, a + al);
    REP(i, al, len) a[i] = '0';
    reverse(b, b + bl);
    REP(i, bl, len) b[i] = '0';
    reverse(c, c + cl);
    REP(i, cl, len) c[i] = '0';

    int res = -1;
    for (int i = d; i < 10; ++i) {
      rep(j, len) p[j] = (a[j] == 'X') ? i : a[j] - '0';
      rep(j, len) q[j] = (b[j] == 'X') ? i : b[j] - '0';
      rep(j, len) r[j] = (c[j] == 'X') ? i : c[j] - '0';
      int t = 0;
      bool found = true;
      rep(j, len) {
        int s = p[j] + q[j] + t;
        if (s >= 10) {
          s -= 10;
          t = 1;
        } else {
          t = 0;
        }
        if (s != r[j]) {
          found = false;
          break;
        }
      }
      if (found) {
        res = i;
        break;
      }
    }
    if (res == -1)
      puts("NA");
    else
      printf("%d\\n", res);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) REP(i, 0, n)
int a[2][13], n, f, c;
int main() {
  while (scanf("%d", &n), n) {
    rep(i, n) scanf("%d", &a[0][i]);
    c = f = 0;
    while (true) {
      bool ok = true;
      rep(i, n) if (a[0][i] != a[1][i]) ok = false;
      if (ok)
        break;
      rep(i, n) a[1 - f][i] = std::count(a[f], a[f] + n, a[f][i]);
      f = 1 - f;
      c++;
    }
    printf("%d\\n", c - 1);
    rep(i, n - 1) printf("%d ", a[f][i]);
    printf("%d\\n", a[f][n - 1]);
  }
  return 0;
}
Problem p00110 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::endl;
using std::getline;
using std::replace;
using std::reverse;
using std::stoll;
using std::string;

string big_add(string a, string b) {
  string res;
  auto ai = a.rbegin();
  auto bi = b.rbegin();
  char carry = '\\0';
  while (ai != a.rend() && bi != b.rend()) {
    res.push_back((*ai - '0' + *bi - '0' + carry) % (char)10 + '0');
    carry = (*(ai++) - '0' + *(bi++) - '0' + carry) / (char)10;
  }
  if (ai != a.rend() || bi != b.rend()) {
    auto i = ai != a.rend() ? ai : bi;
    string &s = ai != a.rend() ? a : b;
    while (i != s.rend()) {
      res.push_back((*i - '0' + carry) % (char)10 + '0');
      carry = (*(i++) - '0' + carry) / (char)10;
    }
  }
  if (carry != '\\0') {
    res.push_back('1');
  }
  reverse(res.begin(), res.end());
  return res;
}

bool x_is_right(string a, string b, string c, char x) {
  replace(a.begin(), a.end(), 'X', x);
  replace(b.begin(), b.end(), 'X', x);
  replace(c.begin(), c.end(), 'X', x);
  return big_add(a, b) == c;
}

int main() {
  string e; // expression
  while (getline(cin, e)) {
    auto ad_pos = e.find('+');
    auto eq_pos = e.find('=');
    string a = e.substr(0, ad_pos); // first number sequence
    string b =
        e.substr(ad_pos + 1, eq_pos - ad_pos - 1); // second number sequence
    string c = e.substr(eq_pos + 1);               // result number sequence
    string right_x;
    for (char x = (a[0] == 'X' && a.size() != 1) ||
                          (b[0] == 'X' && b.size() != 1) ||
                          (c[0] == 'X' && c.size() != 1)
                      ? '1'
                      : '0';
         x <= '9'; ++x) {
      if (x_is_right(a, b, c, x)) {
        right_x.assign(1, x);
        break;
      }
    }
    cout << (right_x.empty() ? "NA" : right_x) << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::endl;
using std::getline;
using std::replace;
using std::stoll;
using std::string;

bool x_is_right(string a, string b, string c, char x) {
  replace(a.begin(), a.end(), 'X', x);
  replace(b.begin(), b.end(), 'X', x);
  replace(c.begin(), c.end(), 'X', x);
  return stoll(a) + stoll(b) == stoll(c);
}

int main() {
  string e; // expression
  while (getline(cin, e)) {
    auto ad_pos = e.find('+');
    auto eq_pos = e.find('=');
    string a = e.substr(0, ad_pos); // first number sequence
    string b =
        e.substr(ad_pos + 1, eq_pos - ad_pos - 1); // second number sequence
    string c = e.substr(eq_pos + 1);               // result number sequence
    string right_x = "";
    for (char x = '0'; x <= '9'; ++x) {
      if (x_is_right(a, b, c, x)) {
        right_x.assign(1, x);
        break;
      }
    }
    cout << (right_x.empty() ? "NA" : right_x) << endl;
  }
}