Problem p03392 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
// #include <bits/stdc++.h>
using namespace std;
#define rank rankk
#define mp make_pair
#define pb push_back
#define xo(a, b) ((b)&1 ? (a) : 0)
#define tm tmp

// #define LL ll
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef long long ll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<ll, ll> pll;
// const double INF=1e20;
const int INF = 0x3f3f3f3f;
// const int INF= 0x7fffffff;
// const ll INF=0x3f3f3f3f3f3f3f3fll;
const ll INFF = 0x3f3f3f3f3f3f3fll;
// const ll INFF=1e14+5;
const int MAX = 3e5 + 5;
// const ll MAXN=2e8;
// const int MAX_N=MAX;
// const ll MOD=1e9+7;
const ll MOD = 998244353;
// const long double pi=acos(-1.0);
// const double eps=0.00000001;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
template <typename T> inline T abs(T a) { return a > 0 ? a : -a; }
// #define double long double
template <class T> inline void read(T &num) {
  bool start = false, neg = false;
  char c;
  num = 0;
  while ((c = getchar()) != EOF) {
    if (c == '-')
      start = neg = true;
    else if (c >= '0' && c <= '9') {
      start = true;
      num = num * 10 + c - '0';
    } else if (start)
      break;
  }
  if (neg)
    num = -num;
}
inline ll powMM(ll a, ll b, ll M) {
  ll ret = 1;
  a %= M;
  //    b%=M;
  while (b) {
    if (b & 1)
      ret = ret * a % M;
    b >>= 1;
    a = a * a % M;
  }
  return ret;
}
// const long double eps=-1.0;
// clock_t t1 = clock();
// fprintf(stderr, "%ld ms\n", clock() - t1);
void open() {
  freopen("in2.txt", "r", stdin);
  freopen("out2.txt", "w", stdout);
}
char s[MAX];
int c[MAX];
ll dp[MAX][4][4][3]; // lo summod3 last3  adj=
char CH = 'a';
int tot;
bool adj;
ll an;
bool vi[1305];
int len;
void dfs() {
  int now = 0;
  for (int i = 1; i <= len; i++)
    now = now * 3 + c[i];
  if (vi[now])
    return;
  else
    ++an;
  for (int j = 1; j <= len - 1; j++)
    if (c[j] != c[j + 1]) {
      int x = c[j], y = c[j + 1];
      for (int t = 0; t < 3; t++)
        if (t != c[j] && t != c[j + 1]) {
          c[j] = c[j + 1] = t;
          break;
        }
      dfs();
      c[j] = x;
      c[j + 1] = y;
    }
}
set<int> em;
int main() {
  scanf("%s", s + 1);
  len = strlen(s + 1);

  for (int i = 1; i <= len; i++) {
    c[i] = s[i] - 'a';
    (tot += c[i]) %= 3;
    if (i > 1 && c[i] == c[i - 1])
      adj = 1;
    em.insert(c[i]);
  }
  tot %= 3;
  if (em.size() == 1)
    return 0 * printf("1\n");
  if (len <= 3) {
    dfs();
    printf("%lld\n", an);
    return 0;
  }
  dp[1][0][0][0] = dp[1][1][1][0] = dp[1][2][2][0] = 1;
  for (int i = 2; i <= len; i++) {
    for (int j = 0; j < 3; j++)   // this
      for (int k = 0; k < 3; k++) // presum
      {
        int sum = (j + k) % 3;
        for (int p = 0; p < 3; p++)
          if (p != j)
            (dp[i][sum][j][0] += dp[i - 1][k][p][0]) %= MOD;
          else
            dp[i][sum][j][1] = dp[i - 1][k][p][0];
        for (int p = 0; p < 3; p++)
          (dp[i][sum][j][1] += dp[i - 1][k][p][1]) %= MOD;
      }
  }
  for (int i = 0; i < 3; i++)
    an += dp[len][tot][i][1];
  if (!adj)
    ++an;
  an %= MOD;
  printf("%lld\n", an);
}
/*

*/


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
// #include <bits/stdc++.h>
using namespace std;
#define rank rankk
#define mp make_pair
#define pb push_back
#define xo(a, b) ((b)&1 ? (a) : 0)
#define tm tmp

