prithivMLmods/Coder-Stat huggingface dataset:
Problem p03290 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

typedef long long ll;

int main() {
  freopen("input.txt", "r", stdin);

  int D, G;
  cin >> D >> G;
  ll problems[D];
  ll bonuses[D];

  for (int i = 0; i < D; i++) {
    cin >> problems[i];
    cin >> bonuses[i];
  }

  // まずどの問題のボーナスポイントをとるか決める
  // 2^D通りある
  ll ans = 1 << 29;
  for (int bit = 0; bit < (1 << D); bit++) {
    ll sum = 0, num = 0;
    for (int i = 0; i < D; i++) {
      // bitがたっていたら全部解く
      if (bit & (1 << i)) {
        sum += problems[i] * 100 * (i + 1) + bonuses[i];
        num += problems[i];
      }
    }

    if (sum >= G) {
      ans = min(ans, num);
    } else {
      // まだ目標に届いていない場合は貪欲に
      for (int i = D - 1; i >= 0; i--) {
        if (bit & (1 << i))
          continue;
        for (int j = 0; j < problems[i]; j++) {
          if (sum >= G) {
            break;
          }
          sum += (i + 1) * 100;
          num++;
        }
      }

      ans = min(ans, num);
    }
  }

  cout << ans;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

typedef long long ll;

int main() {
  // freopen("input.txt", "r", stdin);

  int D, G;
  cin >> D >> G;
  ll problems[D];
  ll bonuses[D];

  for (int i = 0; i < D; i++) {
    cin >> problems[i];
    cin >> bonuses[i];
  }

  // まずどの問題のボーナスポイントをとるか決める
  // 2^D通りある
  ll ans = 1 << 29;
  for (int bit = 0; bit < (1 << D); bit++) {
    ll sum = 0, num = 0;
    for (int i = 0; i < D; i++) {
      // bitがたっていたら全部解く
      if (bit & (1 << i)) {
        sum += problems[i] * 100 * (i + 1) + bonuses[i];
        num += problems[i];
      }
    }

    if (sum >= G) {
      ans = min(ans, num);
    } else {
      // まだ目標に届いていない場合は貪欲に
      for (int i = D - 1; i >= 0; i--) {
        if (bit & (1 << i))
          continue;
        for (int j = 0; j < problems[i]; j++) {
          if (sum >= G) {
            break;
          }
          sum += (i + 1) * 100;
          num++;
        }
      }

      ans = min(ans, num);
    }
  }

  cout << ans;
  return 0;
}
Problem p03290 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;

#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define FORR(i, a, b) for (int i = b - 1; i >= a; --i)
#define REP(i, n) FOR(i, 0, n)
#define REPR(i, n) FORR(i, 0, n)

template <class T> inline void read(T &x) {
  T s = 0, base = 1;
  char c;
  while (!isdigit(c = getchar()))
    if (c == '-')
      base = -base;
  while (isdigit(c)) {
    s = s * 10 + (c ^ 48);
    c = getchar();
  }
  x = base * s;
}

int D, G;
int p[10], c[10];

int main() {
  read(D);
  read(G);
  G /= 100;

  REP(i, D) {
    read(p[i]);
    read(c[i]);
    c[i] /= 100;
  }

  int ans = numeric_limits<int>::max();
  for (int i = 0; i < (1 << D); ++i) {
    // i: 使いきったか否かを表すビット
    int sum = 0;
    int cnt = 0;

    for (int j = 0; j < D; ++D) {
      if ((i >> j) & 1) {
        sum += p[j] * (j + 1) + c[j];
        cnt += p[j];
      }
    }

    for (int j = D - 1; j >= 0; --j) {
      if ((i >> j) & 1)
        continue;
      if (sum >= G)
        break;
      int buf = min((G - sum) / (j + 1), p[j]);
      cnt += buf;
      sum += (j + 1) * buf;
    }

    if (G <= sum)
      ans = min(ans, cnt);
  }

  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;

#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define FORR(i, a, b) for (int i = b - 1; i >= a; --i)
#define REP(i, n) FOR(i, 0, n)
#define REPR(i, n) FORR(i, 0, n)

template <class T> inline void read(T &x) {
  T s = 0, base = 1;
  char c;
  while (!isdigit(c = getchar()))
    if (c == '-')
      base = -base;
  while (isdigit(c)) {
    s = s * 10 + (c ^ 48);
    c = getchar();
  }
  x = base * s;
}

int D, G;
int p[10], c[10];

int main() {
  read(D);
  read(G);
  G /= 100;

  REP(i, D) {
    read(p[i]);
    read(c[i]);
    c[i] /= 100;
  }

  int ans = numeric_limits<int>::max();
  for (int i = 0; i < (1 << D); ++i) {
    // i: 使いきったか否かを表すビット
    int sum = 0;
    int cnt = 0;

    for (int j = 0; j < D; ++j) {
      if ((i >> j) & 1) {
        sum += p[j] * (j + 1) + c[j];
        cnt += p[j];
      }
    }

    for (int j = D - 1; j >= 0; --j) {
      if ((i >> j) & 1)
        continue;
      if (sum >= G)
        break;
      int buf = min((G - sum) / (j + 1), p[j]);
      cnt += buf;
      sum += (j + 1) * buf;
    }

    if (G <= sum)
      ans = min(ans, cnt);
  }

  printf("%d\n", ans);
  return 0;
}
Problem p03290 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;

typedef struct {
  int q, c;
} Problem;

typedef long long ll;
const int N = 11;
Problem problems[N];
const int S = 1e7;
int dp[N][S];
bool used[N][S];
int d, g;
const int inf = 1e9;

int solve(int i, int s) {
  if (s >= g) {
    return 0LL;
  }

  if (i >= d) {
    return inf;
  }

  int &ans = dp[i][s];

  if (used[i][s]) {
    return ans;
  }

  ans = inf;

  for (int k = 0; k <= problems[i].q; k++) {
    int add = 0;

    if (k == problems[i].q) {
      add = problems[i].c;
    }

    ans = min(ans, k + solve(i + 1, (i + 1) * k + add + s));
  }

  return ans;
}

int main() {

  cin >> d >> g;

  g /= 100;

  for (int i = 0; i < d; i++) {
    cin >> problems[i].q >> problems[i].c;
    problems[i].c /= 100;
  }

  cout << solve(0, 0) << endl;

  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;

typedef struct {
  int q, c;
} Problem;

typedef long long ll;
const int N = 11;
Problem problems[N];
const int S = 1e7;
int dp[N][S];
bool used[N][S];
int d, g;
const int inf = 1e9;

int solve(int i, int s) {
  if (s >= g) {
    return 0LL;
  }

  if (i >= d) {
    return inf;
  }

  int &ans = dp[i][s];

  if (used[i][s]) {
    return ans;
  }

  used[i][s] = true;

  ans = inf;

  for (int k = 0; k <= problems[i].q; k++) {
    int add = 0;

    if (k == problems[i].q) {
      add = problems[i].c;
    }

    ans = min(ans, k + solve(i + 1, (i + 1) * k + add + s));
  }

  return ans;
}

int main() {

  cin >> d >> g;

  g /= 100;

  for (int i = 0; i < d; i++) {
    cin >> problems[i].q >> problems[i].c;
    problems[i].c /= 100;
  }

  cout << solve(0, 0) << endl;

  return 0;
}
Problem p03290 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;

constexpr int INF = 1e9;
constexpr int MAX_SCORE = 101010;
int score_cnt[MAX_SCORE];

int main() {
  int d, g;
  cin >> d >> g;
  g /= 100;

  for (int i = 0; i < MAX_SCORE; i++) {
    score_cnt[i] = INF;
  }

  score_cnt[0] = 0;

  for (int i = 1; i <= d; i++) {
    int p, c;
    cin >> p >> c;
    c /= 100;
    // すでに解かれてる問題＋その問題
    for (int j = MAX_SCORE - 1; j >= 0; j--) {
      if (score_cnt[j] != INF) {
        for (int k = 1; k <= p; k++) {
          if (j + i * k < MAX_SCORE) {
            score_cnt[j + i * k] = min(score_cnt[j + i * k], score_cnt[j] + k);
          }
        }
        if (j + i * p < MAX_SCORE) {
          score_cnt[j + i * p + c] =
              min(score_cnt[j + i * p + c], score_cnt[j] + p);
        }
      }
    }

    // その問題だけ
    for (int j = 1; j <= p; j++) {
      score_cnt[i * j] = min(score_cnt[i * j], j);
    }
    // 全完した場合
    score_cnt[i * p + c] = min(score_cnt[i * p + c], p);
  }

  int ans = 1e9;
  for (int i = g; i < MAX_SCORE; i++) {
    ans = min(ans, score_cnt[i]);
  }
  cout << ans << endl;
}


Fixed submission:
#include <iostream>
#include <vector>
using namespace std;

constexpr int INF = 1e9;
constexpr int MAX_SCORE = 1010101;
int score_cnt[MAX_SCORE];

int main() {
  int d, g;
  cin >> d >> g;
  g /= 100;

  for (int i = 0; i < MAX_SCORE; i++) {
    score_cnt[i] = INF;
  }

  score_cnt[0] = 0;

  for (int i = 1; i <= d; i++) {
    int p, c;
    cin >> p >> c;
    c /= 100;
    // すでに解かれてる問題＋その問題
    for (int j = MAX_SCORE - 1; j >= 0; j--) {
      if (score_cnt[j] != INF) {
        for (int k = 1; k <= p; k++) {
          if (j + i * k < MAX_SCORE) {
            score_cnt[j + i * k] = min(score_cnt[j + i * k], score_cnt[j] + k);
          }
        }
        if (j + i * p < MAX_SCORE) {
          score_cnt[j + i * p + c] =
              min(score_cnt[j + i * p + c], score_cnt[j] + p);
        }
      }
    }

    // その問題だけ
    for (int j = 1; j <= p; j++) {
      score_cnt[i * j] = min(score_cnt[i * j], j);
    }
    // 全完した場合
    score_cnt[i * p + c] = min(score_cnt[i * p + c], p);
  }

  int ans = 1e9;
  for (int i = g; i < MAX_SCORE; i++) {
    ans = min(ans, score_cnt[i]);
  }
  cout << ans << endl;
}
Problem p03290 (C++)

Buggy submission (status: Runtime Error):
#include <assert.h>
#include <bits/stdc++.h>

using namespace std;

int main() {
  int d, g;
  cin >> d >> g;
  vector<int> p(d), c(d);
  for (int i = 0; i < d; i++)
    cin >> p[i] >> c[i];
  int ans = 1e9;
  for (int i = 0; i < (1 << d); i++) {
    int sum = 0, cnt = 0, m = -1;
    for (int j = 0; j < d; j++)
      if (i >> j & 1) {
        sum = 100 * (j + 1) * p[j] + c[j];
        cnt += p[j];
      } else
        m = max(m, j);
    if (sum < g)
      if ((g - sum + 100 * (m + 1) - 1) / (100 * (m + 1)) < p[m])
        cnt += (g - sum + 100 * (m + 1) - 1) / (100 * (m + 1));
      else
        continue;
    ans = min(ans, cnt);
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <assert.h>
#include <bits/stdc++.h>

using namespace std;

int main() {
  int d, g;
  cin >> d >> g;
  vector<int> p(d), c(d);
  for (int i = 0; i < d; i++)
    cin >> p[i] >> c[i];
  int ans = 1e9;
  for (int i = 0; i < (1 << d); i++) {
    int sum = 0, cnt = 0, m = -1;
    for (int j = 0; j < d; j++)
      if (i >> j & 1) {
        sum += 100 * (j + 1) * p[j] + c[j];
        cnt += p[j];
      } else
        m = max(m, j);
    if (sum < g)
      if ((g - sum + 100 * (m + 1) - 1) / (100 * (m + 1)) < p[m])
        cnt += (g - sum + 100 * (m + 1) - 1) / (100 * (m + 1));
      else
        continue;
    ans = min(ans, cnt);
  }
  cout << ans << endl;

  return 0;
}
Problem p03291 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <complex>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  const long long int mod = 1000000007, INF = 50000000000000;
  map<long long int, long long int> mp;
  cout << fixed << setprecision(10);
  string str;
  long long int n, m, a, b, now = -1;
  long long int dp[10005][4] = {}, res = 0;
  cin >> str;
  dp[0][3] = 1;
  for (int i = 0; i < str.length(); i++) {
    dp[i + 1][0] = dp[i][0];
    dp[i + 1][1] = dp[i][1];
    dp[i + 1][2] = dp[i][2];
    dp[i + 1][3] = dp[i][3];
    if (str[i] == 'A') {
      dp[i + 1][2] += dp[i][3];
    } else if (str[i] == 'B') {
      dp[i + 1][1] += dp[i][2];
    } else if (str[i] == 'C') {
      dp[i + 1][0] += dp[i][1];
    } else {
      dp[i + 1][0] += 2 * dp[i][0] + dp[i][1];
      dp[i + 1][1] += 2 * dp[i][1] + dp[i][2];
      dp[i + 1][2] += 2 * dp[i][2] + dp[i][3];
      dp[i + 1][3] += 2 * dp[i][3];
    }
    dp[i + 1][0] %= mod;
    dp[i + 1][1] %= mod;
    dp[i + 1][2] %= mod;
    dp[i + 1][3] %= mod;
  }
  cout << dp[str.length()][0];
}


Fixed submission:
#include <algorithm>
#include <complex>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  const long long int mod = 1000000007, INF = 50000000000000;
  map<long long int, long long int> mp;
  cout << fixed << setprecision(10);
  string str;
  long long int n, m, a, b, now = -1;
  long long int dp[100005][4] = {}, res = 0;
  cin >> str;
  dp[0][3] = 1;
  for (int i = 0; i < str.length(); i++) {
    dp[i + 1][0] = dp[i][0];
    dp[i + 1][1] = dp[i][1];
    dp[i + 1][2] = dp[i][2];
    dp[i + 1][3] = dp[i][3];
    if (str[i] == 'A') {
      dp[i + 1][2] += dp[i][3];
    } else if (str[i] == 'B') {
      dp[i + 1][1] += dp[i][2];
    } else if (str[i] == 'C') {
      dp[i + 1][0] += dp[i][1];
    } else {
      dp[i + 1][0] += 2 * dp[i][0] + dp[i][1];
      dp[i + 1][1] += 2 * dp[i][1] + dp[i][2];
      dp[i + 1][2] += 2 * dp[i][2] + dp[i][3];
      dp[i + 1][3] += 2 * dp[i][3];
    }
    dp[i + 1][0] %= mod;
    dp[i + 1][1] %= mod;
    dp[i + 1][2] %= mod;
    dp[i + 1][3] %= mod;
  }
  cout << dp[str.length()][0];
}
Problem p03291 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long dp[10001][4] = {}, MOD = 1000000007;
int main() {
  string s, ABC = "ABC";
  dp[0][0] = 1;
  cin >> s;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '?') {
      dp[i + 1][0] = (dp[i][0] * 3) % MOD;
      for (int j = 0; j < 3; j++) {
        dp[i + 1][j + 1] = (dp[i][j] + dp[i][j + 1] * 3) % MOD;
      }
    } else {
      dp[i + 1][0] = dp[i][0];
      for (int j = 0; j < 3; j++) {
        if (s[i] == ABC[j]) {
          dp[i + 1][j + 1] = (dp[i][j] + dp[i][j + 1]) % MOD;
        } else
          dp[i + 1][j + 1] = dp[i][j + 1] % MOD;
      }
    }
  }
  cout << dp[s.size()][3] << "\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long dp[100010][4] = {}, MOD = 1000000007;
int main() {
  string s, ABC = "ABC";
  dp[0][0] = 1;
  cin >> s;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '?') {
      dp[i + 1][0] = (dp[i][0] * 3) % MOD;
      for (int j = 0; j < 3; j++) {
        dp[i + 1][j + 1] = (dp[i][j] + dp[i][j + 1] * 3) % MOD;
      }
    } else {
      dp[i + 1][0] = dp[i][0];
      for (int j = 0; j < 3; j++) {
        if (s[i] == ABC[j]) {
          dp[i + 1][j + 1] = (dp[i][j] + dp[i][j + 1]) % MOD;
        } else
          dp[i + 1][j + 1] = dp[i][j + 1] % MOD;
      }
    }
  }
  cout << dp[s.size()][3] << "\n";
  return 0;
}
Problem p03291 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
using namespace std;

