Problem p00110 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < n; i++)
#define dump(x) cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000

int i = 0;
string str;

string takeout(char a) {
  if (i != 0)
    i++;
  stringstream tmp;
  while (str[i] != a) {
    tmp << str[i];
    i++;
  }
  return tmp.str();
}

string change(string str, char a) {
  int j = 0;
  while (str[j] != '\\0') {
    if (str[j] == 'X')
      str[j] = a;
    j++;
  }
  return str;
}

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

  while (cin >> str) {
    i = 0;
    string L = takeout('+');
    string R = takeout('=');
    string ANS = takeout('\\0');

    int flag = 0;
    int j;
    for (int i = 9; i >= 0; i--) {
      string tmp1, tmp2, tmp3;
      tmp1 = change(L, '0' + i);
      tmp2 = change(R, '0' + i);
      tmp3 = change(ANS, '0' + i);
      if (i == 0) {
        // dump(R.length());
        if (L.length() > 1) {
          if (L[0] == 'X')
            break;
        }
        if (R.length() > 1) {
          // dump(R[0]);
          if (R[0] == 'X')
            break;
        }
        if (ANS.length() > 1) {
          if (ANS[0] == 'X')
            break;
        }
      }

      int movesup = 0;
      int len1 = tmp1.length() - 1;
      int len2 = tmp2.length() - 1;
      int len3 = tmp3.length() - 1;
      int MAX_len = max(len1, max(len2, len3)) + 1;
      // dump(MAX_len);
      j = 0;
      while (len1 >= 0 || len2 >= 0 || len3 >= 0) {
        int tmpL = 0, tmpR = 0, tmpA = movesup;
        if (len1 >= 0)
          tmpL = tmp1[len1] - '0';
        if (len2 >= 0)
          tmpR = tmp2[len2] - '0';
        if (len3 >= 0)
          tmpA = tmp3[len3] - '0' - movesup;
        /*
        dump(j)
        dump(tmpL);
        dump(tmpR);
        dump(tmpA);
        */
        if (tmpL + tmpR == tmpA) {
          j++;
          movesup = 0;
        } else if (tmpL + tmpR == 10 + tmpA) {
          j++;
          movesup = 1;
        } else if (tmpL + tmpR == 20 + tmpA) {
          j++;
          movesup = 2;
        } else
          break;
        len1 = tmp1.length() - j - 1;
        len2 = tmp2.length() - j - 1;
        len3 = tmp3.length() - j - 1;
      }
      if (j == MAX_len) {
        cout << i << endl;
        flag = 1;
        break;
      }
    }
    if (flag == 0)
      cout << "NA" << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < n; i++)
#define dump(x) cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000

int i = 0;
string str;

string takeout(char a) {
  if (i != 0)
    i++;
  stringstream tmp;
  while (str[i] != a) {
    tmp << str[i];
    i++;
  }
  return tmp.str();
}

string change(string str, char a) {
  int j = 0;
  while (str[j] != '\\0') {
    if (str[j] == 'X')
      str[j] = a;
    j++;
  }
  return str;
}

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

  while (cin >> str) {
    i = 0;
    string L = takeout('+');
    string R = takeout('=');
    string ANS = takeout('\\0');

    int flag = 0;
    for (int i = 0; i < 10; i++) {
      string tmp1, tmp2, tmp3;
      tmp1 = change(L, '0' + i);
      tmp2 = change(R, '0' + i);
      tmp3 = change(ANS, '0' + i);
      if (stoll(tmp1) + stoll(tmp2) == stoll(tmp3)) {
        cout << i << endl;
        flag = 1;
        break;
      }
    }
    if (flag == 0)
      cout << "NA" << endl;
  }

  return 0;
}