// #define LL ll
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef long long ll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<ll, ll> pll;
// const double INF=1e20;
const int INF = 0x3f3f3f3f;
// const int INF= 0x7fffffff;
// const ll INF=0x3f3f3f3f3f3f3f3fll;
const ll INFF = 0x3f3f3f3f3f3f3fll;
// const ll INFF=1e14+5;
const int MAX = 3e5 + 5;
// const ll MAXN=2e8;
// const int MAX_N=MAX;
// const ll MOD=1e9+7;
const ll MOD = 998244353;
// const long double pi=acos(-1.0);
// const double eps=0.00000001;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
template <typename T> inline T abs(T a) { return a > 0 ? a : -a; }
// #define double long double
template <class T> inline void read(T &num) {
  bool start = false, neg = false;
  char c;
  num = 0;
  while ((c = getchar()) != EOF) {
    if (c == '-')
      start = neg = true;
    else if (c >= '0' && c <= '9') {
      start = true;
      num = num * 10 + c - '0';
    } else if (start)
      break;
  }
  if (neg)
    num = -num;
}
inline ll powMM(ll a, ll b, ll M) {
  ll ret = 1;
  a %= M;
  //    b%=M;
  while (b) {
    if (b & 1)
      ret = ret * a % M;
    b >>= 1;
    a = a * a % M;
  }
  return ret;
}
// const long double eps=-1.0;
// clock_t t1 = clock();
// fprintf(stderr, "%ld ms\n", clock() - t1);
void open() {
  freopen("in2.txt", "r", stdin);
  freopen("out2.txt", "w", stdout);
}
char s[MAX];
int c[MAX];
ll dp[MAX][4][4][3]; // lo summod3 last3  adj=
char CH = 'a';
int tot;
bool adj;
ll an;
bool vi[1305];
int len;
void dfs() {
  int now = 0;
  for (int i = 1; i <= len; i++)
    now = now * 3 + c[i];
  if (vi[now])
    return;
  else
    ++an, vi[now] = 1;
  for (int j = 1; j <= len - 1; j++)
    if (c[j] != c[j + 1]) {
      int x = c[j], y = c[j + 1];
      for (int t = 0; t < 3; t++)
        if (t != c[j] && t != c[j + 1]) {
          c[j] = c[j + 1] = t;
          break;
        }
      dfs();
      c[j] = x;
      c[j + 1] = y;
    }
}
set<int> em;
int main() {
  scanf("%s", s + 1);
  len = strlen(s + 1);

  for (int i = 1; i <= len; i++) {
    c[i] = s[i] - 'a';
    (tot += c[i]) %= 3;
    if (i > 1 && c[i] == c[i - 1])
      adj = 1;
    em.insert(c[i]);
  }
  tot %= 3;
  if (em.size() == 1)
    return 0 * printf("1\n");
  if (len <= 3) {
    dfs();
    printf("%lld\n", an);
    return 0;
  }
  dp[1][0][0][0] = dp[1][1][1][0] = dp[1][2][2][0] = 1;
  for (int i = 2; i <= len; i++) {
    for (int j = 0; j < 3; j++)   // this
      for (int k = 0; k < 3; k++) // presum
      {
        int sum = (j + k) % 3;
        for (int p = 0; p < 3; p++)
          if (p != j)
            (dp[i][sum][j][0] += dp[i - 1][k][p][0]) %= MOD;
          else
            dp[i][sum][j][1] = dp[i - 1][k][p][0];
        for (int p = 0; p < 3; p++)
          (dp[i][sum][j][1] += dp[i - 1][k][p][1]) %= MOD;
      }
  }
  for (int i = 0; i < 3; i++)
    an += dp[len][tot][i][1];
  if (!adj)
    ++an;
  an %= MOD;
  printf("%lld\n", an);
}
/*

*/