int main() {
  long long dp[20000][4] = {};
  string s;
  cin >> s;
  int N = s.size();
  long MOD = 1000000007;
  for (int i = N; i >= 0; --i) {
    for (int j = 3; j >= 0; --j) {
      if (i == N) {
        dp[N][3] = 1;
      } else {
        dp[i][j] = dp[i + 1][j] * (s[i] == '?' ? 3 : 1);
        if (j < 3 && (s[i] == "ABC"[j] || s[i] == '?')) {
          dp[i][j] += dp[i + 1][j + 1];
        }
      }
      dp[i][j] %= MOD;
    }
  }
  cout << dp[0][0] << endl;
}

Fixed submission:
#include <iostream>
#include <string>
using namespace std;

int main() {
  long long dp[100010][4] = {};
  string s;
  cin >> s;
  int N = s.size();
  long MOD = 1000000007;
  for (int i = N; i >= 0; --i) {
    for (int j = 3; j >= 0; --j) {
      if (i == N) {
        dp[N][3] = 1;
      } else {
        dp[i][j] = dp[i + 1][j] * (s[i] == '?' ? 3 : 1);
        if (j < 3 && (s[i] == "ABC"[j] || s[i] == '?')) {
          dp[i][j] += dp[i + 1][j + 1];
        }
      }
      dp[i][j] %= MOD;
    }
  }
  cout << dp[0][0] << endl;
}
Problem p03291 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>

