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

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

using namespace std;

int main() {
  int count_l[55555] = {};

  int N, Q;
  cin >> N >> Q;
  string s;
  cin >> s;
  int sum = 0;
  for (int i = 0; i < s.size(); i++) {
    for (int j = 0; j < 2 && i + j < s.size(); j++) {
      if (s[i] == 'A' && s[i + j] == 'C') {
        sum++;
      }
      count_l[i + j] = sum;
    }
  }
  for (int q = 0; q < Q; q++) {
    int l, r;
    cin >> l >> r;
    cout << count_l[r - 1] - count_l[l - 1] << endl;
  }
}


Fixed submission:
#include <iostream>

using namespace std;

int main() {
  int count_l[555555] = {};

  int N, Q;
  cin >> N >> Q;
  string s;
  cin >> s;
  int sum = 0;
  for (int i = 0; i < s.size(); i++) {
    for (int j = 0; j < 2 && i + j < s.size(); j++) {
      if (s[i] == 'A' && s[i + j] == 'C') {
        sum++;
      }
      count_l[i + j] = sum;
    }
  }
  for (int q = 0; q < Q; q++) {
    int l, r;
    cin >> l >> r;
    cout << count_l[r - 1] - count_l[l - 1] << endl;
  }
}
Problem p03087 (C++)

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

string s;
int n, q;
vector<int> l(200000);
vector<int> r(200000);
vector<int> a(200000);
vector<int> ans(200000);

int main() {
  cin >> n >> q >> s;

  int counts = 0;

  for (int i = 0; i < n; ++i) {
    cin >> l.at(i) >> r.at(i);
  }

  for (int i = 0; i < n; ++i) {
    if (i == n - 1) {
      a.at(i) = counts;
      break;
    }

    if (s.at(i) == 'A' && s.at(i + 1) == 'C') {
      a.at(i) = counts;
      counts++;
      a.at(i + 1) = counts;
    } else {
      a.at(i) = counts;
    }
  }

  for (int i = 0; i < q; ++i) {
    ans.at(i) = a.at(r.at(i) - 1) - a.at(l.at(i) - 1);
  }

  for (int i = 0; i < q; ++i)
    cout << ans.at(i) << endl;
}

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

string s;
int n, q;
vector<int> l(200000);
vector<int> r(200000);
vector<int> a(200000);
vector<int> ans(200000);

int main() {
  cin >> n >> q >> s;

  int counts = 0;

  for (int i = 0; i < q; ++i) {
    cin >> l.at(i) >> r.at(i);
  }

  for (int i = 0; i < n; ++i) {
    if (i == n - 1) {
      a.at(i) = counts;
      break;
    }

    if (s.at(i) == 'A' && s.at(i + 1) == 'C') {
      a.at(i) = counts;
      counts++;
      a.at(i + 1) = counts;
    } else {
      a.at(i) = counts;
    }
  }

  for (int i = 0; i < q; ++i) {
    ans.at(i) = a.at(r.at(i) - 1) - a.at(l.at(i) - 1);
  }

  for (int i = 0; i < q; ++i)
    cout << ans.at(i) << endl;
}
Problem p03087 (C++)

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

int main() {
  int n, q;
  cin >> n >> q;
  string s;
  int ans = 0;
  cin >> s;
  vector<int> apper = {};
  apper.push_back(0);
  // cout <<apper.at(0)<<endl;
  for (int i = 0; i <= s.size() - 1; i++) {
    if (s.at(i) == 'A' and s.at(i + 1) == 'C') {
      ans++;
    }
    apper.push_back(ans);
    // cout << apper.at(i+1)<<endl;
  }
  int st, fin;
  for (int i = 0; i < q; i++) {
    cin >> st >> fin;

    cout << apper.at(fin - 1) - apper.at(st - 1) << endl;
  }
}


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

int main() {
  int n, q;
  cin >> n >> q;
  string s;
  int ans = 0;
  cin >> s;
  vector<int> apper = {};
  apper.push_back(0);
  // cout <<apper.at(0)<<endl;
  for (int i = 0; i < s.size() - 1; i++) {
    if (s.at(i) == 'A' and s.at(i + 1) == 'C') {
      ans++;
    }
    apper.push_back(ans);
    // cout << apper.at(i+1)<<endl;
  }
  int st, fin;
  for (int i = 0; i < q; i++) {
    cin >> st >> fin;

    cout << apper.at(fin - 1) - apper.at(st - 1) << endl;
  }
}
Problem p03087 (C++)

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

int main() {
  int n, q;
  string s;
  cin >> n >> q >> s;

  int ac[10500] = {}, lr[10500] = {};
  for (size_t i = 1, count = 0; i < n; i++) {
    if (s[i - 1] == 'A' && s[i] == 'C')
      count++;
    ac[i] = count;
  }

  for (size_t i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    lr[i] = ac[r - 1] - ac[l - 1];
  }

  for (size_t i = 0; i < q; i++) {
    cout << lr[i] << endl;
  }
}

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

int main() {
  int n, q;
  string s;
  cin >> n >> q >> s;

  int ac[100500] = {}, lr[100500] = {};
  for (size_t i = 1, count = 0; i < n; i++) {
    if (s[i - 1] == 'A' && s[i] == 'C')
      count++;
    ac[i] = count;
  }

  for (size_t i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    lr[i] = ac[r - 1] - ac[l - 1];
  }

  for (size_t i = 0; i < q; i++) {
    cout << lr[i] << endl;
  }
}
Problem p03087 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <functional>
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
typedef long long ll;
#define pb pushback
#define fr(i, n) for (int i = 0; i < n; i++)
#define ifr(i, n) for (int i = n - 1; i >= 0; i--)

