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

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

int main() {
  long long int last = -1, ans = 0;
  vector<vector<int>> v(26);
  string s, t;
  cin >> s >> t;
  for (int i = 0; i < s.size(); ++i) {
    v[s[i] - 'a'].push_back(i);
  }
  for (int i = 0; i < t.size(); ++i) {
    if (v[t[i] - 'a'].size() == 0) {
      cout << -1 << '\n';
      return 0;
    } else {
      int isReturn = 1;
      for (int j = 0; j < v[t[i] - 'a'].size(); ++j) {
        int n = v[t[i] - 'a'][j];
        if (n > last) {
          ans += n - last;
          last = n;
          isReturn = 0;
          break;
        }
      }
      if (isReturn) {
        ans += s.size() - last + v[t[i] - 'a'][0];
        last = v[t[i] - 'a'][0];
      }
    }
  }
  cout << ans << '\n';
}


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

int main() {
  long long int last = -1, ans = 0;
  vector<vector<int>> v(26);
  string s, t;
  cin >> s >> t;
  for (int i = 0; i < s.size(); ++i) {
    v[s[i] - 'a'].push_back(i);
  }
  for (int i = 0; i < t.size(); ++i) {
    if (v[t[i] - 'a'].size() == 0) {
      cout << -1 << '\n';
      return 0;
    } else {
      int isReturn = 1;
      int idx = distance(
          v[t[i] - 'a'].begin(),
          lower_bound(v[t[i] - 'a'].begin(), v[t[i] - 'a'].end(), last));
      for (int j = idx; j < v[t[i] - 'a'].size(); ++j) {
        int n = v[t[i] - 'a'][j];
        if (n > last) {
          ans += n - last;
          last = n;
          isReturn = 0;
          break;
        }
      }
      if (isReturn) {
        ans += s.size() - last + v[t[i] - 'a'][0];
        last = v[t[i] - 'a'][0];
      }
    }
  }
  cout << ans << '\n';
}
Problem p02937 (Python)

Buggy submission (status: Time Limit Exceeded):
def main():
    s = input()
    t = input()

    if not set(t) <= set(s):
        print(-1)
        return

    s_indexes = {c: [] for c in "abcdefghijklmnopqrstuvwxyz"}
    for i, s_c in enumerate(s):
        s_indexes[s_c].append(i)
    s_pointer = {c: 0 for c in "abcdefghijklmnopqrstuvwxyz"}
    s_length = {c: len(s_indexes[c]) for c in "abcdefghijklmnopqrstuvwxyz"}

    power = 0
    last_index = -1
    for i, t_c in enumerate(t):
        if s_length[t_c] <= s_pointer[t_c]:
            # reset
            s_pointer = {c: 0 for c in "abcdefghijklmnopqrstuvwxyz"}
            power += 1
        elif s_indexes[t_c][s_pointer[t_c]] <= last_index:
            for i, index in enumerate(s_indexes[t_c]):
                if last_index < index:
                    s_pointer[t_c] = i
                    break
            else:
                # reset
                s_pointer = {c: 0 for c in "abcdefghijklmnopqrstuvwxyz"}
                power += 1

        last_index = s_indexes[t_c][s_pointer[t_c]]
        s_pointer[t_c] += 1

    print(len(s) * power + last_index + 1)


main()


Fixed submission:
def main():
    s = input()
    t = input()

    if not set(t) <= set(s):
        print(-1)
        return

    s_indexes = {c: [] for c in "abcdefghijklmnopqrstuvwxyz"}
    for i, s_c in enumerate(s):
        s_indexes[s_c].append(i)
    s_pointer = {c: 0 for c in "abcdefghijklmnopqrstuvwxyz"}
    s_length = {c: len(s_indexes[c]) for c in "abcdefghijklmnopqrstuvwxyz"}

    power = 0
    last_index = -1
    for i, t_c in enumerate(t):
        if s_length[t_c] <= s_pointer[t_c]:
            # reset
            s_pointer = {c: 0 for c in "abcdefghijklmnopqrstuvwxyz"}
            power += 1
        elif s_indexes[t_c][s_pointer[t_c]] <= last_index:
            for j in range(s_pointer[t_c], s_length[t_c]):
                if last_index < s_indexes[t_c][j]:
                    s_pointer[t_c] = j
                    break
            else:
                # reset
                s_pointer = {c: 0 for c in "abcdefghijklmnopqrstuvwxyz"}
                power += 1

        last_index = s_indexes[t_c][s_pointer[t_c]]
        s_pointer[t_c] += 1

    print(len(s) * power + last_index + 1)


main()
Problem p02937 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using ll = long long;
using P = std::pair<ll, ll>;

#define rep(i, a, b) for (ll(i) = (a); i < (b); i++)
#define all(i) i.begin(), i.end()
#define debug(i) std::cout << i << "\n"

// const ll MOD = 998244353;
const ll MOD = 1e9 + 7;

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  // 問題文中の添え字が0-indexか1-indexか確認!

  std::string s, t;
  std::cin >> s >> t;
  bool flag = true;

  ll n = s.size(), m = t.size();

  std::vector<std::vector<ll>> itr(26);

  rep(i, 0, m) {
    ll num = t[i] - 'a';
    if (itr[num].empty()) {
      rep(j, 0, n) if (s[j] == t[i]) itr[num].push_back(j);
      if (itr[num].empty())
        flag = false;
    }
  }

  if (!flag) {
    std::cout << -1;
    return 0;
  }

  ll ans = itr[(ll)(t[0] - 'a')][0] + 1;

  ll now = ans - 1;
  rep(i, 1, m) {
    ll next = t[i] - 'a';
    auto len = std::upper_bound(all(itr[next]), now);
    if (len != itr[next].end()) {
      ans += (*len - now);
      now = *len;
    } else {
      ans += (n - now + itr[next][0]);
      now = itr[next][0];
    }

    // debug(now);
    // debug(ans);
  }

  std::cout << ans;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using ll = long long;
using P = std::pair<ll, ll>;

#define rep(i, a, b) for (ll(i) = (a); i < (b); i++)
#define all(i) i.begin(), i.end()
#define debug(i) std::cout << i << "\n"

