Problem p03392 (C++)

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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount

#define INF 1e16
#define mod 998244353

int n;
string s;
map<string, bool> vis;
ll res = 0;
ll dp[100010][3][3][2];

void dfs(string t) {
  res++;
  vis[t] = true;
  rep(i, n - 1) {
    if (t[i] == t[i + 1])
      continue;
    string nxt = t;
    repl(c, 'a', 'c' + 1) {
      if (c != t[i] && c != t[i + 1])
        nxt[i] = nxt[i + 1] = c;
    }
    if (!vis[nxt])
      dfs(nxt);
  }
}

int main() {
  cin >> s;
  n = s.size();
  bool same = true;
  rep(i, n - 1) {
    if (s[i] != s[i + 1])
      same = false;
  }
  if (same) {
    cout << 1 << endl;
    return 0;
  }
  if (n <= 3) {
    dfs(s);
  } else {
    rep(i, 3) dp[1][i][i][0] = 1;
    repl(i, 1, n) rep(j, 3) rep(k, 3) rep(l, 2) {
      rep(nxt, 3) {
        (dp[i + 1][(j + nxt) % 3][nxt][l || (nxt == k)] += dp[i][j][k][l]) %=
            mod;
      }
    }
    ll v = 0;
    bool f = false;
    rep(i, n) {
      v += s[i] - 'a';
      if (i < n - 1 && s[i] == s[i + 1])
        f |= 1;
    }
    v %= 3;
    rep(i, 3)(res += dp[n][v][i][1]) %= mod;
    res += (!f);
  }
  cout << res % mod << endl;
  return 0;
}


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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount

#define INF 1e16
#define mod 998244353

int n;
string s;
map<string, bool> vis;
ll res = 0;
ll dp[200010][3][3][2];

void dfs(string t) {
  res++;
  vis[t] = true;
  rep(i, n - 1) {
    if (t[i] == t[i + 1])
      continue;
    string nxt = t;
    repl(c, 'a', 'c' + 1) {
      if (c != t[i] && c != t[i + 1])
        nxt[i] = nxt[i + 1] = c;
    }
    if (!vis[nxt])
      dfs(nxt);
  }
}

int main() {
  cin >> s;
  n = s.size();
  bool same = true;
  rep(i, n - 1) {
    if (s[i] != s[i + 1])
      same = false;
  }
  if (same) {
    cout << 1 << endl;
    return 0;
  }
  if (n <= 3) {
    dfs(s);
  } else {
    rep(i, 3) dp[1][i][i][0] = 1;
    repl(i, 1, n) rep(j, 3) rep(k, 3) rep(l, 2) {
      rep(nxt, 3) {
        (dp[i + 1][(j + nxt) % 3][nxt][l || (nxt == k)] += dp[i][j][k][l]) %=
            mod;
      }
    }
    ll v = 0;
    bool f = false;
    rep(i, n) {
      v += s[i] - 'a';
      if (i < n - 1 && s[i] == s[i + 1])
        f |= 1;
    }
    v %= 3;
    rep(i, 3)(res += dp[n][v][i][1]) %= mod;
    res += (!f);
  }
  cout << res % mod << endl;
  return 0;
}