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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define inf 0x7fffffff
#define eps 1e-9
#define ll long long
#define ld long double
#define pii pair<int, int>
#define x first
#define y second
#define sf scanf
#define pf printf
#define vec vector
#define pb push_back
#define mp make_pair
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define bin_cnt(x) __builtin_popcount(x)
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define rrep(i, b, a) for (int i = b; i >= a; i--)
#define irep(i, a) for (__typeof(a.begin()) i = a.begin(); i != a.end(); i++)
#pragma comment(linker, "/STACK:32505856")

template <class T> inline T sqr(T a) { return a * a; }
template <class T> inline void gmin(T &a, T b) {
  if (a > b)
    a = b;
}
template <class T> inline void gmax(T &a, T b) {
  if (a < b)
    a = b;
}
inline int sign(const double &a) { return a > eps ? 1 : (a < -eps ? -1 : 0); }
struct Initializer {
  Initializer() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
  }
  ~Initializer() {}
} initializer;

#define N 200020
string s;
int f[N][3][3][2];
int pos[] = {0, 0, 1, 2, 0, 1, 2, 0, 1, 2};

int main() {
  while (cin >> s) {
    if (s == "#")
      break;
    int n = sz(s);
    memset(f, 0x7f, sizeof(f));

    rep(l, 0, 2) rep(r, l, 2) { f[0][l][r][0] = f[0][l][r][1] = 0; }

    rep(i, 0, n - 1) {
      if (s[i] < '0' || '9' < s[i])
        continue;
      rep(l, 0, 2) rep(r, l, 2) {
        int p = pos[s[i] - '0'];
        if (p <= r) {
          gmin(f[i + 1][p][r][0], f[i][l][r][0] + 1);
          gmin(f[i + 1][p][r][0], f[i][l][r][1]);
        }
        if (l <= p) {
          gmin(f[i + 1][l][p][1], f[i][l][r][0]);
          gmin(f[i + 1][l][p][1], f[i][l][r][1] + 1);
        }
      }
    }

    int ans = inf;
    rep(l, 0, 2) rep(r, l, 2) {
      gmin(ans, f[n][l][r][0]);
      gmin(ans, f[n][l][r][1]);
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define inf 0x7fffffff
#define eps 1e-9
#define ll long long
#define ld long double
#define pii pair<int, int>
#define x first
#define y second
#define sf scanf
#define pf printf
#define vec vector
#define pb push_back
#define mp make_pair
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define bin_cnt(x) __builtin_popcount(x)
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define rrep(i, b, a) for (int i = b; i >= a; i--)
#define irep(i, a) for (__typeof(a.begin()) i = a.begin(); i != a.end(); i++)
#pragma comment(linker, "/STACK:32505856")

template <class T> inline T sqr(T a) { return a * a; }
template <class T> inline void gmin(T &a, T b) {
  if (a > b)
    a = b;
}
template <class T> inline void gmax(T &a, T b) {
  if (a < b)
    a = b;
}
inline int sign(const double &a) { return a > eps ? 1 : (a < -eps ? -1 : 0); }
struct Initializer {
  Initializer() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
  }
  ~Initializer() {}
} initializer;

#define N 100010
char s[N];
int f[N][3][3][2];
int pos[] = {0, 0, 1, 2, 0, 1, 2, 0, 1, 2};

int main() {
  while (~sf("%s", s + 1)) {
    if (s[1] == '#')
      break;
    int n = strlen(s + 1);
    memset(f, 0x7f, sizeof(f));

    rep(l, 0, 2) rep(r, l, 2) { f[0][l][r][0] = f[0][l][r][1] = 0; }

    rep(i, 0, n - 1) {
      memset(f[i + 1], 0x7f, sizeof(f));
      rep(l, 0, 2) rep(r, l, 2) {
        int p = pos[s[i + 1] - '0'];
        if (p <= r) {
          gmin(f[i + 1][p][r][0], f[i][l][r][0] + 1);
          gmin(f[i + 1][p][r][0], f[i][l][r][1]);
        }
        if (l <= p) {
          gmin(f[i + 1][l][p][1], f[i][l][r][0]);
          gmin(f[i + 1][l][p][1], f[i][l][r][1] + 1);
        }
      }
    }

    int ans = inf;
    rep(l, 0, 2) rep(r, l, 2) {
      gmin(ans, f[n][l][r][0]);
      gmin(ans, f[n][l][r][1]);
    }
    pf("%d\\n", ans);
  }
  return 0;
}