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

#include <bits/stdc++.h>
#include <regex>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) begin(a), end(a)
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef complex<double> Point;
typedef long long ll;
const int INF = 1145141919;
const int MOD = 100000007;
const double EPS = 1e-10;
const double PI = acos(-1.0);
struct edge {
  int from, to, cost;
  bool operator<(const edge &e) const { return cost < e.cost; }
  bool operator>(const edge &e) const { return cost > e.cost; }
};
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///

string s;
int dp[111111][2];

int main() {
  while (cin >> s) {
    if (s == "#")
      break;
    REP(i, s.size()) REP(j, 2) dp[i][j] = (i == 0 ? 0 : INF);
    FOR(i, 1, s.size()) {
      int t = (s[i] - '1') % 3;
      int p = (s[i - 1] - '1') % 3;
      dp[i][0] = dp[i - 1][0] + 1;
      dp[i][1] = dp[i - 1][1] + 1;
      if (t <= p)
        dp[i][0] = min(dp[i][0], dp[i - 1][1]);
      if (t >= p)
        dp[i][1] = min(dp[i][1], dp[i - 1][0]);
    }
    cout << min(dp[s.size() - 1][0], dp[s.size() - 1][1]) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <regex>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) begin(a), end(a)
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef complex<double> Point;
typedef long long ll;
const int INF = 1145141919;
const int MOD = 100000007;
const double EPS = 1e-10;
const double PI = acos(-1.0);
struct edge {
  int from, to, cost;
  bool operator<(const edge &e) const { return cost < e.cost; }
  bool operator>(const edge &e) const { return cost > e.cost; }
};
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///

string s;
int dp[111111][3][3][2];

int solve(int i, int l, int r, int p) {
  if (i == s.size())
    return 0;
  if (dp[i][l][r][p] >= 0)
    return dp[i][l][r][p];
  int t;
  int k = s[i] - '0';
  if (k % 3 == 1)
    t = 0;
  else if (k % 3 == 2)
    t = 1;
  else
    t = 2;
  int res;
  if (t < l)
    res = solve(i + 1, t, r, 0) + (p ? 0 : 1);
  else if (t > r)
    res = solve(i + 1, l, t, 1) + (p ? 1 : 0);
  else
    res = min(solve(i + 1, t, r, 0) + (p ? 0 : 1),
              solve(i + 1, l, t, 1) + (p ? 1 : 0));
  return dp[i][l][r][p] = res;
}

int main() {
  while (cin >> s) {
    if (s == "#")
      break;
    MS(dp, -1);
    int a = solve(0, 1, 1, 0);
    MS(dp, -1);
    int b = solve(0, 1, 1, 1);
    cout << min(a, b) << endl;
  }
  return 0;
}