int main() {
  std::string S;
  std::cin >> S;

  long long dp[10001][4] = {0};

  for (int i = S.length(); i >= 0; i--) {
    for (int j = 3; j >= 0; j--) {
      if (i >= S.length()) //	全部見終えた
      {
        if (j >= 3) {
          // 最後までチェックが終わったので、正常終了
          dp[i][j] = 1;
        } else {
          // ABCの順になっている部分文字列が見つからず
          dp[i][j] = 0;
        }
      } else {
        // ABCを探している最中
        // ?が見つかれば3通り、そうでなければ1通りあり、
        // ?またはABCのj番目の文字が見つかった場合は1通り増える
        dp[i][j] = dp[i + 1][j] * (S.at(i) == '?' ? 3ll : 1ll);

        if ((j < 3) && ((S.at(i) == '?') || (S.at(i) == "ABC"[j]))) {
          dp[i][j] += dp[i + 1][j + 1];
        }

        dp[i][j] %= 1000000007;
      }
    }
  }

  // 結果出力
  std::cout << dp[0][0] << std::endl;

  return (0);
}


Fixed submission:
#include <iostream>
#include <string>

int main() {
  std::string S;
  std::cin >> S;

  long long dp[100001][4] = {0};

  for (int i = S.length(); i >= 0; i--) {
    for (int j = 3; j >= 0; j--) {
      if (i >= S.length()) //	全部見終えた
      {
        if (j >= 3) {
          // 最後までチェックが終わったので、正常終了
          dp[i][j] = 1;
        } else {
          // ABCの順になっている部分文字列が見つからず
          dp[i][j] = 0;
        }
      } else {
        // ABCを探している最中
        // ?が見つかれば3通り、そうでなければ1通りあり、
        // ?またはABCのj番目の文字が見つかった場合は1通り増える
        dp[i][j] = dp[i + 1][j] * (S.at(i) == '?' ? 3ll : 1ll);

        if ((j < 3) && ((S.at(i) == '?') || (S.at(i) == "ABC"[j]))) {
          dp[i][j] += dp[i + 1][j + 1];
        }

        dp[i][j] %= 1000000007;
      }
    }
  }

  // 結果出力
  std::cout << dp[0][0] << std::endl;

  return (0);
}
Problem p03291 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <math.h>
#include <stack>
#include <string.h>

using namespace std;

char S[10009];
long long dp[10009][4];
int N = 1e9 + 7;

int main(int argc, char **argv) {
  cin >> S;
  int T = strlen(S);

  dp[0][0] = 1;
  dp[0][1] = 0;
  dp[0][2] = 0;
  dp[0][3] = 0;
  for (int i = 1; i <= T; i++) {
    if (S[i - 1] != '?')
      dp[i][0] = dp[i - 1][0];
    else
      dp[i][0] = 3 * dp[i - 1][0];
    dp[i][0] = dp[i][0] % N;
  }
  for (int i = 1; i <= T; i++) {
    for (int j = 1; j <= 3; j++) {
      if (S[i - 1] == 'A' + j - 1) {
        dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j];
      } else if (S[i - 1] == '?') {
        dp[i][j] = dp[i - 1][j - 1] + 3 * dp[i - 1][j];
      } else {
        dp[i][j] = 1 * dp[i - 1][j];
      }
      dp[i][j] = dp[i][j] % N;
    }
  }
  /*
  for (int i = 0; i <= T; i++) {
      for (int j = 0; j <= 3; j++) {
          cout << dp[i][j] << ",";
      }
      cout << endl;
  }
  */
  cout << dp[T][3] << endl;
}


Fixed submission:
#include <iostream>
#include <math.h>
#include <stack>
#include <string.h>

using namespace std;

char S[100009];
long dp[100009][4];
long N = 1e9 + 7;