// const ll MOD = 998244353;
const ll MOD = 1e9 + 7;

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  // 問題文中の添え字が0-indexか1-indexか確認!

  std::string s, t;
  std::cin >> s >> t;
  bool flag = true;

  ll n = s.size(), m = t.size();

  std::vector<std::vector<ll>> itr(26);

  rep(i, 0, 26) { rep(j, 0, n) if ((s[j] - 'a') == i) itr[i].push_back(j); }

  rep(i, 0, m) if (itr[t[i] - 'a'].empty()) flag = false;

  if (!flag) {
    std::cout << -1;
    return 0;
  }

  ll ans = itr[(ll)(t[0] - 'a')][0] + 1;

  ll now = ans - 1;
  rep(i, 1, m) {
    ll next = t[i] - 'a';
    auto len = std::upper_bound(all(itr[next]), now);
    if (len != itr[next].end()) {
      ans += (*len - now);
      now = *len;
    } else {
      ans += (n - now + itr[next][0]);
      now = itr[next][0];
    }

    // debug(now);
    // debug(ans);
  }

  std::cout << ans;

  return 0;
}
Problem p02937 (C++)

Buggy submission (status: Runtime Error):
//
// Created by yamunaku on 2019/08/19.
//

#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repl(i, l, r) for (int i = (l); i < (r); i++)
#define per(i, n) for (int i = ((n)-1); i >= 0; i--)
#define perl(i, l, r) for (int i = ((r)-1); i >= (l); i--)
#define all(x) (x).begin(), (x).end()
#define MOD9 998244353
#define MOD1 1000000007
#define IINF 1000000000
#define LINF 1000000000000000000
#define SP << " " <<
#define CYES cout << "Yes" << endl
#define CNO cout << "No" << endl
#define CFS                                                                    \
  cin.tie(0);                                                                  \
  ios::sync_with_stdio(false)
#define CST(x) cout << fixed << setprecision(x)

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vector<int>> mti;
typedef vector<ll> vl;
typedef vector<vector<ll>> mtl;

int main() {
  string s, t;
  cin >> s >> t;
  int n = s.size();
  int m = t.size();
  mti nx(n, vi(26, -1));
  vi a(26, -1);
  per(i, n) { a[s[i] - 'a'] = n + i; }
  per(i, n) {
    rep(j, 26) { nx[i][j] = a[j] - i; }
    a[s[i] - 'a'] = i;
  }
  ll now = n - 1;
  rep(i, m) { now += nx[now % n][t[i] - 'a']; }
  cout << now - (n - 1) << endl;
  return 0;
}


Fixed submission:
//
// Created by yamunaku on 2019/08/19.
//

#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repl(i, l, r) for (int i = (l); i < (r); i++)
#define per(i, n) for (int i = ((n)-1); i >= 0; i--)
#define perl(i, l, r) for (int i = ((r)-1); i >= (l); i--)
#define all(x) (x).begin(), (x).end()
#define MOD9 998244353
#define MOD1 1000000007
#define IINF 1000000000
#define LINF 1000000000000000000
#define SP << " " <<
#define CYES cout << "Yes" << endl
#define CNO cout << "No" << endl
#define CFS                                                                    \
  cin.tie(0);                                                                  \
  ios::sync_with_stdio(false)
#define CST(x) cout << fixed << setprecision(x)

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vector<int>> mti;
typedef vector<ll> vl;
typedef vector<vector<ll>> mtl;

int main() {
  string s, t;
  cin >> s >> t;
  int n = s.size();
  int m = t.size();
  mti nx(n, vi(26, -1));
  vi a(26, -1);
  per(i, n) { a[s[i] - 'a'] = n + i; }
  per(i, n) {
    rep(j, 26) { nx[i][j] = a[j] - i; }
    a[s[i] - 'a'] = i;
  }
  ll now = n - 1;
  rep(i, m) {
    if (nx[now % n][t[i] - 'a'] < 0) {
      cout << -1 << endl;
      return 0;
    }
    now += nx[now % n][t[i] - 'a'];
  }
  cout << now - (n - 1) << endl;
  return 0;
}
Problem p02937 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("Ofast")
#pragma GCC target("avx")
#include <bits/stdc++.h>
constexpr int INF = 2147483647;
constexpr long long int INF_LL = 9223372036854775807;
constexpr int MOD = 1000000007;
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

// contest
// sentence
// s e nte n ce
// 7*4 + 5
//
//  c ontest
int main() {
  string s, t;
  cin >> s >> t;
  vector<vector<int>> pos(26, vector<int>());
  for (int i = 0; i < s.size(); i++) {
    pos[s[i] - 'a'].push_back(i + 1);
  }
  int fpos = -1;
  ll ans = 0;
  for (int i = 0; i < t.size(); i++) {
    auto a = upper_bound(pos[t[i] - 'a'].begin(), pos[t[i] - 'a'].end(), fpos);
    if (fpos == -1 && a == pos[t[i] - 'a'].end()) {
      cout << -1 << endl;
      return 0;
    } else {
      if (a == pos[t[i] - 'a'].end()) {
        fpos = -1;
        ans += s.size();
        a = upper_bound(pos[t[i] - 'a'].begin(), pos[t[i] - 'a'].end(), fpos);
        fpos = *a;
      } else {
        fpos = *a;
      }
      if (i == t.size() - 1) {
        ans += fpos;
      }
    }
  }
  cout << ans << endl;
}

Fixed submission:
#pragma GCC optimize("Ofast")
#pragma GCC target("avx")
#include <bits/stdc++.h>
constexpr int INF = 2147483647;
constexpr long long int INF_LL = 9223372036854775807;
constexpr int MOD = 1000000007;
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