int main() {
  int n, q, l, r;
  cin >> n >> q;
  string s;
  int a[n];
  int c = 0;
  a[0] = 0;
  fr(i, n - 1) {
    if (s.substr(i, 2) == "AC") {
      c++;
    }
    a[i + 1] = c;
  }
  fr(i, q) {
    cin >> l >> r;
    cout << a[r - 1] - a[l - 1] << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <functional>
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
typedef long long ll;
#define pb pushback
#define fr(i, n) for (int i = 0; i < n; i++)
#define ifr(i, n) for (int i = n - 1; i >= 0; i--)

int main() {
  int n, q, l, r;
  cin >> n >> q;
  string s;
  int a[n];
  int c = 0;
  a[0] = 0;
  cin >> s;
  fr(i, n - 1) {
    if (s.substr(i, 2) == "AC") {
      c++;
    }
    a[i + 1] = c;
  }
  fr(i, q) {
    cin >> l >> r;
    cout << a[r - 1] - a[l - 1] << endl;
  }
}
Problem p03087 (C++)

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

#define MAX_N 100'000
#define MAX_Q 100'000

using namespace std;

int R[MAX_Q];

int main() {
  int N, Q;
  string S;
  cin >> N >> Q >> S;
  vector<int> V;
  for (int i = 0; i < N - 1; ++i) {
    if (S.substr(i, 2) == "AC") {
      V.push_back(i);
    }
  }
  for (int i = 0; i < Q; ++i) {
    int l, r;
    cin >> l >> r;
    --l;
    --r;
    auto low = lower_bound(V.begin(), V.end(), l);
    auto up = lower_bound(V.begin(), V.end(), r);
    R[i] = (int)(up - (*up != r) - V.begin()) -
           (int)(low + (*low != l) - V.begin()) + 1;
  }
  for (int i = 0; i < Q; ++i) {
    printf("%d\n", R[i]);
  }
}

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

#define MAX_N 100'000
#define MAX_Q 100'000

using namespace std;

int R[MAX_Q];

int main() {
  int N, Q;
  string S;
  cin >> N >> Q >> S;
  vector<int> V;
  for (int i = 0; i < N - 1; ++i) {
    if (S.substr(i, 2) == "AC") {
      V.push_back(i);
    }
  }
  for (int i = 0; i < Q; ++i) {
    int l, r;
    cin >> l >> r;
    if (V.size() > 0) {
      --l;
      --r;
      auto low = lower_bound(V.begin(), V.end(), l);
      auto up = upper_bound(V.begin(), V.end(), r - 1) - 1;
      R[i] = (int)(up - V.begin()) - (int)(low - V.begin()) + 1;
    } else {
      R[i] = 0;
    }
  }
  for (int i = 0; i < Q; ++i) {
    printf("%d\n", R[i]);
  }
}
Problem p03087 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define RFOR(i, a, b) for (int i = (a); i >= (b); --i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define RREP(i, n) for (int i = (n); i > 0; --i)
#define CI cin >>
#define CO cout <<
#define EN << endl
#define all(x) (x).begin(), (x).end()
#define asn ans
using namespace std;
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}
using ll = long long;

int main() {
  int n, q;
  CI n >> q;
  string s;
  CI s;
  int ans[20000] = {};

  for (int i = 0; i < n; i++) {
    if (s[i] == 'A' and s[i + 1] == 'C') {
      ans[i + 1] = ans[i] + 1;
    } else {
      ans[i + 1] = ans[i];
    }
  }
  vector<pair<int, int>> a(q);
  REP(i, q) { CI a[i].first >> a[i].second; }
  REP(i, q) { CO ans[a[i].second - 1] - ans[a[i].first - 1] EN; }
}

Fixed submission:
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define RFOR(i, a, b) for (int i = (a); i >= (b); --i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define RREP(i, n) for (int i = (n); i > 0; --i)
#define CI cin >>
#define CO cout <<
#define EN << endl
#define all(x) (x).begin(), (x).end()
#define asn ans
using namespace std;
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}
using ll = long long;

int main() {
  int n, q;
  CI n >> q;
  string s;
  CI s;
  int ans[200000] = {};

  for (int i = 0; i < n; i++) {
    if (s[i] == 'A' and s[i + 1] == 'C') {
      ans[i + 1] = ans[i] + 1;
    } else {
      ans[i + 1] = ans[i];
    }
  }
  vector<pair<int, int>> a(q);
  REP(i, q) { CI a[i].first >> a[i].second; }
  REP(i, q) { CO ans[a[i].second - 1] - ans[a[i].first - 1] EN; }
}
Problem p03087 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
int main() {
  int n, q;
  string s;
  cin >> n >> q >> s;
  vector<int> a(n, 0);
  for (int i = 1; i < n; ++i) {
    for (int j = 0; j < i; ++j) {
      if (s[j] == 'A' && s[j + 1] == 'C')
        a[i]++;
    }
  }
  vector<int> l(q), r(q);
  rep(i, q) cin >> l[i] >> r[i];
  rep(i, q) {
    l[i]--;
    r[i]--;
    cout << a[r[i]] - a[l[i]] << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
int main() {
  int n, q;
  string s;
  cin >> n >> q >> s;
  vector<int> a(n, 0);
  for (int i = 1; i < n; ++i) {
    if (s[i - 1] == 'A' && s[i] == 'C')
      a[i] = a[i - 1] + 1;
    else
      a[i] = a[i - 1];
  }
  vector<int> l(q), r(q);
  rep(i, q) cin >> l[i] >> r[i];
  rep(i, q) {
    l[i]--;
    r[i]--;
    cout << a[r[i]] - a[l[i]] << endl;
  }
}
Problem p03087 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long int ll;

int main() {
  ll N, Q;
  cin >> N >> Q;
  string S;
  cin >> S;
  vector<ll> l(Q), r(Q);
  for (int i = 0; i < Q; ++i) {
    cin >> l[i] >> r[i];
    l[i]--;
    r[i]--;
  }

  vector<ll> list;
  for (int i = 0; i < N; ++i)
    if (S[i] == 'A' && S[i + 1] == 'C')
      list.push_back(i);
  list.push_back(10e8);
  int i, j, k;
  for (i = 0; i < Q; ++i) {
    for (j = 0; j < list.size() + 1; ++j) {
      if (l[i] <= list[j])
        break;
    }
    for (k = j; k < list.size() + 1; ++k)
      if (r[i] <= list[k])
        break;

    cout << k - j << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long int ll;

int main() {
  ll N, Q;
  cin >> N >> Q;
  string S;
  cin >> S;
  vector<ll> l(Q), r(Q);
  for (int i = 0; i < Q; ++i) {
    cin >> l[i] >> r[i];
    l[i]--;
    r[i]--;
  }

  vector<ll> list;
  for (int i = 0; i < N; ++i)
    if (S[i] == 'A' && S[i + 1] == 'C')
      list.push_back(i);
  list.push_back(10e8);
  int i, j, k;
  for (i = 0; i < Q; ++i) {
    cout << lower_bound(list.begin(), list.end(), r[i]) -
                lower_bound(list.begin(), list.end(), l[i])
         << endl;
  }
}
Problem p03087 (C++)

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

int main() {
  int n, q;
  cin >> n >> q;
  string s;
  cin >> s;
  vector<int> t(n + 1, 0);
  for (int i = 0; i < n; ++i) {
    if (i + 1 < n && s[i] == 'A' && s[i + 1] == 'C') {
      t[i + 1] = t[i] + 1;
    } else
      t[i + 1] = t[i];
    for (int j = 0; j < q; ++j) {
      int l, r;
      cin >> l >> r;
      l--;
      r--;
      cout << t[r] - t[l] << endl;
    }
  }
  return 0;
}

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

int main() {
  int n, q;
  cin >> n >> q;
  string s;
  cin >> s;
  vector<int> t(n + 1, 0);
  for (int i = 0; i < n; ++i) {
    if (i + 1 < n && s[i] == 'A' && s[i + 1] == 'C') {
      t[i + 1] = t[i] + 1;
    } else
      t[i + 1] = t[i];
  }
  for (int j = 0; j < q; ++j) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    cout << t[r] - t[l] << endl;
  }
  return 0;
}
Problem p03087 (C++)

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

int main() {
  int N, Q;
  string S;
  cin >> N >> Q >> S;
  vector<pair<int, int>> num(Q);
  for (int i = 0; i < Q; i++) {
    cin >> num[i].first;
    cin >> num[i].second;
  }
  char first, second;
  vector<int> start;
  for (int i = 0; i < N - 1; i++) {
    first = S[i];
    second = S[i + 1];
    if (S[i] == 'A' && S[i + 1] == 'C') {
      start.push_back(i);
    }
  }

  for (int i = 0; i < Q; i++) {
    int count = 0;
    for (int j = 0; j < start.size(); j++) {
      if (start[j] + 1 >= num[i].first && start[j] + 1 < num[i].second)
        count++;
    }
    cout << count << endl;
  }
  return 0;
}

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

int main() {
  int N, Q;
  string S;
  cin >> N >> Q >> S;
  vector<pair<int, int>> num(Q);
  for (int i = 0; i < Q; i++) {
    cin >> num[i].first;
    cin >> num[i].second;
  }
  char first, second;
  vector<int> start;
  for (int i = 0; i < N - 1; i++) {
    first = S[i];
    second = S[i + 1];
    if (S[i] == 'A' && S[i + 1] == 'C') {
      start.push_back(i);
    }
  }

  for (int i = 0; i < Q; i++) {
    int count = 0;
    vector<int>::iterator first_itr =
        lower_bound(start.begin(), start.end(), num[i].first - 1);
    vector<int>::iterator last_itr =
        lower_bound(start.begin(), start.end(), num[i].second - 1);

    count = distance(first_itr, last_itr);
    cout << count << endl;
  }
  return 0;
}
Problem p03087 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef unsigned long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int ans[100];
int main() {
  int n, q;
  cin >> n >> q;
  string s;
  cin >> s;
  vector<int> v(n);
  v[0] = 0;
  for (int i = 1; i < n; i++) {
    if (s[i] == 'C' && s[i - 1] == 'A')
      v[i] = v[i - 1] + 1;
    else
      v[i] = v[i - 1];
  }
  int id = 0;
  for (int j = 0; j < q; j++) {
    int l, r;
    cin >> l >> r;
    ans[id] = v[r - 1] - v[l - 1];
    id++;
  }
  // for (int h=0;h <= n;h++) cout << v[h];
  // cout << endl;
  for (int i = 0; i < q; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef unsigned long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int ans[100010];
int main() {
  int n, q;
  cin >> n >> q;
  string s;
  cin >> s;
  vector<int> v(n);
  v[0] = 0;
  for (int i = 1; i < n; i++) {
    if (s[i] == 'C' && s[i - 1] == 'A')
      v[i] = v[i - 1] + 1;
    else
      v[i] = v[i - 1];
  }
  int id = 0;
  for (int j = 0; j < q; j++) {
    int l, r;
    cin >> l >> r;
    ans[id] = v[r - 1] - v[l - 1];
    id++;
  }
  // for (int h=0;h <= n;h++) cout << v[h];
  // cout << endl;
  for (int i = 0; i < q; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}
Problem p03087 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long int LL;
using namespace std;
#define INF (1LL << 60)
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, k, n) for (int i = (k); (i) < (n); (i)++)
#define REP(i, n) for (int i = 0; (i) < (n); (i)++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define F first
#define S second

int main() {
  int n, q;
  cin >> n >> q;
  string s;
  cin >> s;
  vector<int> l(q);
  vector<int> r(q);

  REP(i, q) {
    cin >> l[i] >> r[i];
    l[i]--;
    r[i]--;
  }

  vector<int> ac;
  REP(i, n - 1) {
    if (s[i] == 'A' && s[i + 1] == 'C') {
      ac.push_back(i);
      i++;
    }
  }
  if (ac.size() == 0) {
    REP(i, q) { cout << endl; }
  }

  REP(i, q) {
    int ans = 0;
    auto itr1 = lower_bound(ALL(ac), l[i]);
    auto itr2 = lower_bound(ALL(ac), r[i]);
    if (*(itr2 - 1) < r[i]) {
      ans = distance(itr1, itr2 - 1) + 1;
    } else {
      ans = distance(itr1, itr2 - 2) + 1;
    }
    cout << ans << endl;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long int LL;
using namespace std;
#define INF (1LL << 60)
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, k, n) for (int i = (k); (i) < (n); (i)++)
#define REP(i, n) for (int i = 0; (i) < (n); (i)++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define F first
#define S second

int main() {
  int n, q;
  cin >> n >> q;
  string s;
  cin >> s;
  vector<int> l(q);
  vector<int> r(q);

  REP(i, q) {
    cin >> l[i] >> r[i];
    l[i]--;
    r[i]--;
  }

  vector<int> ac;
  REP(i, n - 1) {
    if (s[i] == 'A' && s[i + 1] == 'C') {
      ac.push_back(i);
      i++;
    }
  }
  if (ac.size() == 0) {
    REP(i, q) { cout << 0 << endl; }
    return 0;
  }

  REP(i, q) {
    int ans = 0;
    auto itr1 = lower_bound(ALL(ac), l[i]);
    auto itr2 = lower_bound(ALL(ac), r[i]);
    if (*(itr2 - 1) < r[i]) {
      ans = distance(itr1, itr2 - 1) + 1;
    } else {
      ans = distance(itr1, itr2 - 2) + 1;
    }
    cout << ans << endl;
  }

  return 0;
}
Problem p03087 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;
using namespace std;
const ll INF = 1000000007;

int main() {
  int n, q;
  cin >> n >> q;
  string s;
  cin >> s;
  vector<int> l(q), r(q);
  rep(i, q) cin >> l[i] >> r[i];
  vector<int> a(n + 1);
  a[0] = 0;
  a[1] = 0;
  for (int i = 1; i < n; i++) {
    if (s[i - 1] == 'A' && s[i] == 'C')
      a[i + 1] = a[i] + 1;
    else
      a[i + 1] = a[i];
  }
  rep(i, q) {
    int ansi = a[r[i]] - a[l[i] - 1];
    if (l[i] > 1 && s[l[i] - 1] == 'C' && s[l[i - 2]] == 'A')
      ansi--;
    cout << ansi << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;
using namespace std;
const ll INF = 1000000007;

int main() {
  int n, q;
  cin >> n >> q;
  string s;
  cin >> s;
  vector<int> l(q), r(q);
  rep(i, q) cin >> l[i] >> r[i];
  vector<int> a(n + 1);
  a[0] = 0;
  a[1] = 0;
  for (int i = 1; i < n; i++) {
    if (s[i - 1] == 'A' && s[i] == 'C')
      a[i + 1] = a[i] + 1;
    else
      a[i + 1] = a[i];
  }
  rep(i, q) {
    int ansi = a[r[i]] - a[l[i] - 1];
    if (l[i] > 1 && s[l[i] - 1] == 'C' && s[l[i] - 2] == 'A')
      ansi--;
    cout << ansi << endl;
  }
}
Problem p03087 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <cstring>
#include <functional>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
using namespace std;
typedef long long ll;

int main(void) {
  // Your code here
  int N, Q;
  string S;
  cin >> N >> Q >> S;
  vector<int> dp(N);
  dp.at(0) = 0;
  int cnt = 0;
  for (int i = 0; i < N; i++) {
    dp.at(i) = cnt;
    if (S.at(i) == 'A' && S.at(i + 1) == 'C') {
      cnt++;
    }
  }

  for (int i = 0; i < Q; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    cout << dp.at(y) - dp.at(x) << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <cstring>
#include <functional>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
using namespace std;
typedef long long ll;

int main(void) {
  // Your code here
  int N, Q;
  string S;
  cin >> N >> Q >> S;
  vector<int> dp(N);
  dp.at(0) = 0;
  int cnt = 0;
  for (int i = 0; i < N; i++) {
    dp.at(i) = cnt;
    if (S.at(i) == 'A' && i + 1 < N && S.at(i + 1) == 'C') {
      cnt++;
    }
  }

  for (int i = 0; i < Q; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    cout << dp.at(y) - dp.at(x) << endl;
  }
}
Problem p03087 (C++)

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

int main() {
  int N, Q;
  string S;
  cin >> N >> Q >> S;

  vector<int> vec(N + 1);
  vec.at(0) = 0;
  int ACcount = 0;
  for (int i = 1; i <= N; i++) {
    if (S.at(i - 1) == 'A' && S.at(i) == 'C') {
      ACcount++;
      vec.at(i) = ACcount;
    } else {
      vec.at(i) = ACcount;
    }
  }

  for (int i = 0; i < Q; i++) {
    int l, r;
    int count = 0;
    cin >> l >> r;
    count = vec.at(r - 1) - vec.at(l - 1);
    cout << count << endl;
  }
}

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

int main() {
  int N, Q;
  string S;
  cin >> N >> Q >> S;
  S += 'B';
  vector<int> vec(N + 1);
  vec.at(0) = 0;
  int ACcount = 0;
  for (int i = 1; i <= N; i++) {
    if (S.at(i - 1) == 'A' && S.at(i) == 'C') {
      ACcount++;
      vec.at(i) = ACcount;
    } else {
      vec.at(i) = ACcount;
    }
  }

  for (int i = 0; i < Q; i++) {
    int l, r;
    int count = 0;
    cin >> l >> r;
    count = vec.at(r - 1) - vec.at(l - 1);
    cout << count << endl;
  }
}
Problem p03087 (C++)

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

int main() {
  int n;
  int q;
  string s;

  cin >> n >> q >> s;

  int l[q];
  int r[q];
  int sum[n];
  int ans = 0;

  for (int i = 0; i < q; i++) {
    cin >> l[i] >> r[i];
  }

  for (int i = 1; i < n; i++) {
    if (s[i - 1] == 'A' and s[i] == 'C') {
      sum[i + 1] = sum[i] + 1;
    } else {
      sum[i + 1] = sum[i];
    }
  }
  for (int i = 0; i < n; i++) {
    cout << sum[r[i]] - sum[l[i]] << endl;
  }

  return 0;
}

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

int main() {
  int n;
  int q;
  string s;

  cin >> n >> q >> s;

  int l[q];
  int r[q];
  int sum[n];
  int ans = 0;

  for (int i = 0; i < q; i++) {
    cin >> l[i] >> r[i];
  }

  for (int i = 1; i < n; i++) {
    if (s[i - 1] == 'A' and s[i] == 'C') {
      sum[i + 1] = sum[i] + 1;
    } else {
      sum[i + 1] = sum[i];
    }
  }
  for (int i = 0; i < q; i++) {
    cout << sum[r[i]] - sum[l[i]] << endl;
  }

  return 0;
}
Problem p03087 (C++)

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

int main() {
  string s;
  int n, q, m = 0;
  int a[10001];

  cin >> n >> q >> s;

  a[0] = 0;

  for (int j = 0; j < s.size(); j++) {
    if (s[j] == 'A' && s[j + 1] == 'C')
      m++;
    a[j + 1] = m;
  }

  for (int i = 0; i < q; i++) {
    int l, r;

    cin >> l >> r;

    cout << a[r - 1] - a[l - 1] << endl;
  }

  return 0;
}

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

int main() {
  string s;
  int n, q, m = 0;
  int a[100001];

  cin >> n >> q >> s;

  a[0] = 0;

  for (int j = 0; j < s.size(); j++) {
    if (s[j] == 'A' && s[j + 1] == 'C')
      m++;
    a[j + 1] = m;
  }

  for (int i = 0; i < q; i++) {
    int l, r;

    cin >> l >> r;

    cout << a[r - 1] - a[l - 1] << endl;
  }

  return 0;
}
Problem p03087 (C++)

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

int main() {
  int N, Q;
  cin >> N >> Q;
  string str;
  cin >> str;

  // 累積和
  vector<int> s(N + 1, 0);
  for (int i = 0; i < N; ++i) {
    if (i + 1 < N && str[i] == 'A' && str[i + 1] == 'C')
      s[i + 1] = s[i] + 1;
    else
      s[i + 1] = s[i];
  }

  // クエリ
  for (int q = 0; q < Q; ++Q) {
    int l, r;
    cin >> l >> r;
    --l, --r; // 0-indexed にする
    cout << s[r] - s[l] << endl;
  }
}


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

int main() {
  int N, Q;
  cin >> N >> Q;
  string str;
  cin >> str;

  // 累積和
  vector<int> s(N + 1, 0);
  for (int i = 0; i < N; ++i) {
    if (i + 1 < N && str[i] == 'A' && str[i + 1] == 'C')
      s[i + 1] = s[i] + 1;
    else
      s[i + 1] = s[i];
  }

  // クエリ
  for (int q = 0; q < Q; ++q) {
    int l, r;
    cin >> l >> r;
    --l, --r; // 0-indexed にする
    cout << s[r] - s[l] << endl;
  }
}
Problem p03088 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
#define ll long long
const ll mod = 1e9 + 7;
ll substring_count[105][4][4][4];

ll add(ll &a, ll &b) { a = (a + b) % mod; }

bool ng(int a, int b, int c) {
  if (a == 1 && b == 2 && c == 3) {
    return true;
  } else {
    return false;
  }
}

int main() {
  int n;
  cin >> n;
  substring_count[0][0][0][0] = 1;

  for (int i = 0; i < n; ++i) {
    for (int a = 0; a < 4; ++a) {
      for (int b = 0; b < 4; ++b) {
        for (int c = 0; c < 4; ++c) {
          for (int d = 0; d < 4; ++d) {

            if (ng(b, c, d) || ng(c, b, d) || ng(b, d, c))
              continue;
            if (ng(a, c, d) || ng(a, b, d))
              continue;

            add(substring_count[i + 1][b][c][d], substring_count[i][a][b][c]);
          }
        }
      }
    }
  }
  ll ans = 0;
  for (int a = 0; a < 4; ++a) {
    for (int b = 0; b < 4; ++b) {
      for (int c = 0; c < 4; ++c) {
        add(ans, substring_count[n][a][b][c]);
      }
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <iostream>
using namespace std;
#define ll long long
const ll mod = 1e9 + 7;
ll substring_count[105][4][4][4];

void add(ll &a, ll &b) { a = (a + b) % mod; }

bool ng(int a, int b, int c) {
  if (a == 1 && b == 2 && c == 3) {
    return true;
  } else {
    return false;
  }
}

int main() {
  int n;
  cin >> n;
  substring_count[0][0][0][0] = 1;

  for (int i = 0; i < n; ++i) {
    for (int a = 0; a < 4; ++a) {
      for (int b = 0; b < 4; ++b) {
        for (int c = 0; c < 4; ++c) {
          for (int d = 0; d < 4; ++d) {

            if (ng(b, c, d) || ng(c, b, d) || ng(b, d, c))
              continue;
            if (ng(a, c, d) || ng(a, b, d))
              continue;

            add(substring_count[i + 1][b][c][d], substring_count[i][a][b][c]);
          }
        }
      }
    }
  }
  ll ans = 0;
  for (int a = 0; a < 4; ++a) {
    for (int b = 0; b < 4; ++b) {
      for (int c = 0; c < 4; ++c) {
        add(ans, substring_count[n][a][b][c]);
      }
    }
  }
  cout << ans << endl;
}
Problem p03088 (C++)

Buggy submission (status: Time Limit Exceeded):
// https://atcoder.jp/contests/abc122/tasks/abc122_d
#include <iostream>
#include <string.h>
using namespace std;
const long mod = 1e9 + 7;
long N, memo[110][6][6][6][6];

// A=0,G=1,C=2,T=3
long judge(int now, int b3, int b2, int b1, int b) {
  if (b3 == 0 && b2 == 1 && b == 2)
    return 0; // AG?Cの場合
  if (b3 == 0 && b1 == 1 && b == 2)
    return 0; // A?CGの場合
  if (b2 == 0 && b1 == 1 && b == 2)
    return 0; // ?AGCの場合
  if (b2 == 0 && b1 == 2 && b == 1)
    return 0; // ?ACGの場合
  if (b2 == 1 && b1 == 0 && b == 2)
    return 0; // ?GACの場合
  if (now == N)
    return 1;
  // 答えを再帰で求める
  long ans = 0;
  for (int i = 0; i < 4; i++) {
    ans += judge(now + 1, b2, b1, b, i);
  }
  return memo[now][b3][b2][b1][b] = ans % mod;
}
int main() {
  cin >> N;
  memset(memo, -1, sizeof(memo));
  cout << judge(0, 3, 3, 3, 3) << endl;
}


Fixed submission:
// https://atcoder.jp/contests/abc122/tasks/abc122_d
#include <iostream>
#include <string.h>
using namespace std;
const long mod = 1e9 + 7;
long N, memo[110][6][6][6][6];

// A=0,G=1,C=2,T=3
long judge(int now, int b3, int b2, int b1, int b) {
  if (memo[now][b3][b2][b1][b] != -1)
    return memo[now][b3][b2][b1][b];
  if (b3 == 0 && b2 == 1 && b == 2)
    return 0; // AG?Cの場合
  if (b3 == 0 && b1 == 1 && b == 2)
    return 0; // A?CGの場合
  if (b2 == 0 && b1 == 1 && b == 2)
    return 0; // ?AGCの場合
  if (b2 == 0 && b1 == 2 && b == 1)
    return 0; // ?ACGの場合
  if (b2 == 1 && b1 == 0 && b == 2)
    return 0; // ?GACの場合
  if (now == N)
    return 1;
  // 答えを再帰で求める
  long ans = 0;
  for (int i = 0; i < 4; i++) {
    ans += judge(now + 1, b2, b1, b, i);
  }
  return memo[now][b3][b2][b1][b] = ans % mod;
}
int main() {
  cin >> N;
  memset(memo, -1, sizeof(memo));
  cout << judge(0, 3, 3, 3, 3) << endl;
}
Problem p03088 (Python)

Buggy submission (status: Runtime Error):
def main():
    N = int(input())
    dp = [
        [[[0 for _ in range(4)] for _ in range(4)] for _ in range(4)]
        for _ in range(101)
    ]
    dp[0][3][3][3] = 1
    mod = 1_000_000_007
    for n in range(100):
        for i in range(4):
            for j in range(4):
                for k in range(4):
                    if dp[n][i][j][k] == 0:
                        continue
                    for a in range(4):
                        if (
                            (j == 0 and i == 1 and a == 2)
                            or (j == 0 and i == 2 and a == 1)
                            or (j == 1 and i == 0 and a == 2)
                            or (k == 0 and j == 1 and a == 2)
                            or (k == 0 and i == 1 and a == 2)
                        ):
                            continue
                        dp[n + 1][a][i][j] += dp[n][i][j][k]
                        dp[n + 1][a][i][j] %= mod
    ans = 0
    for i in range(4):
        for j in range(4):
            for k in range(4):
                ans += dp[N][i][j][k]
                ans %= mod
    print(ans)


main()


Fixed submission:
def main():
    N = int(input())
    dp = [
        [[[0 for _ in range(4)] for _ in range(4)] for _ in range(4)]
        for _ in range(101)
    ]
    dp[0][3][3][3] = 1
    mod = 10**9 + 7
    for n in range(100):
        for i in range(4):
            for j in range(4):
                for k in range(4):
                    if dp[n][i][j][k] == 0:
                        continue
                    for a in range(4):
                        if (
                            (j == 0 and i == 1 and a == 2)
                            or (j == 0 and i == 2 and a == 1)
                            or (j == 1 and i == 0 and a == 2)
                            or (k == 0 and j == 1 and a == 2)
                            or (k == 0 and i == 1 and a == 2)
                        ):
                            continue
                        dp[n + 1][a][i][j] += dp[n][i][j][k]
                        dp[n + 1][a][i][j] %= mod
    ans = 0
    for i in range(4):
        for j in range(4):
            for k in range(4):
                ans += dp[N][i][j][k]
                ans %= mod
    print(ans)


main()
Problem p03088 (Python)

Buggy submission (status: Runtime Error):
# 解説を参考に作成
# import sys
# sys.setrecursionlimit(10 ** 6)
# import bisect
# from collections import deque
from decorator import stop_watch


@stop_watch
def solve(N):
    mod = 10**9 + 7
    dp = [{} for _ in range(N + 1)]
    dp[0].setdefault("TTT", 1)

    def check(last4):
        for i in range(4):
            x = list(last4)
            if i > 0:
                x[i], x[i - 1] = x[i - 1], x[i]
            if "".join(x).count("AGC") >= 1:
                return False
        return True

    for i in range(1, N + 1):
        for last3, count in dp[i - 1].items():
            for c in "AGCT":
                if check(last3 + c):
                    new_last3 = last3[1:] + c
                    dp[i].setdefault(last3[1:] + c, 0)
                    dp[i][new_last3] = (dp[i][new_last3] + count) % mod

    print(sum(dp[-1].values()) % mod)


if __name__ == "__main__":
    N = int(input())
    solve(N)

    # # test
    # from random import randint
    # from func import random_str
    # solve()


Fixed submission:
# 解説を参考に作成
# import sys
# sys.setrecursionlimit(10 ** 6)
# import bisect
# from collections import deque
# from decorator import stop_watch
#
#
# @stop_watch
def solve(N):
    mod = 10**9 + 7
    dp = [{} for _ in range(N + 1)]
    dp[0].setdefault("TTT", 1)

    def check(last4):
        for i in range(4):
            x = list(last4)
            if i > 0:
                x[i], x[i - 1] = x[i - 1], x[i]
            if "".join(x).count("AGC") >= 1:
                return False
        return True

    for i in range(1, N + 1):
        for last3, count in dp[i - 1].items():
            for c in "AGCT":
                if check(last3 + c):
                    new_last3 = last3[1:] + c
                    dp[i].setdefault(last3[1:] + c, 0)
                    dp[i][new_last3] = (dp[i][new_last3] + count) % mod

    print(sum(dp[-1].values()) % mod)


if __name__ == "__main__":
    N = int(input())
    solve(N)

    # # test
    # from random import randint
    # from func import random_str
    # solve()
Problem p03088 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

#define MOD 1000000007

vector<unordered_map<string, int>> memo(100);
int N;

bool ok(string s) {
  if (s.find("AGC") != string::npos) {
    return false;
  }
  for (int i = 0; i < 3; i++) {
    string copied = s;
    char tmp = copied[i];
    copied[i] = copied[i + 1];
    copied[i + 1] = tmp;
    if (copied.find("AGC") != string::npos) {
      return false;
    }
  }
  return true;
}

int dfs(int cur, string last) {
  auto it = memo[cur].find(last);
  if (it != memo[cur].end()) {
    return memo[cur][last];
  }
  if (cur == N)
    return 1;
  int cnt = 0;
  for (auto c : "AGCT") {
    if (c == 0)
      continue;
    string newstr = last + c;
    if (ok(newstr)) {
      cnt = (cnt + dfs(cur + 1, newstr.substr(1))) % MOD;
    }
  }
  memo[cur][last] = cnt;
  return cnt;
}

int main() {
  cin >> N;
  cout << dfs(0, "XXX") << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

#define MOD 1000000007

vector<unordered_map<string, int>> memo(200);
int N;

bool ok(string s) {
  if (s.find("AGC") != string::npos) {
    return false;
  }
  for (int i = 0; i < 3; i++) {
    string copied = s;
    char tmp = copied[i];
    copied[i] = copied[i + 1];
    copied[i + 1] = tmp;
    if (copied.find("AGC") != string::npos) {
      return false;
    }
  }
  return true;
}

int dfs(int cur, string last) {
  auto it = memo[cur].find(last);
  if (it != memo[cur].end()) {
    return memo[cur][last];
  }
  if (cur == N)
    return 1;
  int cnt = 0;
  for (auto c : "AGCT") {
    if (c == 0)
      continue;
    string newstr = last + c;
    if (ok(newstr)) {
      cnt = (cnt + dfs(cur + 1, newstr.substr(1))) % MOD;
    }
  }
  memo[cur][last] = cnt;
  return cnt;
}

int main() {
  cin >> N;
  cout << dfs(0, "XXX") << endl;
  return 0;
}
Problem p03088 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <string.h>

using namespace std;

#define MAX_LENGTH 100
#define MOD 1000000007

int N;
long long memo[110][6][6][6][6];

// A = 0, C = 1, G = 2, T = 3
long long count(int cur, int c3, int c2, int c1, int input) {
  // ?AGC
  if (c2 == 0 && c1 == 2 && input == 1) {
    return 0;
    // AG?C
  } else if (c3 == 0 && c2 == 2 && input == 1) {
    return 0;
    // ?GAC
  } else if (c2 == 2 && c1 == 0 && input == 1) {
    return 0;
    // ?ACG
  } else if (c2 == 0 && c1 == 1 && input == 2) {
    return 0;
    // A?GC
  } else if (c3 == 0 && c1 == 2 && input == 1) {
    return 0;
  }

  if (cur == N) {
    return 1;
  }

  long long ans = 0;

  for (int i = 0; i < 4; i++) {
    ans += count(cur + 1, c2, c1, input, i);
  }

  memo[cur][c3][c2][c1][input] = ans % MOD;

  return memo[cur][c3][c2][c1][input];
}

int main() {
  memset(memo, -1, sizeof(memo));
  cin >> N;
  cout << count(0, 3, 3, 3, 3) << endl;

  return 0;
}

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

using namespace std;

#define MAX_LENGTH 100
#define MOD 1000000007

int N;
long long memo[110][6][6][6][6];

// A = 0, C = 1, G = 2, T = 3
long long count(int cur, int c3, int c2, int c1, int input) {
  if (memo[cur][c3][c2][c1][input] != -1) {
    return memo[cur][c3][c2][c1][input];
  }

  // ?AGC
  if (c2 == 0 && c1 == 2 && input == 1) {
    return 0;
    // AG?C
  } else if (c3 == 0 && c2 == 2 && input == 1) {
    return 0;
    // ?GAC
  } else if (c2 == 2 && c1 == 0 && input == 1) {
    return 0;
    // ?ACG
  } else if (c2 == 0 && c1 == 1 && input == 2) {
    return 0;
    // A?GC
  } else if (c3 == 0 && c1 == 2 && input == 1) {
    return 0;
  }

  if (cur == N) {
    return 1;
  }

  long long ans = 0;

  for (int i = 0; i < 4; i++) {
    ans += count(cur + 1, c2, c1, input, i);
  }

  memo[cur][c3][c2][c1][input] = ans % MOD;

  return memo[cur][c3][c2][c1][input];
}

int main() {
  memset(memo, -1, sizeof(memo));
  cin >> N;
  cout << count(0, 3, 3, 3, 3) << endl;

  return 0;
}
Problem p03088 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return
// true; } return false; } template<class T> inline bool chmin(T& a, T b) { if
// (a > b) { a = b; return true; } return false; }

// long longのシフト演算には気をつけよう

int dp[101][4][4][4];

int main() {
  int N;
  cin >> N;
  int mod = 1000000007;

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

  for (int len = 0; len < N; len++) {
    for (int i = 0; i < 4; i++) {
      for (int j = 0; j < 4; j++) {
        for (int k = 0; k < 4; k++) {
          if (dp[len][i][j][k] == 0)
            continue;

          for (int a = 0; a < 4; i++) {

            if (j == 0 && k == 1 && a == 2)
              continue;
            if (j == 1 && k == 0 && a == 2)
              continue;
            if (j == 0 && k == 2 && a == 1)
              continue;
            if (i == 0 && k == 1 && a == 2)
              continue;
            if (i == 0 && j == 1 && a == 2)
              continue;

            dp[len + 1][j][k][a] += dp[len][i][j][k];
            dp[len + 1][j][k][a] %= mod;
          }
        }
      }
    }
  }

  int ans = 0;

  for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 4; j++) {
      for (int k = 0; k < 4; k++) {
        ans += dp[N][i][j][k];
        ans %= mod;
      }
    }
  }

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return
// true; } return false; } template<class T> inline bool chmin(T& a, T b) { if
// (a > b) { a = b; return true; } return false; }

// long longのシフト演算には気をつけよう

int dp[101][4][4][4];

int main() {
  int N;
  cin >> N;
  int mod = 1000000007;

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

  for (int len = 0; len < N; len++) {
    for (int i = 0; i < 4; i++) {
      for (int j = 0; j < 4; j++) {
        for (int k = 0; k < 4; k++) {
          if (dp[len][i][j][k] == 0)
            continue;

          for (int a = 0; a < 4; a++) {

            if (j == 0 && k == 1 && a == 2)
              continue;
            if (j == 1 && k == 0 && a == 2)
              continue;
            if (j == 0 && k == 2 && a == 1)
              continue;
            if (i == 0 && k == 1 && a == 2)
              continue;
            if (i == 0 && j == 1 && a == 2)
              continue;

            dp[len + 1][j][k][a] += dp[len][i][j][k];
            dp[len + 1][j][k][a] %= mod;
          }
        }
      }
    }
  }

  int ans = 0;

  for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 4; j++) {
      for (int k = 0; k < 4; k++) {
        ans += dp[N][i][j][k];
        ans %= mod;
      }
    }
  }

  cout << ans << endl;
}
Problem p03088 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
#define rep(i, n) for (ll i = 0; i < n; i++)
ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  while (b > 0) {
    ll p = a % b;
    a = b;
    b = p;
  }
  return a;
}
ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return (a / g) * b;
}
int main() {
  ll n;
  cin >> n;
  vector<vector<vector<vector<ll>>>> dp(
      n + 1,
      vector<vector<vector<ll>>>(4, vector<vector<ll>>(4, vector<ll>(4, 0))));
  rep(j, 4) {
    rep(k, 4) {
      rep(l, 4) {
        if (!((j == 0 && k == 1 && l == 2) || (j == 0 && k == 2 && l == 1) ||
              (j == 1 && k == 0 && l == 2))) {
          dp[3][j][k][l] = 1;
        }
      }
    }
  }
  for (int i = 4; i < n + 1; i++) {
    rep(j, 4) {
      rep(k, 4) {
        rep(l, 4) {
          rep(m, 4) {
            if (!((j == 0 && k == 1 && m == 2) ||
                  (j == 0 && l == 1 && m == 2) ||
                  (k == 0 && l == 1 && m == 2) ||
                  (k == 0 && l == 2 && m == 1) ||
                  (k == 1 && l == 0 && m == 2))) {
              dp[i + 1][k][l][m] += dp[i][j][k][l];
              dp[i + 1][k][l][m] %= mod;
            }
          }
        }
      }
    }
  }
  ll sum = 0;
  rep(j, 4) {
    rep(k, 4) {
      rep(l, 4) {
        sum += dp[n][j][k][l];
        sum %= mod;
      }
    }
  }
  cout << sum << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
#define rep(i, n) for (ll i = 0; i < n; i++)
ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  while (b > 0) {
    ll p = a % b;
    a = b;
    b = p;
  }
  return a;
}
ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return (a / g) * b;
}
int main() {
  ll n;
  cin >> n;
  vector<vector<vector<vector<ll>>>> dp(
      n + 1,
      vector<vector<vector<ll>>>(4, vector<vector<ll>>(4, vector<ll>(4, 0))));
  rep(j, 4) {
    rep(k, 4) {
      rep(l, 4) {
        if (!((j == 0 && k == 1 && l == 2) || (j == 0 && k == 2 && l == 1) ||
              (j == 1 && k == 0 && l == 2))) {
          dp[3][j][k][l] = 1;
        }
      }
    }
  }
  for (int i = 3; i < n; i++) {
    rep(j, 4) {
      rep(k, 4) {
        rep(l, 4) {
          rep(m, 4) {
            if (!((j == 0 && k == 1 && m == 2) ||
                  (j == 0 && l == 1 && m == 2) ||
                  (k == 0 && l == 1 && m == 2) ||
                  (k == 0 && l == 2 && m == 1) ||
                  (k == 1 && l == 0 && m == 2))) {
              dp[i + 1][k][l][m] += dp[i][j][k][l];
              dp[i + 1][k][l][m] %= mod;
            }
          }
        }
      }
    }
  }
  ll sum = 0;
  rep(j, 4) {
    rep(k, 4) {
      rep(l, 4) {
        sum += dp[n][j][k][l];
        sum %= mod;
      }
    }
  }
  cout << sum << endl;
}
Problem p03089 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

static std::vector<int> as;
static std::vector<int> bs;
static int n;

bool solve(int z) {
  if (z <= 0) {
    return true;
  }
  int i = z - 1;
  int j = n - 1;
  for (; i >= 0; --i, --j) {
    while (bs[j] < 0) {
      --j;
    }
    if (bs[j] != i) {
      continue;
    }
    bs[j] = -1;
    if (solve(z - 1)) {
      as[z - 1] = i + 1;
      return true;
    }
    bs[j] = i;
  }
  return false;
}

int main() {
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);
  std::cin >> n;
  bs.resize(n);
  for (int i = 0; i < n; ++i) {
    int b;
    std::cin >> b;
    bs[i] = b - 1;
  }
  as.resize(n);
  auto const found = solve(n);
  if (!found) {
    std::cout << -1 << std::endl;
    return 0;
  }
  for (auto const a : as) {
    std::cout << a << std::endl;
  }
  return 0;
}


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

using namespace std;

static std::vector<int> as;
static std::vector<int> bs;
static int n;

bool solve(int z) {
  if (z <= 0) {
    return true;
  }
  int i = z - 1;
  int j = n - 1;
  for (; i >= 0; --i, --j) {
    while (bs[j] < 0) {
      --j;
    }
    if (bs[j] > i) {
      return false;
    }
  }
  i = z - 1;
  j = n - 1;
  for (; i >= 0; --i, --j) {
    while (bs[j] < 0) {
      --j;
    }
    if (bs[j] != i) {
      continue;
    }
    bs[j] = -1;
    if (solve(z - 1)) {
      as[z - 1] = i + 1;
      return true;
    }
    bs[j] = i;
  }
  return false;
}

int main() {
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);
  std::cin >> n;
  bs.resize(n);
  for (int i = 0; i < n; ++i) {
    int b;
    std::cin >> b;
    bs[i] = b - 1;
  }
  as.resize(n);
  auto const found = solve(n);
  if (!found) {
    std::cout << -1 << std::endl;
    return 0;
  }
  for (auto const a : as) {
    std::cout << a << std::endl;
  }
  return 0;
}
Problem p03089 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;
int n, a;
vector<int> b, ans;
int main(void) {
  // Your code here!
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a;
    b.push_back(a);
  }
  while (b.size() != 0) {
    bool jud = false;
    for (int i = b.size() - 1; i >= 0; i--) {
      if (b[i] == i + 1) {
        ans.push_back(b[i]);
        b.erase(b.begin() + i);
        jud = true;
        break;
      }
    }
    if (!jud) {
      cout << -1 << endl;
      return 1;
    }
  }
  for (int i = ans.size() - 1; i >= 0; i--) {
    cout << ans[i] << endl;
  }
}


Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
int n, a;
vector<int> b, ans;
int main(void) {
  // Your code here!
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a;
    b.push_back(a);
  }
  while (b.size() != 0) {
    bool jud = false;
    for (int i = b.size() - 1; i >= 0; i--) {
      if (b[i] == i + 1) {
        ans.push_back(b[i]);
        b.erase(b.begin() + i);
        jud = true;
        break;
      }
    }
    if (!jud) {
      cout << -1 << endl;
      return 0;
    }
  }
  for (int i = ans.size() - 1; i >= 0; i--) {
    cout << ans[i] << endl;
  }
}
Problem p03089 (C++)

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

int search(int N, vector<int> b) {
  for (int i = N; i >= 1; i--) {
    if (b[i - 1] > i)
      return -1;
    if (b[i - 1] == i)
      return i;
  }
}

int main() {
  int N;
  cin >> N;
  vector<int> b(N);
  for (int i = 0; i < N; i++)
    cin >> b[i];

  vector<int> ans(N, 0);

  for (int size = N; size >= 1; size--) {
    int pos = search(size, b);
    if (pos == -1) {
      cout << -1 << endl;
      return 1;
    } else {
      ans[size - 1] = pos;
      b.erase(b.begin() + pos - 1);
    }
  }

  for (int i = 0; i < N; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}

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

int search(int N, vector<int> b) {
  for (int i = N; i >= 1; i--) {
    if (b[i - 1] > i)
      return -1;
    if (b[i - 1] == i)
      return i;
  }
}

int main() {
  int N;
  cin >> N;
  vector<int> b(N);
  for (int i = 0; i < N; i++)
    cin >> b[i];

  vector<int> ans(N, 0);

  for (int size = N; size >= 1; size--) {
    int pos = search(size, b);
    if (pos == -1) {
      cout << -1 << endl;
      return 0;
    } else {
      ans[size - 1] = pos;
      b.erase(b.begin() + pos - 1);
    }
  }

  for (int i = 0; i < N; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}