int main(int argc, char **argv) {
  cin >> S;
  int T = strlen(S);

  dp[0][0] = 1;
  dp[0][1] = 0;
  dp[0][2] = 0;
  dp[0][3] = 0;
  for (int i = 1; i <= T; i++) {
    if (S[i - 1] != '?')
      dp[i][0] = dp[i - 1][0];
    else
      dp[i][0] = 3 * dp[i - 1][0];
    dp[i][0] = dp[i][0] % N;
  }
  for (int i = 1; i <= T; i++) {
    for (int j = 1; j <= 3; j++) {
      if (S[i - 1] == 'A' + j - 1) {
        dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j];
      } else if (S[i - 1] == '?') {
        dp[i][j] = dp[i - 1][j - 1] + 3 * dp[i - 1][j];
      } else {
        dp[i][j] = 1 * dp[i - 1][j];
      }
      dp[i][j] = dp[i][j] % N;
    }
  }
  /*
  for (int i = 0; i <= T; i++) {
      for (int j = 0; j <= 3; j++) {
          cout << dp[i][j] << ",";
      }
      cout << endl;
  }
  */
  cout << dp[T][3] << endl;
}
Problem p03291 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
typedef long long ll;
ll dp[10010][4];
const ll mod = 1000000007;
string s;
int main(void) {
  // Your code here!
  cin >> s;
  ll len = s.length();
  dp[0][0] = 1;
  for (ll i = 0; i < len; i++) {
    if (s[i] == 'A' || s[i] == '?')
      (dp[i + 1][1] += dp[i][0]) %= mod;
    if (s[i] == 'B' || s[i] == '?')
      (dp[i + 1][2] += dp[i][1]) %= mod;
    if (s[i] == 'C' || s[i] == '?')
      (dp[i + 1][3] += dp[i][2]) %= mod;
    for (ll j = 0; j < 4; j++) {
      (dp[i + 1][j] += dp[i][j] * (s[i] == '?' ? 3 : 1)) %= mod;
    }
  }
  cout << dp[len][3] << endl;
}


Fixed submission:
#include <iostream>
using namespace std;
typedef long long ll;
ll dp[100100][4];
const ll mod = 1000000007;
string s;
int main(void) {
  // Your code here!
  cin >> s;
  ll len = s.length();
  dp[0][0] = 1;
  for (ll i = 0; i < len; i++) {
    if (s[i] == 'A' || s[i] == '?')
      (dp[i + 1][1] += dp[i][0]) %= mod;
    if (s[i] == 'B' || s[i] == '?')
      (dp[i + 1][2] += dp[i][1]) %= mod;
    if (s[i] == 'C' || s[i] == '?')
      (dp[i + 1][3] += dp[i][2]) %= mod;
    for (ll j = 0; j < 4; j++) {
      (dp[i + 1][j] += dp[i][j] * (s[i] == '?' ? 3 : 1)) %= mod;
    }
  }
  cout << dp[len][3] << endl;
}
Problem p03291 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define dump(x) cout << (x) << endl
typedef long long ll;
typedef pair<ll, ll> pl;
typedef pair<int, int> pi;
typedef vector<int> V;