// contest
// sentence
// s e nte n ce
// 7*4 + 5
//
//  c ontest
int main() {
  string s, t;
  cin >> s >> t;
  vector<vector<int>> pos(26, vector<int>());
  for (int i = 0; i < s.size(); i++) {
    pos[s[i] - 'a'].push_back(i + 1);
  }
  int fpos = -1;
  ll ans = 0;
  for (int i = 0; i < t.size(); i++) {
    auto a = upper_bound(pos[t[i] - 'a'].begin(), pos[t[i] - 'a'].end(), fpos);
    if (fpos == -1 && a == pos[t[i] - 'a'].end()) {
      cout << -1 << endl;
      return 0;
    } else {
      if (a == pos[t[i] - 'a'].end()) {
        fpos = -1;
        ans += s.size();
        a = upper_bound(pos[t[i] - 'a'].begin(), pos[t[i] - 'a'].end(), fpos);
        if (a == pos[t[i] - 'a'].end()) {
          cout << -1 << endl;
          return 0;
        }
        fpos = *a;
      } else {
        fpos = *a;
      }
      if (i == t.size() - 1) {
        ans += fpos;
      }
    }
  }
  cout << ans << endl;
}
Problem p02937 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define pb push_back
using namespace std;
typedef long long ll;

int main() {
  string s, t;
  cin >> s >> t;

  ll ans = -1;
  vector<vector<int>> a(26);
  rep(i, s.size()) { a[s[i] - 'a'].pb(i); }
  /*  vector<vector<int>> b(t.size());
    rep(i, t.size()){
        for (auto x : a[t[i] - 'a']){
            b[i].pb(x);
        }
        if (b[i].empty()){
            cout << ans << endl;
            return 0;
        }
    }*/
  ll now = a[t[0] - 'a'][0];
  int res = 0;
  rep(i, t.size() - 1) {
    auto it =
        upper_bound(a[t[i + 1] - 'a'].begin(), a[t[i + 1] - 'a'].end(), now);
    if (it - a[t[i + 1] - 'a'].begin() == a[t[i + 1] - 'a'].size()) {
      now = a[t[i + 1] - 'a'][0];
      res++;
    } else {
      now = *it;
    }
  }
  ans = (ll)res * s.size() + now + 1;
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define pb push_back
using namespace std;
typedef long long ll;

int main() {
  string s, t;
  cin >> s >> t;

  ll ans = -1;
  vector<vector<int>> a(26);
  rep(i, s.size()) { a[s[i] - 'a'].pb(i); }

  rep(i, t.size()) {
    if (a[t[i] - 'a'].empty()) {
      cout << ans << endl;
      return 0;
    }
  }
  ll now = a[t[0] - 'a'][0];
  int res = 0;
  rep(i, t.size() - 1) {
    auto it =
        upper_bound(a[t[i + 1] - 'a'].begin(), a[t[i + 1] - 'a'].end(), now);
    if (it - a[t[i + 1] - 'a'].begin() == a[t[i + 1] - 'a'].size()) {
      now = a[t[i + 1] - 'a'][0];
      res++;
    } else {
      now = *it;
    }
  }
  ans = (ll)res * s.size() + now + 1;
  cout << ans << endl;
}
Problem p02937 (C++)

Buggy submission (status: Runtime Error):
/*
[abc138] E - Strings of Impurity
*/

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
#define ALL(c) (c).begin(), (c).end()

const int MAX_S = 1e5;
const int INF = 1e9;

string s, t;
int d['z' - 'a' + 1][MAX_S];

ll solve() {
  int sl = s.length();
  string ss = s + s;

  for (char c = 'a'; c <= 'z'; c++) {
    for (int i = 0; i < sl;) {
      size_t idx = ss.find(c, i);
      if (idx == string::npos) {
        fill(d[c - 'a'], d[c - 'a'] + sl, INF);
        break;
      }
      for (; i <= (int)idx; i++) {
        d[c - 'a'][i] = idx - i;
      }
    }
  }

  ll ans = 0;
  for (char c : t) {
    int dd = d[c - 'a'][ans % sl];
    if (dd >= INF) {
      return -1;
    }
    ans += dd + 1;
  }

  return ans;
}

int main() {
  cin >> s;
  cin >> t;

  cout << solve() << endl;

  return 0;
}


Fixed submission:
/*
[abc138] E - Strings of Impurity
*/

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
#define ALL(c) (c).begin(), (c).end()

const int MAX_S = 1e5;
const int INF = 1e9;

string s, t;
int d['z' - 'a' + 1][MAX_S];

ll solve() {
  int sl = s.length();
  string ss = s + s;

  for (char c = 'a'; c <= 'z'; c++) {
    for (int i = 0; i < sl;) {
      size_t idx = ss.find(c, i);
      if (idx == string::npos) {
        fill(d[c - 'a'], d[c - 'a'] + sl, INF);
        break;
      }
      for (; i < sl && i <= (int)idx; i++) {
        d[c - 'a'][i] = idx - i;
      }
    }
  }

  ll ans = 0;
  for (char c : t) {
    int dd = d[c - 'a'][ans % sl];
    if (dd >= INF) {
      return -1;
    }
    ans += dd + 1;
  }

  return ans;
}

int main() {
  cin >> s;
  cin >> t;

  cout << solve() << endl;

  return 0;
}
Problem p02937 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
typedef long double ld;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  string s, t;
  cin >> s >> t;
  unordered_map<char, vector<int>> s_mp;
  REP(i, s.size()) s_mp[s[i]].push_back(i + 1);
  ll cnt = 0;
  int pos = 0;
  REP(i, t.size()) {
    if (s_mp[t[i]].size() == 0) {
      cout << -1 << '\n';
      return 0;
    }
    bool flag = true;
    for (auto p : s_mp[t[i]]) {
      if (pos < p) {
        pos = p;
        flag = false;
        break;
      }
    }
    if (flag) {
      cnt += (ll)s.size();
      pos = s_mp[t[i]][0];
    }
    // cout << cnt << " " << pos << '\n';
  }
  cout << cnt + (ll)pos << '\n';
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
typedef long double ld;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  string s, t;
  cin >> s >> t;
  unordered_map<char, vector<int>> s_mp;
  REP(i, s.size()) s_mp[s[i]].push_back(i + 1);
  ll cnt = 0;
  int pos = 0;
  REP(i, t.size()) {
    if (s_mp[t[i]].size() == 0) {
      cout << -1 << '\n';
      return 0;
    }
    bool flag = true;
    auto itr = upper_bound(ALL(s_mp[t[i]]), pos);
    if (itr != s_mp[t[i]].end()) {
      pos = *itr;
    } else {
      cnt += (ll)s.size();
      pos = s_mp[t[i]][0];
    }
    // cout << cnt << " " << pos << '\n';
  }
  cout << cnt + (ll)pos << '\n';
  return 0;
}
Problem p02937 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
const ll mod = 1000000007;