int main() {
  string s;
  cin >> s;
  ll dp[10005][4] = {};
  int n = s.size(), m = 1000000007;
  for (int i = n; i >= 0; i--) {
    for (int j = 3; j >= 0; j--) {
      if (i == n)
        dp[i][j] = (j == 3 ? 1 : 0);
      else {
        dp[i][j] = dp[i + 1][j] * (s[i] == '?' ? 3LL : 1LL);
        if (j < 3 && (s[i] == '?' || s[i] == "ABC"[j])) {
          dp[i][j] += dp[i + 1][j + 1];
        }
        dp[i][j] %= m;
      }
    }
  }
  dump(dp[0][0]);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define dump(x) cout << (x) << endl
typedef long long ll;
typedef pair<ll, ll> pl;
typedef pair<int, int> pi;
typedef vector<int> V;

int main() {
  string s;
  cin >> s;
  ll dp[100005][4] = {};
  int n = s.size(), m = 1000000007;
  for (int i = n; i >= 0; i--) {
    for (int j = 3; j >= 0; j--) {
      if (i == n)
        dp[i][j] = (j == 3 ? 1 : 0);
      else {
        dp[i][j] = dp[i + 1][j] * (s[i] == '?' ? 3LL : 1LL);
        if (j < 3 && (s[i] == '?' || s[i] == "ABC"[j])) {
          dp[i][j] += dp[i + 1][j + 1];
        }
        dp[i][j] %= m;
      }
    }
  }
  dump(dp[0][0]);
  return 0;
}
Problem p03291 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <math.h>
#include <string>
#include <vector>

using namespace std;

int mod_pow(int x, int y, int MOD, vector<int> &vc) {
  long long ans = 1;
  for (int i = 0; i < y; i++) {
    ans *= x;
    ans %= MOD;
    vc[i + 1] = ans;
  }
  return ans;
}

int main() {
  string s;
  int const MOD = pow(10, 9) + 7;
  long long A = 0;
  long long AB = 0;
  long long ABC = 0;
  int cnt = 0;
  int num = 0;
  cin >> s;

  for (int i = 0; i < s.size(); i++) {
    char x = s[i];
    if (x == '?') {
      num++;
    }
  }

  vector<int> mPow(num, 1);
  mod_pow(3, num, MOD, mPow);

  for (int i = 0; i < s.size(); i++) {
    char x = s[i];
    if (x == 'A') {
      A += 1 * mPow[cnt];
    } else if (x == 'B') {
      AB += A;
    } else if (x == 'C') {
      ABC += AB;
    } else {
      ABC *= 3;
      ABC += AB;
      AB *= 3;
      AB += A;
      A *= 3;
      A += mPow[cnt];
      cnt++;
    }
    A %= MOD;
    AB %= MOD;
    ABC %= MOD;
  }
  cout << ABC << '\n';
}

Fixed submission:
#include <iostream>
#include <math.h>
#include <string>
#include <vector>

using namespace std;

int mod_pow(int x, int y, int MOD, vector<int> &vc) {
  long long ans = 1;
  for (int i = 0; i < y; i++) {
    ans *= x;
    ans %= MOD;
    vc[i + 1] = ans;
  }
  return ans;
}

int main() {
  string s;
  int const MOD = pow(10, 9) + 7;
  long long A = 0;
  long long AB = 0;
  long long ABC = 0;
  int cnt = 0;
  int num = 0;
  cin >> s;

  for (int i = 0; i < s.size(); i++) {
    char x = s[i];
    if (x == '?') {
      num++;
    }
  }

  vector<int> mPow(num + 1, 1);
  mod_pow(3, num, MOD, mPow);

  for (int i = 0; i < s.size(); i++) {
    char x = s[i];
    if (x == 'A') {
      A += 1 * mPow[cnt];
    } else if (x == 'B') {
      AB += A;
    } else if (x == 'C') {
      ABC += AB;
    } else {
      ABC *= 3;
      ABC += AB;
      AB *= 3;
      AB += A;
      A *= 3;
      A += mPow[cnt];
      cnt++;
    }
    A %= MOD;
    AB %= MOD;
    ABC %= MOD;
  }
  cout << ABC << '\n';
}
Problem p03291 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <numeric>
#include <string>
#include <utility>
#include <vector>

using namespace std;

long MOD = 1e9 + 7;
string s;

long dp(int pos, char next) {
  if (pos == s.size())
    return next == 'D' ? 1 : 0;
  if (s[pos] == '?') {
    long consume = next != 'D' ? dp(pos + 1, next + 1) : 0;
    return (3 * dp(pos + 1, next) + consume) % MOD;
  } else {
    long consume = s[pos] == next ? dp(pos + 1, next + 1) : 0;
    return (dp(pos + 1, next) + consume) % MOD;
  }
}

int main() {
  cin >> s;
  cout << dp(0, 'A') << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <numeric>
#include <string>
#include <utility>
#include <vector>

using namespace std;

long MOD = 1e9 + 7;
string s;

long dp(int pos, char next) {
  if (pos == s.size())
    return next == 'D' ? 1 : 0;
  if (s[pos] == '?') {
    long consume = next != 'D' ? dp(pos + 1, next + 1) : 0;
    return (3 * dp(pos + 1, next) + consume) % MOD;
  } else {
    long consume = s[pos] == next ? dp(pos + 1, next + 1) : 0;
    return (dp(pos + 1, next) + consume) % MOD;
  }
}

int main() {
  cin >> s;

  vector<vector<long>> dp(s.size(), vector<long>(4));
  dp.push_back({0, 0, 0, 1});
  for (int i = s.size() - 1; i >= 0; i--) {
    if (s[i] == '?') {
      for (char c = 'A'; c < 'D'; c++) {
        dp[i][c - 'A'] = (3 * dp[i + 1][c - 'A'] + dp[i + 1][c - 'A' + 1]);
      }
      dp[i]['D' - 'A'] = 3 * dp[i + 1]['D' - 'A'];
    } else {
      for (char c = 'A'; c < 'D'; c++) {
        if (s[i] == c) {
          dp[i][c - 'A'] = dp[i + 1][c - 'A'] + dp[i + 1][c - 'A' + 1];
        } else {
          dp[i][c - 'A'] = dp[i + 1][c - 'A'];
        }
      }
      dp[i]['D' - 'A'] = dp[i + 1]['D' - 'A'];
    }
    for (char c = 'A'; c <= 'D'; c++) {
      dp[i][c - 'A'] %= MOD;
    }
  }
  cout << dp[0][0] << endl;
}
Problem p03291 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll, ll> pll;
const ll mod = 1e9 + 7;
// const ll mod=998244353;
const ll inf = 5e18;

int main() {
  string s;
  cin >> s;
  ll n = s.size();
  mat dp(n + 1, vec(3));
  dp[n][3] = 1;
  for (ll i = n - 1; i > -1; i--) {
    for (ll j = 3; j > -1; j--) {
      ll m = 1;
      if (s[i] == '?')
        m = 3;
      (dp[i][j] = m * dp[i + 1][j]) %= mod;
      if (s[i] == '?' || (j == 0 && s[i] == 'A') || (j == 1 && s[i] == 'B') ||
          (j == 2 && s[i] == 'C'))
        m = 1;
      else
        m = 0;
      if (j != 3) {
        (dp[i][j] += m * dp[i + 1][j + 1]) %= mod;
      }
    }
  }
  cout << dp[0][0] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll, ll> pll;
const ll mod = 1e9 + 7;
// const ll mod=998244353;
const ll inf = 5e18;

int main() {
  string s;
  cin >> s;
  ll n = s.size();
  mat dp(n + 1, vec(4));
  dp[n][3] = 1;
  for (ll i = n - 1; i > -1; i--) {
    for (ll j = 3; j > -1; j--) {
      ll m = 1;
      if (s[i] == '?')
        m = 3;
      (dp[i][j] = m * dp[i + 1][j]) %= mod;
      if (s[i] == '?' || (j == 0 && s[i] == 'A') || (j == 1 && s[i] == 'B') ||
          (j == 2 && s[i] == 'C'))
        m = 1;
      else
        m = 0;
      if (j != 3) {
        (dp[i][j] += m * dp[i + 1][j + 1]) %= mod;
      }
    }
  }
  cout << dp[0][0] << endl;
}
Problem p03291 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define pb push_back
#define mod (int)(1e9 + 7)
using namespace std;
typedef pair<int, int> pii;

int dp[11111][11];
int main() {

  string s;
  cin >> s;

  dp[0][0] = 1;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'A' || s[i] == '?') {
      for (int j = 0; j < 4; j++) {
        dp[i + 1][j] += dp[i][j];
        dp[i + 1][j] %= mod;
      }
      dp[i + 1][1] += dp[i][0];
      dp[i + 1][1] %= mod;
    }

    if (s[i] == 'B' || s[i] == '?') {
      for (int j = 0; j < 4; j++) {
        dp[i + 1][j] += dp[i][j];
        dp[i + 1][j] %= mod;
      }
      dp[i + 1][2] += dp[i][1];
      dp[i + 1][2] %= mod;
    }

    if (s[i] == 'C' || s[i] == '?') {
      for (int j = 0; j < 4; j++) {
        dp[i + 1][j] += dp[i][j];
        dp[i + 1][j] %= mod;
      }
      dp[i + 1][3] += dp[i][2];
      dp[i + 1][3] %= mod;
    }
  }
  cout << dp[s.size()][3] << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define pb push_back
#define mod (int)(1e9 + 7)
using namespace std;
typedef pair<int, int> pii;

int dp[111111][11];
int main() {

  string s;
  cin >> s;

  dp[0][0] = 1;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'A' || s[i] == '?') {
      for (int j = 0; j < 4; j++) {
        dp[i + 1][j] += dp[i][j];
        dp[i + 1][j] %= mod;
      }
      dp[i + 1][1] += dp[i][0];
      dp[i + 1][1] %= mod;
    }

    if (s[i] == 'B' || s[i] == '?') {
      for (int j = 0; j < 4; j++) {
        dp[i + 1][j] += dp[i][j];
        dp[i + 1][j] %= mod;
      }
      dp[i + 1][2] += dp[i][1];
      dp[i + 1][2] %= mod;
    }

    if (s[i] == 'C' || s[i] == '?') {
      for (int j = 0; j < 4; j++) {
        dp[i + 1][j] += dp[i][j];
        dp[i + 1][j] %= mod;
      }
      dp[i + 1][3] += dp[i][2];
      dp[i + 1][3] %= mod;
    }
  }
  cout << dp[s.size()][3] << endl;
  return 0;
}
Problem p03291 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;

const int maxN = 10010;
const int INF = 1000000000;
const int MOD = 1000000007;
char s[maxN];
ll f[maxN][4];
char t[4];
int main() {
  scanf("%s", s + 1);
  int len = strlen(s + 1);
  ll ans = 0;
  t[1] = 'A';
  t[2] = 'B';
  t[3] = 'C';
  for (int i = 0; i <= len; i++) {
    for (int j = 0; j <= 3; j++) {
      if (i == 0) {
        f[i][j] = (j == 0) ? 1 : 0;
      } else {
        if (s[i] == '?') {
          f[i][j] = 3ll * f[i - 1][j];
        } else
          f[i][j] = f[i - 1][j];
        if (j > 0 && (s[i] == '?' || s[i] == t[j])) {
          f[i][j] += f[i - 1][j - 1];
        }
      }
      f[i][j] %= MOD;
    }
  }
  printf("%lld\n", f[len][3]);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;

const int maxN = 100010;
const int INF = 1000000000;
const int MOD = 1000000007;
char s[maxN];
ll f[maxN][4];
char t[4];
int main() {
  scanf("%s", s + 1);
  int len = strlen(s + 1);
  ll ans = 0;
  t[1] = 'A';
  t[2] = 'B';
  t[3] = 'C';
  for (int i = 0; i <= len; i++) {
    for (int j = 0; j <= 3; j++) {
      if (i == 0) {
        f[i][j] = (j == 0) ? 1 : 0;
      } else {
        if (s[i] == '?') {
          f[i][j] = 3ll * f[i - 1][j];
        } else
          f[i][j] = f[i - 1][j];
        if (j > 0 && (s[i] == '?' || s[i] == t[j])) {
          f[i][j] += f[i - 1][j - 1];
        }
      }
      f[i][j] %= MOD;
    }
  }
  printf("%lld\n", f[len][3]);
  return 0;
}
Problem p03291 (C++)

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

const int MOD = 1000000007;

// begin() end()
#define all(x) (x).begin(), (x).end()

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)