int main() {
  string s, t;
  cin >> s >> t;
  map<char, vector<ll>> mps;
  for (ll i = 0; i < s.size(); i++)
    mps[s[i]].push_back(i + 1), mps[s[i]].push_back(i + 1 + s.size());
  for (char s = 'a'; s <= 'z'; s++) {
    auto &v = mps[s];
    sort(v.begin(), v.end());
  }
  ll cnt = 0;
  ll freq = 0;
  for (ll i = 0; i < t.size(); i++) {
    if (mps[t[i]].empty()) {
      printf("%d\n", -1);
      return 0;
    } else {
      auto q = mps[t[i]];
      ll tmp = *upper_bound(q.begin(), q.end(), cnt);
      if (tmp < s.size())
        cnt = tmp;
      else
        freq++, cnt = tmp - s.size();
    }
  }
  printf("%lld\n", freq * s.size() + cnt);
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
const ll mod = 1000000007;

int main() {
  string s, t;
  cin >> s >> t;
  map<char, vector<ll>> mps;
  for (ll i = 0; i < s.size(); i++)
    mps[s[i]].push_back(i + 1), mps[s[i]].push_back(i + 1 + s.size());
  for (char s = 'a'; s <= 'z'; s++) {
    auto &v = mps[s];
    sort(v.begin(), v.end());
  }
  ll cnt = 0;
  ll freq = 0;
  for (ll i = 0; i < t.size(); i++) {
    if (mps[t[i]].empty()) {
      printf("%d\n", -1);
      return 0;
    } else {
      ll tmp = *upper_bound(mps[t[i]].begin(), mps[t[i]].end(), cnt);
      if (tmp < s.size())
        cnt = tmp;
      else
        freq++, cnt = tmp - s.size();
    }
  }
  printf("%lld\n", freq * s.size() + cnt);
}
Problem p02937 (C++)

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

using namespace std;

#define int long long
#define ALL(x) (x).begin(), (x).end()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))

typedef pair<int, int> PI;
typedef pair<int, pair<int, int>> PII;
static const int INF = 1010000000000000017LL;
static const double eps = 1e-12;
static const double pi = 3.14159265358979323846;
static const int dx[4] = {1, -1, 0, 0};
static const int dy[4] = {0, 0, 1, -1};
static const int ddx[8] = {1, -1, 0, 0, 1, 1, -1, -1};
static const int ddy[8] = {0, 0, 1, -1, 1, -1, 1, -1};

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

string s, t;
vector<int> v[25];

signed main() {
  cin >> s >> t;
  for (int i = 0; i < s.size(); ++i)
    v[s[i] - 'a'].push_back(i);
  int front = -1;
  int k = 0;
  int cnt = 0;
  while (k < t.size()) {
    int c = t[k] - 'a';
    if (v[c].size() == 0) {
      cout << -1 << endl;
      return 0;
    }
    int idx = lower_bound(v[c].begin(), v[c].end(), front + 1) - v[c].begin();
    if (idx == v[c].size()) {
      cnt++;
      front = -1;
    } else {
      k++;
      front = v[c][idx];
    }
  }
  cout << cnt * s.size() + front + 1 << endl;
}

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

using namespace std;

#define int long long
#define ALL(x) (x).begin(), (x).end()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))

typedef pair<int, int> PI;
typedef pair<int, pair<int, int>> PII;
static const int INF = 1010000000000000017LL;
static const double eps = 1e-12;
static const double pi = 3.14159265358979323846;
static const int dx[4] = {1, -1, 0, 0};
static const int dy[4] = {0, 0, 1, -1};
static const int ddx[8] = {1, -1, 0, 0, 1, 1, -1, -1};
static const int ddy[8] = {0, 0, 1, -1, 1, -1, 1, -1};

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

string s, t;
vector<int> v[30];

signed main() {
  cin >> s >> t;
  for (int i = 0; i < s.size(); ++i)
    v[s[i] - 'a'].push_back(i);
  int front = -1;
  int k = 0;
  int cnt = 0;
  while (k < t.size()) {
    int c = t[k] - 'a';
    if (v[c].size() == 0) {
      cout << -1 << endl;
      return 0;
    }
    int idx = lower_bound(v[c].begin(), v[c].end(), front + 1) - v[c].begin();
    if (idx == v[c].size()) {
      cnt++;
      front = -1;
    } else {
      k++;
      front = v[c][idx];
    }
  }
  cout << cnt * s.size() + front + 1 << endl;
}
Problem p02937 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define MN 400005
char ch[MN], ch2[MN];
int nex[MN][26];
long long ans;
int main() {
  scanf("%s%s", ch + 1, ch2);
  long long l1 = strlen(ch + 1), l2 = strlen(ch2);
  for (int i = 0; i < 26; ++i)
    nex[(l1 << 1)][i] = -1;
  for (int i = (l1 << 1); i; --i) {
    for (int j = 0; j < 26; ++j) {
      nex[i - 1][j] = nex[i][j];
    }
    nex[i - 1][ch[i % l1 == 0 ? i : (i % l1)] - 'a'] = i;
  }
  int now = 0;
  for (int i = 0; i < l2; ++i) {
    if (nex[now][ch2[i] - 'a'] == -1) {
      puts("-1");
      return 0;
    }
    now = nex[now][ch2[i] - 'a'];
    //	cout<<"HI "<<i<<" "<<now<<endl;
    if (now >= l1)
      ans += l1, now -= l1;
  }
  printf("%lld", ans + (long long)now);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define MN 400005
char ch[MN], ch2[MN];
int nex[MN][26];
long long ans;
int main() {
  scanf("%s%s", ch + 1, ch2);
  long long l1 = strlen(ch + 1), l2 = strlen(ch2);
  for (int i = 0; i < 26; ++i)
    nex[(l1 << 1)][i] = -1;
  for (int i = (l1 << 1); i; --i) {
    for (int j = 0; j < 26; ++j) {
      nex[i - 1][j] = nex[i][j];
    }
    int real = i;
    if (i > l1)
      real -= l1;
    nex[i - 1][ch[real] - 'a'] = i;
  }
  int now = 0;
  for (int i = 0; i < l2; ++i) {
    if (nex[now][ch2[i] - 'a'] == -1) {
      puts("-1");
      return 0;
    }
    now = nex[now][ch2[i] - 'a'];
    //	cout<<"HI "<<i<<" "<<now<<endl;
    if (now >= l1)
      ans += l1, now -= l1;
  }
  printf("%lld", ans + (long long)now);
}
Problem p02937 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using ll = long long;

using namespace std;

int main() {
  string s, t;
  cin >> s >> t;
  string sample = s + s;
  ll pos[s.size()][26];
  for (int yi = 0; yi < s.size(); ++yi) {
    for (int xi = 0; xi < 26; ++xi) {
      pos[yi][xi] = -1;
    }
  }
  vector<queue<ll>> a(26);
  for (int i = 0; i < sample.length(); i++) {
    a[sample[i] - 'a'].push(i);
  }
  for (int i = 0; i < 26; ++i) {
    ll currentPos = 0;
    while (!a[i].empty()) {
      ll tmp = a[i].front();
      a[i].pop();
      for (int j = currentPos; j <= tmp; j++) {
        if (j == s.size()) {
          break;
        }
        pos[j][i] = tmp;
      }
      currentPos = tmp + 1;
    }
  }
  for (int xi = 0; xi < 26; ++xi) {
    for (int yi = 0; yi < s.size(); yi++) {
      // cout << pos[yi][xi] << " ";
    }
    // cout << endl;
  }
  ll count = 0;
  for (int i = 0; i < t.size(); ++i) {
    int current = t[i] - 'a';
    ll m = count % s.size();
    ll next = pos[m][current];
    if (next == -1) {
      cout << -1 << endl;
      return 0;
    }
    count += next - m + 1;
  }
  cout << count << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using ll = long long;

using namespace std;

int main() {
  string s, t;
  cin >> s >> t;
  string sample = s + s;
  ll pos[s.size()][26];
  for (int yi = 0; yi < s.size(); ++yi) {
    for (int xi = 0; xi < 26; ++xi) {
      pos[yi][xi] = -1;
    }
  }
  vector<queue<ll>> a(26);
  for (int i = 0; i < sample.length(); i++) {
    a[sample[i] - 'a'].push(i);
  }
  for (int i = 0; i < 26; ++i) {
    ll currentPos = 0;
    while (!a[i].empty()) {
      ll tmp = a[i].front();
      a[i].pop();
      for (int j = currentPos; j <= tmp; j++) {
        if (j >= s.size()) {
          break;
        }
        pos[j][i] = tmp;
      }
      currentPos = tmp + 1;
    }
  }
  for (int xi = 0; xi < 26; ++xi) {
    for (int yi = 0; yi < s.size(); yi++) {
      // cout << pos[yi][xi] << " ";
    }
    // cout << endl;
  }
  ll count = 0;
  for (int i = 0; i < t.size(); ++i) {
    int current = t[i] - 'a';
    ll m = count % s.size();
    ll next = pos[m][current];
    if (next == -1) {
      cout << -1 << endl;
      return 0;
    }
    count += next - m + 1;
  }
  cout << count << endl;
}
Problem p02937 (C++)

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

using namespace std;

#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define REPO(i, n) for (ll i = 1; i <= n; i++)
#define ll long long
#define INF (ll)1 << 60
#define MINF (-1 * INF)
#define ALL(n) n.begin(), n.end()
#define MOD (ll)1000000007
#define P pair<ll, ll>

string s, t;
bool used[26], used2[26];
ll nx[110000][26];
int main() {
  cin >> s >> t;
  ll n = s.size();

  //-1判定
  REP(i, s.size()) used[s[i] - 'a'] = true;
  REP(i, t.size()) used2[t[i] - 'a'] = true;
  REP(i, 26) {
    if (used2[i] and !used[i]) {
      cout << -1 << endl;
      return 0;
    }
  }
  s = s + s;
  REPR(i, s.size() - 1) {
    REP(j, 26) {
      nx[i][j] = nx[i + 1][j];
      if (i != s.size() - 1) {
        nx[i][s[i + 1] - 'a'] = i + 1;
      }
    }
  }
  ll ans = 0, now = n - 1;
  REP(i, t.size()) {
    ans += nx[now][t[i] - 'a'] - now;
    now = nx[now][t[i] - 'a'] % n;
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define REPO(i, n) for (ll i = 1; i <= n; i++)
#define ll long long
#define INF (ll)1 << 60
#define MINF (-1 * INF)
#define ALL(n) n.begin(), n.end()
#define MOD (ll)1000000007
#define P pair<ll, ll>

string s, t;
bool used[26], used2[26];
ll nx[210000][26];
int main() {
  cin >> s >> t;
  ll n = s.size();

  //-1判定
  REP(i, s.size()) used[s[i] - 'a'] = true;
  REP(i, t.size()) used2[t[i] - 'a'] = true;
  REP(i, 26) {
    if (used2[i] and !used[i]) {
      cout << -1 << endl;
      return 0;
    }
  }
  s = s + s;
  REPR(i, s.size() - 1) {
    REP(j, 26) {
      nx[i][j] = nx[i + 1][j];
      if (i != s.size() - 1) {
        nx[i][s[i + 1] - 'a'] = i + 1;
      }
    }
  }
  ll ans = 0, now = n - 1;
  REP(i, t.size()) {
    ans += nx[now][t[i] - 'a'] - now;
    now = nx[now][t[i] - 'a'] % n;
  }
  cout << ans << endl;
}
Problem p02937 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using P = pair<char, int>;

vector<int> c2p[10000];
string s, t;

int main() {
  cin >> s >> t;
  int sz = s.size();
  for (int i = 0; i < sz; i++) {
    c2p[s[i]].push_back(i);
  }

  long long int cnt = 0;
  int prep = -1;
  for (auto c : t) {
    if (c2p[c].size() == 0) {
      cout << -1 << endl;
      return 0;
    }

    auto cand = c2p[c];
    auto pos = upper_bound(cand.begin(), cand.end(), prep);
    if (pos == cand.end())
      pos = cand.begin();

    if (prep >= *pos)
      cnt++;
    prep = *pos;
  }
  cout << cnt * s.size() + prep + 1 << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using P = pair<char, int>;

vector<int> c2p[10000];
string s, t;

int main() {
  cin >> s >> t;
  int sz = s.size();
  for (int i = 0; i < sz; i++) {
    c2p[s[i]].push_back(i);
  }

  long long int cnt = 0;
  int prep = -1;
  for (auto c : t) {
    if (c2p[c].size() == 0) {
      cout << -1 << endl;
      return 0;
    }

    auto &cand = c2p[c];
    auto pos = upper_bound(cand.begin(), cand.end(), prep);
    if (pos == cand.end())
      pos = cand.begin();

    if (prep >= *pos)
      cnt++;
    prep = *pos;
  }
  cout << cnt * s.size() + prep + 1 << endl;
}
Problem p02937 (C++)

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

void solve(string s, string t) {
  int sn = s.size(), tn = t.size();
  vector<vector<int>> positions(26);
  for (int i = 0; i < sn; i++) {
    positions.at(s.at(i) - 'a').push_back(i);
  }
  for (int i = 0; i < sn; i++) {
    positions.at(s.at(i) - 'a').push_back(sn + i);
  }

  for (int i = 0; i < tn; i++) {
    if (positions.at(t.at(i) - 'a').size() == 0) {
      cout << -1 << endl;
      return;
    }
  }

  int64_t res = 0, p = 0;
  for (int i = 0; i < tn; i++) {
    vector<int> pos_ti = positions.at(t.at(i) - 'a');
    int p_n = *lower_bound(pos_ti.begin(), pos_ti.end(), p) + 1;
    if (p_n >= sn) {
      res += sn;
      p_n -= sn;
    }
    p = p_n;
  }
  res += p;
  cout << res << endl;
}

int main() {
  string s, t;
  cin >> s >> t;
  solve(move(s), move(t));
  return 0;
}


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

void solve(string s, string t) {
  int sn = s.size(), tn = t.size();
  vector<vector<int>> positions(26);
  for (int i = 0; i < sn; i++) {
    positions.at(s.at(i) - 'a').push_back(i);
  }
  for (int i = 0; i < sn; i++) {
    positions.at(s.at(i) - 'a').push_back(sn + i);
  }

  for (int i = 0; i < tn; i++) {
    if (positions.at(t.at(i) - 'a').size() == 0) {
      cout << -1 << endl;
      return;
    }
  }

  int64_t res = 0, p = 0;
  for (int i = 0; i < tn; i++) {
    int p_n = *lower_bound(positions.at(t.at(i) - 'a').begin(),
                           positions.at(t.at(i) - 'a').end(), p) +
              1;
    if (p_n >= sn) {
      res += sn;
      p_n -= sn;
    }
    p = p_n;
  }
  res += p;
  cout << res << endl;
}

int main() {
  string s, t;
  cin >> s >> t;
  solve(move(s), move(t));
  return 0;
}
Problem p02937 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1LL << 62
#define inf 1000000007

vector<vector<ll>> a(26);
int main() {
  string s, t;
  cin >> s >> t;
  for (int i = 0; i < s.size(); i++) {
    ll now = s[i] - 'a';
    a[now].push_back(i + 1);
  }
  ll ans = 0, now = 0;
  bool out = false;
  for (int i = 0; i < t.size(); i++) {
    ll sear = t[i] - 'a';
    bool ch = false;
    for (int j = 0; j < a[sear].size(); j++) {
      ll fi = a[sear][j];
      if (fi > now) {
        ans += fi - now;
        now = fi;
        ch = true;
        out = false;
        break;
      }
    }
    if (out) {
      cout << -1;
      return 0;
    }
    if (ch != true) {
      i--;
      ans += s.size() - now;
      now = 0;
      out = true;
    }
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1LL << 62
#define inf 1000000007

vector<vector<ll>> a(26);
int main() {
  string s, t;
  cin >> s >> t;
  for (int i = 0; i < s.size(); i++) {
    ll now = s[i] - 'a';
    a[now].push_back(i + 1);
  }
  ll ans = 0, now = 0;
  bool out = false;
  for (int i = 0; i < t.size(); i++) {
    ll sear = t[i] - 'a';
    bool ch = false;
    auto Iter1 = upper_bound(a.at(sear).begin(), a.at(sear).end(), now);
    ll res = Iter1 - a[sear].begin();
    if (res != a[sear].size()) {
      ll fi = a[sear][res];
      ans += fi - now;
      now = fi;
      ch = true;
      out = false;
    }
    if (out) {
      cout << -1;
      return 0;
    }
    if (ch != true) {
      i--;
      ans += s.size() - now;
      now = 0;
      out = true;
    }
  }
  cout << ans;
  return 0;
}
Problem p02938 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
int main() {
  long long l, r;
  cin >> l >> r;
  long long ans = 0;
  // x ⊂ y
  // (0, 0), (0, 1), (1, 1)
  for (int k = 0; k < 62; k++) {
    long long c = 1LL << k;
    long long d = 1LL << (k + 1);
    if (l >= d || c > r)
      continue;
    long long dp[k + 1][4];
    // 0: l < curr && r > curr, 1: l <= curr, 2: r >= curr
    memset(dp, 0, sizeof(dp));
    int s = 0;
    if (c <= l && l < d)
      s += 1 << 0;
    if (c <= r && r < d)
      s += 1 << 1;
    dp[k][s]++;
    long long prev = ans;
    for (int i = k - 1; i >= 0; i--) {
      if (i < 0)
        continue;
      bool lb = l & (1LL << i);
      bool rb = r & (1LL << i);

      // dp[i+1][0]
      dp[i][0] += dp[i + 1][0] * 3 % mod;

      // dp[i+1][1]
      if (lb) {
        dp[i][1] += dp[i + 1][1];
      } else {
        dp[i][0] += dp[i + 1][1];
        dp[i][1] += dp[i + 1][1] * 2 % mod;
      }

      // dp[i+1][2]
      if (rb) {
        dp[i][2] += dp[i + 1][2] * 2 % mod;
        dp[i][0] += dp[i + 1][2];
      } else {
        dp[i][2] += dp[i + 1][2];
      }

      // dp[i+1][3]
      if (lb == rb) {
        dp[i][3] += dp[i + 1][3];
      } else {
        if (dp[i + 1][3])
          assert(!lb);
        // lb 0, rb 1
        dp[i][1] += dp[i + 1][3];
        dp[i][2] += dp[i + 1][3];
        dp[i][3] += dp[i + 1][3];
      }
    }

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

  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
int main() {
  long long l, r;
  cin >> l >> r;
  long long ans = 0;
  // x ⊂ y
  // (0, 0), (0, 1), (1, 1)
  for (int k = 0; k < 62; k++) {
    long long c = 1LL << k;
    long long d = 1LL << (k + 1);
    if (l >= d || c > r)
      continue;
    long long dp[k + 1][4];
    // 0: l < curr && r > curr, 1: l <= curr, 2: r >= curr
    memset(dp, 0, sizeof(dp));
    int s = 0;
    if (c <= l && l < d)
      s += 1 << 0;
    if (c <= r && r < d)
      s += 1 << 1;
    dp[k][s]++;
    long long prev = ans;
    for (int i = k - 1; i >= 0; i--) {
      if (i < 0)
        continue;
      bool lb = l & (1LL << i);
      bool rb = r & (1LL << i);

      // dp[i+1][0]
      dp[i][0] += dp[i + 1][0] * 3 % mod;

      // dp[i+1][1]
      if (lb) {
        dp[i][1] += dp[i + 1][1];
      } else {
        dp[i][0] += dp[i + 1][1];
        dp[i][1] += dp[i + 1][1] * 2 % mod;
      }

      // dp[i+1][2]
      if (rb) {
        dp[i][2] += dp[i + 1][2] * 2 % mod;
        dp[i][0] += dp[i + 1][2];
      } else {
        dp[i][2] += dp[i + 1][2];
      }

      // dp[i+1][3]
      if (lb == rb) {
        dp[i][3] += dp[i + 1][3];
      } else if (rb) {
        dp[i][1] += dp[i + 1][3];
        dp[i][2] += dp[i + 1][3];
        dp[i][3] += dp[i + 1][3];
      }
    }

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

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

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
char s[100010];
int main() {
  scanf("%s", s + 1);
  int L = strlen(s + 1), ans = 0;
  for (int i = 1, j; i <= L; ++i) {
    for (j = i; s[j] == s[j + 1]; ++j)
      ;
    int l = j - i + 1;
    switch (l % 3) {
    case 0:
      ans += l / 3 * 2;
      break;
    case 1:
      ans += l / 3 * 2 + 1;
      break;
    case 2:
      ans += l / 3 * 2 + 2, ++j;
      break;
    }
    j > L &&l % 3 == 2 && (--ans);
    i = j;
  }
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstring>
char s[200010];
int main() {
  scanf("%s", s + 1);
  int L = strlen(s + 1), ans = 0;
  for (int i = 1, j; i <= L; ++i) {
    for (j = i; s[j] == s[j + 1]; ++j)
      ;
    int l = j - i + 1;
    switch (l % 3) {
    case 0:
      ans += l / 3 * 2;
      break;
    case 1:
      ans += l / 3 * 2 + 1;
      break;
    case 2:
      ans += l / 3 * 2 + 2, ++j;
      break;
    }
    j > L &&l % 3 == 2 && (--ans);
    i = j;
  }
  printf("%d\n", ans);
  return 0;
}
Problem p02939 (C++)

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

using ll = long long int;

const int MAX = (int)(1e5 + 5);
const ll INF = (ll)(1e10 + 5);

string s;

map<pair<int, string>, int> memo;

int ans;

int solve(int pos, string last) {
  auto key = make_pair(pos, last);
  if (memo.find(key) != memo.end())
    return memo[key];

  if (pos == s.size())
    return memo[key] = 0;

  int ret = -1;

  for (int i = 1; (pos + i) <= s.size(); ++i) {
    int next_pos = pos + i;

    if (last == s.substr(pos, i))
      continue;

    ret = max(ret, solve(next_pos, s.substr(pos, i)));
  }

  ret += (ret == -1) ? 0 : 1;

  return memo[key] = ret;
}

int main(void) {
  // Here your code !
  cin >> s;

  // int pos = 0;

  // for (int i = 1; (pos + i) <= s.size(); ++i) {
  //   int next_pos = pos + i;

  //   printf("%d\n", next_pos);

  //   cout << s.substr(pos, i) << endl;
  // }

  set<string> occur;

  ans = solve(0, "");

  printf("%d\n", ans);

  return 0;
}


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

using ll = long long int;

const int MAX = (int)(1e5 + 5);
const ll INF = (ll)(1e10 + 5);

string s;

map<pair<int, string>, int> memo;

int ans;

int solve(int pos, string last) {
  auto key = make_pair(pos, last);
  if (memo.find(key) != memo.end())
    return memo[key];

  if (pos == s.size())
    return memo[key] = 0;

  int ret = -1;

  for (int i = 1; (pos + i) <= min((int)(s.size()), pos + 2); ++i) {
    int next_pos = pos + i;

    if (last == s.substr(pos, i))
      continue;

    ret = max(ret, solve(next_pos, s.substr(pos, i)));
  }

  ret += (ret == -1) ? 0 : 1;

  return memo[key] = ret;
}

int main(void) {
  // Here your code !
  cin >> s;

  // int pos = 0;

  // for (int i = 1; (pos + i) <= s.size(); ++i) {
  //   int next_pos = pos + i;

  //   printf("%d\n", next_pos);

  //   cout << s.substr(pos, i) << endl;
  // }

  set<string> occur;

  ans = solve(0, "");

  printf("%d\n", ans);

  return 0;
}
Problem p02939 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fio()                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)
#define int long long
#define MOD 998244353
using namespace std;
const int N = 300005;

string s;
int dp[100001][2];
int solve(int i, int b) {

  int j = b;
  if (i == s.size())
    return 0;

  if (dp[i][b] != -1)
    return dp[i][b];

  if (b == 0) {
    dp[i][j] = 0;
    if (i != s.size() - 1)
      dp[i][j] = 1 + solve(i + 2, 1);
    if (s[i] != s[i - 1])
      dp[i][j] = max(dp[i][j], 1 + solve(i + 1, 0));
  }

  if (b == 1)
    dp[i][j] = 1 + solve(i + 1, 0);

  // cout << i << " " << b << " " << dp[i][b] << endl;
  return dp[i][j];
}
int32_t main() {
  fio();
  cin >> s;
  if (s.size() == 1)
    return 1;
  for (int i = 0; i < s.size(); i++)
    dp[i][0] = -1, dp[i][1] = -1;
  cout << 1 + max(solve(1, 0), solve(2, 1));
}

Fixed submission:
#include <bits/stdc++.h>
#define fio()                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)
#define int long long
#define MOD 998244353
using namespace std;
const int N = 300005;

string s;
int dp[200006][2];
int solve(int i, int b) {

  int j = b;
  if (i == s.size())
    return 0;

  if (dp[i][b] != -1)
    return dp[i][b];

  if (b == 0) {
    dp[i][j] = 0;
    if (i != s.size() - 1)
      dp[i][j] = 1 + solve(i + 2, 1);
    if (s[i] != s[i - 1])
      dp[i][j] = max(dp[i][j], 1 + solve(i + 1, 0));
  }

  if (b == 1)
    dp[i][j] = 1 + solve(i + 1, 0);

  // cout << i << " " << b << " " << dp[i][b] << endl;
  return dp[i][j];
}
int32_t main() {
  fio();
  cin >> s;
  if (s.size() == 1)
    return 1;
  for (int i = 0; i < s.size(); i++)
    dp[i][0] = -1, dp[i][1] = -1;
  cout << 1 + max(solve(1, 0), solve(2, 1));
}
Problem p02939 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <map>
#include <math.h>
#include <string>
#include <vector>
using namespace std;

#define rep(i, o) for (int i = 0; i < (o); i++)
#define REP(i, a, b) for (int i = (int)(a); (i) < (int)(b); i++)
#define NUM 1e5

typedef long long ll;
typedef unsigned long long ull;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll c, ll d) { return c / gcd(c, d) * d; }

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

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

  int dp[100010][3];
  memset(dp, 0, sizeof(dp));
  /* dp[i][j] で
  1.i文字目まで見たか,
  2.前の文字の長さが j 文字の時  を表現*/
  dp[1][0] = dp[2][1] = 1;
  rep(i, s.size() + 1) {
    if (i >= 1 && s[i - 1] != s[i]) {
      chmax(dp[i + 1][1], dp[i][1] + 1);
    }
    if (i >= 2 && (s[i - 2] != s[i] || s[i - 1] != s[i + 1])) {
      chmax(dp[i + 2][2], dp[i][2] + 1);
    }
    chmax(dp[i + 2][2], dp[i][1] + 1); // 1 -> 2
    chmax(dp[i + 1][1], dp[i][2] + 1); // 2 -> 1
                                       // rep(i, s.size() + 1) {
    // cout << dp[i][1] << " " << dp[i][2] << endl;
    // }
  }
  cout << max(dp[s.size()][1], dp[s.size()][2]) << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <map>
#include <math.h>
#include <string>
#include <vector>
using namespace std;

#define rep(i, o) for (int i = 0; i < (o); i++)
#define REP(i, a, b) for (int i = (int)(a); (i) < (int)(b); i++)
#define NUM 1e5

typedef long long ll;
typedef unsigned long long ull;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll c, ll d) { return c / gcd(c, d) * d; }

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

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

  int dp[s.size() + 1][3];
  memset(dp, 0, sizeof(dp));
  /* dp[i][j] で
  1.i文字目まで見たか,
  2.前の文字の長さが j 文字の時  を表現*/
  dp[1][0] = dp[2][1] = 1;
  rep(i, s.size() + 1) {
    if (i >= 1 && s[i - 1] != s[i]) {
      chmax(dp[i + 1][1], dp[i][1] + 1);
    }
    if (i >= 2 && (s[i - 2] != s[i] || s[i - 1] != s[i + 1])) {
      chmax(dp[i + 2][2], dp[i][2] + 1);
    }
    chmax(dp[i + 2][2], dp[i][1] + 1); // 1 -> 2
    chmax(dp[i + 1][1], dp[i][2] + 1); // 2 -> 1
                                       // rep(i, s.size() + 1) {
    // cout << dp[i][1] << " " << dp[i][2] << endl;
    // }
  }
  cout << max(dp[s.size()][1], dp[s.size()][2]) << endl;
  return 0;
}
Problem p02939 (C++)

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

using namespace std;

using ll = long long;

mt19937 mt(736);

int foo(const string &s, const string &last = "", int p = 0) {
  static map<pair<int, string>, int> dp;

  if (p == s.size())
    return 0;

  for (int i = p + 1; i <= s.size() && i < p + 4; i++)
    if (s.substr(p, i - p) != last)
      dp[{p, last}] = max(dp[{p, last}], 1 + foo(s, s.substr(p, i - p), i));

  return dp[{p, last}];
}

void solve(istream &cin = std::cin, ostream &cout = std::cout) {
  string s;

  cin >> s;

  cout << foo(s) << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  cout << fixed;

#ifdef LOCAL
  auto st = clock();

  ifstream fin("../input.txt");

  solve(fin);

  cout << "clock: " << setprecision(4)
       << (clock() - st) / (double)CLOCKS_PER_SEC << endl;
#else
  solve();
#endif

  return 0;
}


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

using namespace std;

using ll = long long;

mt19937 mt(736);

int foo(const string &s, const string &last = "", int p = 0) {
  static map<pair<int, string>, int> dp;

  if (p == s.size())
    return 0;

  if (!dp.count({p, last}))
    for (int i = p + 1; i <= s.size() && i < p + 4; i++)
      if (s.substr(p, i - p) != last)
        dp[{p, last}] = max(dp[{p, last}], 1 + foo(s, s.substr(p, i - p), i));

  return dp[{p, last}];
}

void solve(istream &cin = std::cin, ostream &cout = std::cout) {
  string s;

  cin >> s;

  cout << foo(s) << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  cout << fixed;

#ifdef LOCAL
  auto st = clock();

  ifstream fin("../input.txt");

  solve(fin);

  cout << "clock: " << setprecision(4)
       << (clock() - st) / (double)CLOCKS_PER_SEC << endl;
#else
  solve();
#endif

  return 0;
}