// 最大公約数
unsigned gcd(unsigned a, unsigned b) {
  if (a < b)
    return gcd(b, a);
  unsigned r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

// 最小公倍数
int lcm(int number1, int number2) {
  return number1 * number2 / gcd(number1, number2);
}

void add(long long &a, long long b) {
  a += b;
  if (a >= MOD)
    a -= MOD;
}

int main() {
  string S;
  cin >> S;

  long long dp[20000][5];
  dp[0][0] = 1;
  REP(i, S.size()) {
    REP(j, 5) {
      if (S[i] != '?')
        add(dp[i + 1][j], dp[i][j]);
      else
        add(dp[i + 1][j], dp[i][j] * 3 % MOD);
    }
    if (S[i] == 'A' || S[i] == '?')
      add(dp[i + 1][1], dp[i][0]);
    if (S[i] == 'B' || S[i] == '?')
      add(dp[i + 1][2], dp[i][1]);
    if (S[i] == 'C' || S[i] == '?')
      add(dp[i + 1][3], dp[i][2]);
  }

  cout << dp[S.size()][3] << endl;
}

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

const int MOD = 1000000007;

// begin() end()
#define all(x) (x).begin(), (x).end()

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)

// 最大公約数
unsigned gcd(unsigned a, unsigned b) {
  if (a < b)
    return gcd(b, a);
  unsigned r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

// 最小公倍数
int lcm(int number1, int number2) {
  return number1 * number2 / gcd(number1, number2);
}

void add(long long &a, long long b) {
  a += b;
  if (a >= MOD)
    a -= MOD;
}

int main() {
  string S;
  cin >> S;

  long long dp[210000][5];
  dp[0][0] = 1;
  REP(i, S.size()) {
    REP(j, 5) {
      if (S[i] != '?')
        add(dp[i + 1][j], dp[i][j]);
      else
        add(dp[i + 1][j], dp[i][j] * 3 % MOD);
    }
    if (S[i] == 'A' || S[i] == '?')
      add(dp[i + 1][1], dp[i][0]);
    if (S[i] == 'B' || S[i] == '?')
      add(dp[i + 1][2], dp[i][1]);
    if (S[i] == 'C' || S[i] == '?')
      add(dp[i + 1][3], dp[i][2]);
  }

  cout << dp[S.size()][3] << endl;
}
Problem p03291 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>

using namespace std;

string S;

long long int M = 1000000007;

long long int dp[10005][5];

int main(void) {

  cin >> S;

  int N = S.size();

  S = "#" + S;

  dp[0][0] = 1;

  for (int i = 1; i <= N; i++) {
    if (S[i] == 'A') {
      dp[i][0] = dp[i - 1][0];
      dp[i][1] = (dp[i - 1][0] + dp[i - 1][1]) % M;
      dp[i][2] = dp[i - 1][2];
      dp[i][3] = dp[i - 1][3];
    } else if (S[i] == 'B') {
      dp[i][0] = dp[i - 1][0];
      dp[i][1] = dp[i - 1][1];
      dp[i][2] = (dp[i - 1][1] + dp[i - 1][2]) % M;
      dp[i][3] = dp[i - 1][3];
    } else if (S[i] == 'C') {
      dp[i][0] = dp[i - 1][0];
      dp[i][1] = dp[i - 1][1];
      dp[i][2] = dp[i - 1][2];
      dp[i][3] = (dp[i - 1][2] + dp[i - 1][3]) % M;
    } else {
      dp[i][0] = (3 * dp[i - 1][0]) % M;
      dp[i][1] = (dp[i - 1][1] * 3 + dp[i - 1][0]) % M;
      dp[i][2] = (dp[i - 1][2] * 3 + dp[i - 1][1]) % M;
      dp[i][3] = (dp[i - 1][3] * 3 + dp[i - 1][2]) % M;
    }
  }

  /*int i = 2;
  for (int j = 0; j <= 3; j++) {
          cout << "dp[" << i<<"][" << j<<"]= " <<dp[i][j] << endl;
  }*/

  cout << dp[N][3] << endl;

  // system("pause");
  return 0;
}

Fixed submission:
#include <iostream>
#include <string>

using namespace std;

string S;

long long int M = 1000000007;

long long int dp[100005][5];

int main(void) {

  cin >> S;

  int N = S.size();

  S = "#" + S;

  dp[0][0] = 1;

  for (int i = 1; i <= N; i++) {
    if (S[i] == 'A') {
      dp[i][0] = dp[i - 1][0];
      dp[i][1] = (dp[i - 1][0] + dp[i - 1][1]) % M;
      dp[i][2] = dp[i - 1][2];
      dp[i][3] = dp[i - 1][3];
    } else if (S[i] == 'B') {
      dp[i][0] = dp[i - 1][0];
      dp[i][1] = dp[i - 1][1];
      dp[i][2] = (dp[i - 1][1] + dp[i - 1][2]) % M;
      dp[i][3] = dp[i - 1][3];
    } else if (S[i] == 'C') {
      dp[i][0] = dp[i - 1][0];
      dp[i][1] = dp[i - 1][1];
      dp[i][2] = dp[i - 1][2];
      dp[i][3] = (dp[i - 1][2] + dp[i - 1][3]) % M;
    } else {
      dp[i][0] = (3 * dp[i - 1][0]) % M;
      dp[i][1] = (dp[i - 1][1] * 3 + dp[i - 1][0]) % M;
      dp[i][2] = (dp[i - 1][2] * 3 + dp[i - 1][1]) % M;
      dp[i][3] = (dp[i - 1][3] * 3 + dp[i - 1][2]) % M;
    }
  }

  /*int i = 2;
  for (int j = 0; j <= 3; j++) {
          cout << "dp[" << i<<"][" << j<<"]= " <<dp[i][j] << endl;
  }*/

  cout << dp[N][3] << endl;

  // system("pause");
  return 0;
}
Problem p03291 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;

ll X[100100];
ll Y[100100];
ll Z[100100];

ll p(ll b) { // 3のb乗
  if (b == 0) {
    return 1;
  } else if (b == 1) {
    return 3;
  } else {
    if (b % 2 == 0) {
      return ((p(b / 2) % MOD) * (p(b / 2) % MOD)) % MOD;
    } else {
      return (3 * (p((b - 1) / 2) % MOD) * (p((b - 1) / 2) % MOD)) % MOD;
    }
  }
}

int main() {
  string T;
  cin >> T;
  ll cnt = 0;
  for (int i = 0; i < 100100; i++) {
    X[i] = 0LL;
    Y[i] = 0LL;
    Z[i] = 0LL;
  }
  if (T[0] == 'A' || T[0] == '?') {
    X[0] = 1LL;
  }
  if (T[0] == '?') {
    cnt++;
  }

  for (int i = 0; i < (int)T.size() - 1; i++) {
    int flag;
    if (T[i + 1] == 'A') {
      flag = 1;
    }
    if (T[i + 1] == 'B') {
      flag = 2;
    }
    if (T[i + 1] == 'C') {
      flag = 3;
    }
    if (T[i + 1] == '?') {
      flag = 4;
    }

    if (flag == 1 || flag == 4) {
      X[i + 1] += X[i] + p(cnt - 1);
      Y[i + 1] += Y[i];
      Z[i + 1] += Z[i];
    }
    if (flag == 2 || flag == 4) {
      X[i + 1] += X[i];
      Y[i + 1] += Y[i] + X[i];
      Z[i + 1] += Z[i];
    }
    if (flag == 3 || flag == 4) {
      X[i + 1] += X[i];
      Y[i + 1] += Y[i];
      Z[i + 1] += Z[i] + Y[i];
    }
    X[i + 1] %= MOD;
    Y[i + 1] %= MOD;
    Z[i + 1] %= MOD;

    if (flag == 4) {
      cnt++;
    }
  }

  cout << Z[(int)T.size() - 1] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;

ll X[100100];
ll Y[100100];
ll Z[100100];

ll p(ll b) { // 3のb乗
  if (b == 0) {
    return 1;
  } else if (b == 1) {
    return 3;
  } else {
    if (b % 2 == 0) {
      return ((p(b / 2) % MOD) * (p(b / 2) % MOD)) % MOD;
    } else {
      return (3 * (p((b - 1) / 2) % MOD) * (p((b - 1) / 2) % MOD)) % MOD;
    }
  }
}

int main() {
  string T;
  cin >> T;
  ll cnt = 0;
  for (int i = 0; i < 100100; i++) {
    X[i] = 0LL;
    Y[i] = 0LL;
    Z[i] = 0LL;
  }
  if (T[0] == 'A' || T[0] == '?') {
    X[0] = 1LL;
  }
  if (T[0] == '?') {
    cnt++;
  }

  for (int i = 0; i < (int)T.size() - 1; i++) {
    int flag;
    if (T[i + 1] == 'A') {
      flag = 1;
    }
    if (T[i + 1] == 'B') {
      flag = 2;
    }
    if (T[i + 1] == 'C') {
      flag = 3;
    }
    if (T[i + 1] == '?') {
      flag = 4;
    }

    if (flag == 1 || flag == 4) {
      X[i + 1] += X[i] + p(cnt);
      Y[i + 1] += Y[i];
      Z[i + 1] += Z[i];
    }
    if (flag == 2 || flag == 4) {
      X[i + 1] += X[i];
      Y[i + 1] += Y[i] + X[i];
      Z[i + 1] += Z[i];
    }
    if (flag == 3 || flag == 4) {
      X[i + 1] += X[i];
      Y[i + 1] += Y[i];
      Z[i + 1] += Z[i] + Y[i];
    }
    X[i + 1] %= MOD;
    Y[i + 1] %= MOD;
    Z[i + 1] %= MOD;

    if (flag == 4) {
      cnt++;
    }
  }

  cout << Z[(int)T.size() - 1] << endl;
  return 0;
}
Problem p03291 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<ll, ll> P;
typedef vector<int> vi;
typedef vector<vi> vvi;

#define REP(i, n) for (ll i = 0; i < (n); ++i)
#define REPR(i, n) for (ll i = (n)-1; i >= 0; --i)
#define FOR(i, n, m) for (ll i = (n); i < (m); ++i)
#define FORR(i, n, m) for (ll i = (m)-1; i >= (n); --i)
#define FORE(x, xs) for (auto &x : (xs))
#define ALL(v) v.begin(), v.end()
#define ZERO(a) memset(a, 0, sizeof(a))

const int INF = 1000000009;
const int MOD = 1000000007;
const double EPS = 1e-14;
const double PI = acos(-1);

const int MAX = 1e5;

int N;
ll dp[MAX][4];
/// dp[i][0]は?が出て来るたびに3倍する。文字列の種類の数
/// dp[i][1]はi文字目までで取れるAの種類の数
/// dp[i][2]、dp[i][3]はそれぞれiまでのB、Cの組み合わせの数

int main() {

  string s;
  cin >> s;
  N = s.size();

  dp[0][1] = dp[0][2] = dp[0][3] = 0;
  dp[0][0] = 1;

  FOR(i, 1, N + 1) {
    char x = s[i - 1];
    dp[i][0] = dp[i - 1][0];
    if (x == '?')
      dp[i][0] = dp[i][0] * 3 % MOD;

    FOR(j, 1, 4) {
      dp[i][j] = dp[i - 1][j];
      if (x == '?') {
        dp[i][j] = (dp[i - 1][j] * 3 + dp[i - 1][j - 1]) % MOD;
      } else if (x == "ABC"[j - 1]) {
        dp[i][j] = (dp[i - 1][j] + dp[i - 1][j - 1]) % MOD;
      }
    }
  }

  /*
  FORR (j, 0, 4) {
    REP (i, N+1) cout << dp[i][j] << " ";
    cout << endl;
  }
  */
  cout << dp[N][3] << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<ll, ll> P;
typedef vector<int> vi;
typedef vector<vi> vvi;

#define REP(i, n) for (ll i = 0; i < (n); ++i)
#define REPR(i, n) for (ll i = (n)-1; i >= 0; --i)
#define FOR(i, n, m) for (ll i = (n); i < (m); ++i)
#define FORR(i, n, m) for (ll i = (m)-1; i >= (n); --i)
#define FORE(x, xs) for (auto &x : (xs))
#define ALL(v) v.begin(), v.end()
#define ZERO(a) memset(a, 0, sizeof(a))

const int INF = 1000000009;
const int MOD = 1000000007;
const double EPS = 1e-14;
const double PI = acos(-1);

const int MAX = 1e5;

int N;
ll dp[MAX + 1][4];
/// dp[i][0]は?が出て来るたびに3倍する。文字列の種類の数
/// dp[i][1]はi文字目までで取れるAの種類の数
/// dp[i][2]、dp[i][3]はそれぞれiまでのB、Cの組み合わせの数

int main() {

  string s;
  cin >> s;
  N = s.size();

  dp[0][1] = dp[0][2] = dp[0][3] = 0;
  dp[0][0] = 1;

  FOR(i, 1, N + 1) {
    char x = s[i - 1];
    dp[i][0] = dp[i - 1][0];
    if (x == '?')
      dp[i][0] = dp[i][0] * 3 % MOD;

    FOR(j, 1, 4) {
      dp[i][j] = dp[i - 1][j];
      if (x == '?') {
        dp[i][j] = (dp[i - 1][j] * 3 + dp[i - 1][j - 1]) % MOD;
      } else if (x == "ABC"[j - 1]) {
        dp[i][j] = (dp[i - 1][j] + dp[i - 1][j - 1]) % MOD;
      }
    }
  }

  /*
  FORR (j, 0, 4) {
    REP (i, N+1) cout << dp[i][j] << " ";
    cout << endl;
  }
  */
  cout << dp[N][3] << endl;
  return 0;
}
Problem p03291 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
string st;
const int mo = 1e9 + 7;
long long l, f[10007][4];
inline long long qp(int x, int y) {
  long long t = 1, tt = x;
  while (y) {
    if (y & 1)
      t = (t * tt) % mo;
    tt = (tt * tt) % mo;
    y >>= 1;
  }
  return t;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> st;
  l = st.length();
  f[0][0] = 1;
  for (int i = 0; i < l; i++) {
    if (st[i] == 'C' || st[i] == '?') {
      f[i + 1][0] = (f[i + 1][0] + f[i][0]) % mo;
      f[i + 1][1] = (f[i + 1][1] + f[i][1]) % mo;
      f[i + 1][2] = (f[i + 1][2] + f[i][2]) % mo;
      f[i + 1][3] = (f[i + 1][3] + f[i][2] + f[i][3]) % mo;
    }
    if (st[i] == 'B' || st[i] == '?') {
      f[i + 1][0] = (f[i + 1][0] + f[i][0]) % mo;
      f[i + 1][1] = (f[i + 1][1] + f[i][1]) % mo;
      f[i + 1][2] = (f[i + 1][2] + f[i][1] + f[i][2]) % mo;
      f[i + 1][3] = (f[i + 1][3] + f[i][3]) % mo;
    }
    if (st[i] == 'A' || st[i] == '?') {
      f[i + 1][0] = (f[i + 1][0] + f[i][0]) % mo;
      f[i + 1][1] = (f[i + 1][1] + f[i][0] + f[i][1]) % mo;
      f[i + 1][2] = (f[i + 1][2] + f[i][2]) % mo;
      f[i + 1][3] = (f[i + 1][3] + f[i][3]) % mo;
    }
  }
  cout << f[l][3] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
string st;
const int mo = 1e9 + 7;
long long l, f[100007][4];
inline long long qp(int x, int y) {
  long long t = 1, tt = x;
  while (y) {
    if (y & 1)
      t = (t * tt) % mo;
    tt = (tt * tt) % mo;
    y >>= 1;
  }
  return t;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> st;
  l = st.length();
  f[0][0] = 1;
  for (int i = 0; i < l; i++) {
    if (st[i] == 'C' || st[i] == '?') {
      f[i + 1][0] = (f[i + 1][0] + f[i][0]) % mo;
      f[i + 1][1] = (f[i + 1][1] + f[i][1]) % mo;
      f[i + 1][2] = (f[i + 1][2] + f[i][2]) % mo;
      f[i + 1][3] = (f[i + 1][3] + f[i][2] + f[i][3]) % mo;
    }
    if (st[i] == 'B' || st[i] == '?') {
      f[i + 1][0] = (f[i + 1][0] + f[i][0]) % mo;
      f[i + 1][1] = (f[i + 1][1] + f[i][1]) % mo;
      f[i + 1][2] = (f[i + 1][2] + f[i][1] + f[i][2]) % mo;
      f[i + 1][3] = (f[i + 1][3] + f[i][3]) % mo;
    }
    if (st[i] == 'A' || st[i] == '?') {
      f[i + 1][0] = (f[i + 1][0] + f[i][0]) % mo;
      f[i + 1][1] = (f[i + 1][1] + f[i][0] + f[i][1]) % mo;
      f[i + 1][2] = (f[i + 1][2] + f[i][2]) % mo;
      f[i + 1][3] = (f[i + 1][3] + f[i][3]) % mo;
    }
  }
  cout << f[l][3] << endl;
  return 0;
}
Problem p03291 (Python)

Buggy submission (status: Time Limit Exceeded):
S = input()
N = len(S)

mod = 10**9 + 7
A, AB, ABC, ALL = 0, 0, 0, 1
for s in S:
    if s == "A":
        A += ALL

    if s == "B":
        AB += A

    if s == "C":
        ABC += AB

    if s == "?":
        ABC = ABC * 3 + AB
        AB = AB * 3 + A
        A = A * 3 + ALL
        ALL *= 3

print(ABC % mod)


Fixed submission:
S = input()
N = len(S)

mod = 10**9 + 7
A, AB, ABC, ALL = 0, 0, 0, 1
for s in S:
    if s == "A":
        A += ALL

    if s == "B":
        AB += A

    if s == "C":
        ABC += AB

    if s == "?":
        ABC = (ABC * 3 + AB) % mod
        AB = (AB * 3 + A) % mod
        A = (A * 3 + ALL) % mod
        ALL = (ALL * 3) % mod

print(ABC % mod)
Problem p03291 (Python)

Buggy submission (status: Time Limit Exceeded):
s = input()
n = len(s)
mod = 10**9 + 7
dp = [[0] * 4 for i in range(n + 1)]
dp[0][0] = 1

for i in range(n):
    if s[i] == "A":
        dp[i + 1][1] = dp[i + 1][1] + dp[i][0]
        dp[i + 1][0] += dp[i][0]
        dp[i + 1][1] += dp[i][1]
        dp[i + 1][2] += dp[i][2]
        dp[i + 1][3] += dp[i][3]
    elif s[i] == "B":
        dp[i + 1][2] = dp[i + 1][2] + dp[i][1]
        dp[i + 1][0] += dp[i][0]
        dp[i + 1][1] += dp[i][1]
        dp[i + 1][2] += dp[i][2]
        dp[i + 1][3] += dp[i][3]
    elif s[i] == "C":
        dp[i + 1][3] = dp[i + 1][3] + dp[i][2]
        dp[i + 1][0] += dp[i][0]
        dp[i + 1][1] += dp[i][1]
        dp[i + 1][2] += dp[i][2]
        dp[i + 1][3] += dp[i][3]
    else:
        for j in range(4):
            dp[i + 1][j] += 3 * dp[i][j]
        dp[i + 1][1] += dp[i][0]
        dp[i + 1][2] += dp[i][1]
        dp[i + 1][3] += dp[i][2]

print(dp[n][3] % mod)


Fixed submission:
s = input()
n = len(s)
mod = 10**9 + 7
dp = [[0] * 4 for i in range(n + 1)]
dp[0][0] = 1

for i in range(n):
    if s[i] == "A":
        dp[i + 1][1] = dp[i + 1][1] + dp[i][0]
        dp[i + 1][0] += dp[i][0]
        dp[i + 1][1] += dp[i][1]
        dp[i + 1][2] += dp[i][2]
        dp[i + 1][3] += dp[i][3]
    elif s[i] == "B":
        dp[i + 1][2] = dp[i + 1][2] + dp[i][1]
        dp[i + 1][0] += dp[i][0]
        dp[i + 1][1] += dp[i][1]
        dp[i + 1][2] += dp[i][2]
        dp[i + 1][3] += dp[i][3]
    elif s[i] == "C":
        dp[i + 1][3] = dp[i + 1][3] + dp[i][2]
        dp[i + 1][0] += dp[i][0]
        dp[i + 1][1] += dp[i][1]
        dp[i + 1][2] += dp[i][2]
        dp[i + 1][3] += dp[i][3]
    else:
        for j in range(4):
            dp[i + 1][j] += 3 * dp[i][j]
        dp[i + 1][1] += dp[i][0]
        dp[i + 1][2] += dp[i][1]
        dp[i + 1][3] += dp[i][2]
    for j in range(4):
        dp[i + 1][0] %= mod
        dp[i + 1][1] %= mod
        dp[i + 1][2] %= mod
        dp[i + 1][3] %= mod

print(dp[n][3] % mod)
Problem p03291 (Python)

Buggy submission (status: Time Limit Exceeded):
mod = 1000000007

S = input()
n = 1
a = 0
ab = 0
abc = 0
for s in S:
    if s in "A":
        a += n
    if s in "B":
        ab += a
    if s == "C":
        abc += ab
    if s in "?":
        n, a, ab, abc = 3 * n, 3 * a + n, 3 * ab + a, 3 * abc + ab

print(abc % mod)


Fixed submission:
mod = 1000000007

S = input()
n = 1
a = 0
ab = 0
abc = 0
for s in S:
    if s in "A":
        a += n
    if s in "B":
        ab += a
    if s == "C":
        abc += ab
    if s in "?":
        n, a, ab, abc = 3 * n, 3 * a + n, 3 * ab + a, 3 * abc + ab
    n %= mod
    a %= mod
    ab %= mod
    abc %= mod

print(abc % mod)