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

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

#define FOR(i, x, n) for (ll i = x; i < n; i++)
#define pb push_back
#define pf push_front
#define ll long long
#define hii cout << "hii" << endl
#define pii pair<int, int>
#define pll pair<ll, ll>
#define int ll
#define mpp make_pair
#define endl '\n'
#define ff first
#define ss second
#define vi vector<int>
#define all(s) s.begin(), s.end()
#define si size()

const int mod = 1e9 + 7;
const int N = 2e5 + 6;
int arr[N];
int DP[N][3];
string input;

int f(int idx, int cnt) {
  if (idx >= input.size())
    return 0;
  if (DP[idx][cnt] != -1)
    return DP[idx][cnt];
  if (idx == 0) {
    int ans1 = 1 + f(idx + 1, 1);
    int ans2 = 1 + f(idx + 2, 2);
    return DP[idx][cnt] = max(ans1, ans2);
  } else {
    if (cnt == 1) {
      int ans1 = -9999999999, ans2 = -9999999999;
      if (input[idx] != input[idx - 1]) {
        ans1 = 1 + f(idx + 1, 1);
        ans2 = 1 + f(idx + 2, 2);
      } else if (idx != input.si - 1) {
        ans2 = 1 + f(idx + 2, 2);
      }
      return DP[idx][cnt] = max(ans1, ans2);
    } else {
      int ans1 = 1 + f(idx + 1, 1);
      return DP[idx][cnt] = ans1;
    }
  }
}

void solve() {
  cin >> input;
  int n = input.size();
  memset(DP, -1, sizeof(DP));
  cout << f(0, 0);
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  solve();
  return 0;
}

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

#define FOR(i, x, n) for (ll i = x; i < n; i++)
#define pb push_back
#define pf push_front
#define ll long long
#define hii cout << "hii" << endl
#define pii pair<int, int>
#define pll pair<ll, ll>
#define int ll
#define mpp make_pair
#define endl '\n'
#define ff first
#define ss second
#define vi vector<int>
#define all(s) s.begin(), s.end()
#define si size()

const int mod = 1e9 + 7;
const int N = 2e5 + 6;
int arr[N];
int DP[N][3];
string input;

int f(int idx, int cnt) {
  if (idx >= input.size())
    return 0;
  if (DP[idx][cnt] != -1)
    return DP[idx][cnt];
  if (idx == 0) {
    int ans1 = 1 + f(idx + 1, 1);
    int ans2 = 1 + f(idx + 2, 2);
    return DP[idx][cnt] = max(ans1, ans2);
  } else {
    if (cnt == 1) {
      int ans1 = -9999999999, ans2 = -9999999999;
      if (input[idx] != input[idx - 1]) {
        ans1 = 1 + f(idx + 1, 1);
        ans2 = 1 + f(idx + 2, 2);
      } else if (idx != input.si - 1) {
        ans2 = 1 + f(idx + 2, 2);
      }
      return DP[idx][cnt] = max(ans1, ans2);
    } else {
      int ans1 = 1 + f(idx + 1, 1);
      return DP[idx][cnt] = ans1;
    }
  }
}

void solve() {
  cin >> input;
  int n = input.size();
  memset(DP, -1, sizeof(DP));
  cout << f(0, 0);
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  // #ifndef ONLINE_JUDGE
  //    freopen("input.txt", "r", stdin);
  //    freopen("output.txt", "w", stdout);
  // #endif
  solve();
  return 0;
}
Problem p02939 (C++)

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

typedef long long ll;
#define int ll
#define fi first
#define se second
#define SORT(a) sort(a.begin(), a.end())
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define MP(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define INF LLONG_MAX / 2
#define all(x) (x).begin(), (x).end()
#define debug(x) cerr << #x << ": " << x << endl
#define debug_vec(v)                                                           \
  cerr << #v << ":";                                                           \
  rep(i, v.size()) cerr << " " << v[i];                                        \
  cerr << endl
using namespace std;

int MOD = 998244353;
ll dp[20010][2] = {0};

signed main() {

  string s;
  cin >> s;

  ll n = s.size();

  dp[1][0] = 1;
  dp[1][1] = 0;
  for (int i = 2; i <= n; i++) {
    // 2コ使う
    if (i < 4 || (s[i - 1] == s[i - 3] && s[i - 2] == s[i - 4])) {
      dp[i][1] = dp[i - 2][0] + 1;
    } else {
      dp[i][1] = max(dp[i - 2][0], dp[i - 2][1]) + 1;
    }
    if (s[i - 1] == s[i - 2]) {
      dp[i][0] = dp[i - 1][1] + 1;
    } else {
      dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]) + 1;
    }
  }

  cout << max(dp[n][0], dp[n][1]) << endl;

  return 0;
}

// g++ -std=c++14 code1.cpp
// rm -r -f test;oj dl
// https://cf17-final-open.contest.atcoder.jp/tasks/cf17_final_a rm -r -f
// test;oj dl http://agc037.contest.atcoder.jp/tasks/agc037_a

Fixed submission:
#include "bits/stdc++.h"

typedef long long ll;
#define int ll
#define fi first
#define se second
#define SORT(a) sort(a.begin(), a.end())
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define MP(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define INF LLONG_MAX / 2
#define all(x) (x).begin(), (x).end()
#define debug(x) cerr << #x << ": " << x << endl
#define debug_vec(v)                                                           \
  cerr << #v << ":";                                                           \
  rep(i, v.size()) cerr << " " << v[i];                                        \
  cerr << endl
using namespace std;

int MOD = 998244353;
ll dp[200010][2] = {0};

signed main() {

  string s;
  cin >> s;

  ll n = s.size();

  dp[1][0] = 1;
  dp[1][1] = 0;
  for (int i = 2; i <= n; i++) {
    // 2コ使う
    if (i < 4 || (s[i - 1] == s[i - 3] && s[i - 2] == s[i - 4])) {
      dp[i][1] = dp[i - 2][0] + 1;
    } else {
      dp[i][1] = max(dp[i - 2][0], dp[i - 2][1]) + 1;
    }
    if (s[i - 1] == s[i - 2]) {
      dp[i][0] = dp[i - 1][1] + 1;
    } else {
      dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]) + 1;
    }
  }

  cout << max(dp[n][0], dp[n][1]) << endl;

  return 0;
}

// g++ -std=c++14 code1.cpp
// rm -r -f test;oj dl
// https://cf17-final-open.contest.atcoder.jp/tasks/cf17_final_a rm -r -f
// test;oj dl http://agc037.contest.atcoder.jp/tasks/agc037_a
Problem p02939 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define N 200005
#define task "t"
#define mu(x) x *x

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using piii = pair<int, pii>;
const int inf = 1e9 + 7;
const int base = 311;

string s;
ll Hash = 0, res = 0, Hash1 = -1;

int main() {
  ios::sync_with_stdio(false);
  cin.tie();
  cout.tie();
#ifndef ONLINE_JUDGE
  freopen(task ".inp", "r", stdin);
  freopen(task ".out", "w", stdout);
#endif // ONLINE_JUDGE
  cin >> s;
  for (int i = 0; i < s.length(); i++) {
    Hash = (Hash * base + s[i] - 'a' + 1) % inf;
    if (Hash != Hash1) {
      res++;
      Hash1 = Hash;
      Hash = 0;
    }
  }
  cout << res;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define N 200005
#define task "t"
#define mu(x) x *x

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using piii = pair<int, pii>;
const int inf = 1e9 + 7;
const int base = 311;

string s;
ll Hash = 0, res = 0, Hash1 = -1;

int main() {
  ios::sync_with_stdio(false);
  cin.tie();
  cout.tie();
#ifndef ONLINE_JUDGE
  // freopen(task".inp","r",stdin);
  // freopen(task".out","w",stdout);
#endif // ONLINE_JUDGE
  cin >> s;
  for (int i = 0; i < s.length(); i++) {
    Hash = (Hash * base + s[i] - 'a' + 1) % inf;
    if (Hash != Hash1) {
      res++;
      Hash1 = Hash;
      Hash = 0;
    }
  }
  cout << res;
  return 0;
}
Problem p02939 (C++)

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

using namespace std;

string s;
int f[maxn][3], n;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // freopen("in.txt","r",stdin);
  cin >> s;
  n = s.length();
  for (int i = 0; i < n; i++)
    for (int k = 0; k < 3; k++)
      f[i][k] = -1;
  f[0][0] = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < 3; j++) {
      if (f[i][j] == -1)
        continue;
      for (int k = 1; k < 3; k++) {
        if (i + k > n)
          continue;
        if (j != k || s.substr(i - j, j) != s.substr(i, k))
          f[i + k][k] = max(f[i + k][k], f[i][j] + 1);
      }
      // cout<<f[i][j]<<endl;
    }
  int ans = 0;
  for (int i = 0; i < 3; i++)
    ans = max(ans, f[n][i]);
  cout << ans;
}


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

using namespace std;

string s;
int f[maxn][3], n;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // freopen("in.txt","r",stdin);
  cin >> s;
  n = s.length();
  for (int i = 0; i < n; i++)
    for (int k = 0; k < 3; k++)
      f[i][k] = -1;
  f[0][0] = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < 3; j++) {
      if (f[i][j] == -1)
        continue;
      for (int k = 1; k < 3; k++) {
        if (i + k > n)
          continue;
        if (j != k || s.substr(i - j, j) != s.substr(i, k))
          f[i + k][k] = max(f[i + k][k], f[i][j] + 1);
      }
      // cout<<f[i][j]<<endl;
    }
  int ans = 0;
  for (int i = 0; i < 3; i++)
    ans = max(ans, f[n][i]);
  cout << ans;
}
Problem p02939 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long long ll;
typedef pair<ll, ll> P;
const ll mod = 1000000007;
const ll LINF = 1LL << 60;
const int INF = 1 << 30;

int main() {
  string S;
  cin >> S;
  int n = (int)(S.length());
  vector<int> dp(n);
  int i = 0;
  dp[0] = 0;
  int tmp = 1;
  while (i < n - 1) {
    // cout << i << endl;
    // for (int j = 0;j < n; j++){
    // 	cout << dp[j] << endl;
    // }
    if (tmp == 1) {
      if (S[i] != S[i + 1]) {
        dp[i + 1] = dp[i] + 1;
        i++;
        tmp = 1;
      } else {
        dp[i + 1] = dp[i];
        dp[i + 2] = dp[i] + 1;
        i += 2;
        tmp = 2;
      }
    } else {
      dp[i + 1] = dp[i] + 1;
      i++;
      tmp = 1;
    }
  }
  if (dp[n - 1] == 0) {
    cout << dp[n - 1] << endl;
  } else {
    cout << dp[n - 1] + 1 << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long long ll;
typedef pair<ll, ll> P;
const ll mod = 1000000007;
const ll LINF = 1LL << 60;
const int INF = 1 << 30;

int main() {
  string S;
  cin >> S;
  int n = (int)(S.length());
  vector<int> dp(n + 2);
  int i = 0;
  dp[0] = 0;
  int tmp = 1;
  while (i < n - 1) {
    // cout << i << endl;
    // for (int j = 0;j < n; j++){
    // 	cout << dp[j] << endl;
    // }
    if (tmp == 1) {
      if (S[i] != S[i + 1]) {
        dp[i + 1] = dp[i] + 1;
        i++;
        tmp = 1;
      } else {
        dp[i + 1] = dp[i];
        dp[i + 2] = dp[i] + 1;
        i += 2;
        tmp = 2;
      }
    } else {
      dp[i + 1] = dp[i] + 1;
      i++;
      tmp = 1;
    }
  }
  if (dp[n - 1] == 0) {
    cout << dp[n - 1] << endl;
  } else {
    cout << dp[n - 1] + 1 << endl;
  }
  return 0;
}
Problem p02939 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <vector>

#define repd(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) repd(i, 0, n)
typedef long long ll;

using namespace std;

int inputValue() {
  int a;
  cin >> a;
  return a;
};

void inputArray(int *p, int a){rep(i, a){cin >> p[i];
}
}
;

void inputVector(vector<int> *p, int a) {
  rep(i, a) {
    int input;
    cin >> input;
    p->push_back(input);
  }
}

template <typename T> void output(T a, int precision) {
  if (precision > 0) {
    cout << setprecision(precision) << a << "\n";
  } else {
    cout << a << "\n";
  }
}

int main(int argc, const char *argv[]) {

  // source code
  string ss;
  cin >> ss;
  int vccount = 0;
  vector<string> vc;

  rep(i, ss.length()) {
    vc.push_back(&ss[i]);
    if (vc.size() > 1 && (vc.at(vc.size() - 2) == string() + ss[i])) {
      if (i == ss.length() - 1) {
        cout << vc.size() - 1;
        return 0;
      } else {
        vc.at(vc.size() - 1) = vc.at(vc.size() - 1) + ss[i + 1];
        i++;
      }
    }
  }
  cout << vc.size();
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <vector>

#define repd(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) repd(i, 0, n)
typedef long long ll;

using namespace std;

int inputValue() {
  int a;
  cin >> a;
  return a;
};

void inputArray(int *p, int a){rep(i, a){cin >> p[i];
}
}
;

void inputVector(vector<int> *p, int a) {
  rep(i, a) {
    int input;
    cin >> input;
    p->push_back(input);
  }
}

template <typename T> void output(T a, int precision) {
  if (precision > 0) {
    cout << setprecision(precision) << a << "\n";
  } else {
    cout << a << "\n";
  }
}

int main(int argc, const char *argv[]) {

  // source code
  string ss;
  cin >> ss;
  int vccount = 0;
  vector<string> vc;

  rep(i, ss.length()) {
    vc.push_back(string() + ss[i]);
    if (vc.size() > 1 && (vc.at(vc.size() - 2) == string() + ss[i])) {
      if (i == ss.length() - 1) {
        cout << vc.size() - 1;
        return 0;
      } else {
        vc.at(vc.size() - 1) = vc.at(vc.size() - 1) + ss[i + 1];
        i++;
      }
    }
  }
  cout << vc.size();
  return 0;
}
Problem p02939 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
#define MAXN 100000
#define LL long long
LL read() {
  LL x = 0, F = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      F = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * F;
}
char s[MAXN + 5];
int n, ans, cnt, flag;
char pre;
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  int i;
  pre = '0';
  for (i = 1; i <= n; i++)
    if (s[i] == pre) {
      pre = '0';
      flag = 1;
    } else {
      if (!flag)
        pre = s[i];
      flag = 0;
      ans++;
    }
  printf("%d", ans);
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
#define MAXN 200000
#define LL long long
LL read() {
  LL x = 0, F = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      F = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * F;
}
char s[MAXN + 5];
int n, ans, cnt, flag;
char pre;
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  int i;
  pre = '0';
  for (i = 1; i <= n; i++)
    if (s[i] == pre) {
      pre = '0';
      flag = 1;
    } else {
      if (!flag)
        pre = s[i];
      flag = 0;
      ans++;
    }
  printf("%d", ans);
}
Problem p02939 (C++)

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

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

using namespace std;

const int INF = 1 << 29;

int main() {
  string s;
  cin >> s;
  int n = s.length();

  int dp[100001][2] =
      {}; // dp[i][j] = (s[0,i) について, 分割した一番右の文字列が長さ j+1
          // であるような最大分割数)
  for (int i = 1; i <= n; i++) {
    // 1 文字で分割
    dp[i][0] = dp[i - 1][1] + 1;
    if (i >= 2 && s[i - 1] != s[i - 2])
      dp[i][0] = max(dp[i][0], dp[i - 1][0] + 1);

    // 2 文字で分割
    dp[i][1] = -INF;
    if (i >= 2)
      dp[i][1] = dp[i - 2][0] + 1;
    if (i >= 4 && s.substr(i - 2, 2) != s.substr(i - 4, 2))
      dp[i][1] = max(dp[i][1], dp[i - 2][1] + 1);
  }

  printf("%d\n", max(dp[n][0], dp[n][1]));

  return 0;
}


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

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

using namespace std;

const int INF = 1 << 29;

int main() {
  string s;
  cin >> s;
  int n = s.length();

  int dp[200001][2] =
      {}; // dp[i][j] = (s[0,i) について, 分割した一番右の文字列が長さ j+1
          // であるような最大分割数)
  for (int i = 1; i <= n; i++) {
    // 1 文字で分割
    dp[i][0] = dp[i - 1][1] + 1;
    if (i >= 2 && s[i - 1] != s[i - 2])
      dp[i][0] = max(dp[i][0], dp[i - 1][0] + 1);

    // 2 文字で分割
    dp[i][1] = -INF;
    if (i >= 2)
      dp[i][1] = dp[i - 2][0] + 1;
    if (i >= 4 && s.substr(i - 2, 2) != s.substr(i - 4, 2))
      dp[i][1] = max(dp[i][1], dp[i - 2][1] + 1);
  }

  printf("%d\n", max(dp[n][0], dp[n][1]));

  return 0;
}
Problem p02939 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define M 1000000007
#define sz(a) (ll) a.size()
#define pll pair<ll, ll>
#define rep(i, a, b) for (ll i = (ll)a; i < (ll)b; i++)
#define sep(i, a, b) for (ll i = (ll)a; i >= (ll)b; i--)
#define mll map<ll, ll>
#define vl vector<ll>
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(a) a.begin(), a.end()
#define F first
#define S second
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
ll dp[100005][2];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  string s;
  cin >> s;
  if (s.size() == 1) {
    cout << 1 << "\n";
    return 0;
  }
  dp[0][1] = 1;
  dp[1][2] = 1;
  if (s[1] != s[0])
    dp[1][1] = 2;
  rep(i, 2, sz(s)) {
    if (dp[i - 1][2])
      dp[i][1] = 1 + dp[i - 1][2];
    if (dp[i - 1][1] && s[i] != s[i - 1])
      dp[i][1] = max(dp[i][1], 1 + dp[i - 1][1]);
    dp[i][2] = 1 + dp[i - 2][1];
    if (dp[i - 2][2] && !(s[i - 3] == s[i - 1] && s[i - 2] == s[i]))
      dp[i][2] = max(dp[i][2], 1 + dp[i - 2][2]);
  }
  cout << max(dp[sz(s) - 1][1], dp[sz(s) - 1][2]);
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define M 1000000007
#define sz(a) (ll) a.size()
#define pll pair<ll, ll>
#define rep(i, a, b) for (ll i = (ll)a; i < (ll)b; i++)
#define sep(i, a, b) for (ll i = (ll)a; i >= (ll)b; i--)
#define mll map<ll, ll>
#define vl vector<ll>
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(a) a.begin(), a.end()
#define F first
#define S second
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
ll dp[200005][2];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  string s;
  cin >> s;
  if (s.size() == 1) {
    cout << 1 << "\n";
    return 0;
  }
  dp[0][1] = 1;
  dp[1][2] = 1;
  if (s[1] != s[0])
    dp[1][1] = 2;
  rep(i, 2, sz(s)) {
    if (dp[i - 1][2])
      dp[i][1] = 1 + dp[i - 1][2];
    if (dp[i - 1][1] && s[i] != s[i - 1])
      dp[i][1] = max(dp[i][1], 1 + dp[i - 1][1]);
    dp[i][2] = 1 + dp[i - 2][1];
    if (dp[i - 2][2] && !(s[i - 3] == s[i - 1] && s[i - 2] == s[i]))
      dp[i][2] = max(dp[i][2], 1 + dp[i - 2][2]);
  }
  cout << max(dp[sz(s) - 1][1], dp[sz(s) - 1][2]);
}
Problem p02939 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
const int inf = 1012345678;
int dp[100009][5];
int main() {
  string s;
  cin >> s;
  int n = s.size();
  for (int i = 0; i <= n; ++i) {
    for (int j = 1; j <= 4; ++j) {
      dp[i][j] = -inf;
    }
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= 4 && j <= i; ++j) {
      if (i == j)
        dp[i][j] = 1;
      for (int k = 1; k <= 4; ++k) {
        if (dp[i - j][k] != -inf &&
            s.substr(i - j - k, k) != s.substr(i - j, j)) {
          dp[i][j] = max(dp[i][j], dp[i - j][k] + 1);
        }
      }
    }
  }
  int ans = -inf;
  for (int i = 1; i <= 4; ++i) {
    ans = max(ans, dp[n][i]);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
const int inf = 1012345678;
int dp[200009][5];
int main() {
  string s;
  cin >> s;
  int n = s.size();
  for (int i = 0; i <= n; ++i) {
    for (int j = 1; j <= 4; ++j) {
      dp[i][j] = -inf;
    }
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= 4 && j <= i; ++j) {
      if (i == j)
        dp[i][j] = 1;
      for (int k = 1; k <= 4; ++k) {
        if (dp[i - j][k] != -inf &&
            s.substr(i - j - k, k) != s.substr(i - j, j)) {
          dp[i][j] = max(dp[i][j], dp[i - j][k] + 1);
        }
      }
    }
  }
  int ans = -inf;
  for (int i = 1; i <= 4; ++i) {
    ans = max(ans, dp[n][i]);
  }
  cout << ans << endl;
  return 0;
}
Problem p02939 (C++)

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

const int N = 100000;

int n;
char s[N + 5];

int main() {
  scanf("%s", s);
  n = strlen(s);

  vector<int> a;
  int j = 0;
  for (int i = 0; i < n; ++i) {
    if (i && s[i] != s[i - 1])
      a.push_back(j), j = 1;
    else
      ++j;
  }
  a.push_back(j);

  int ans = 0;
  while (!a.empty()) {
    int x = a.back();
    a.pop_back();
    if (x % 3 == 0)
      ans += x / 3 * 2;
    if (x % 3 == 1)
      ans += x / 3 * 2 + 1;
    if (x % 3 == 2) {
      if (!a.empty())
        a.back()--, ans += x / 3 * 2 + 2;
      else
        ans += x / 3 * 2 + 1;
    }
  }
  printf("%d\n", ans);
  return 0;
}


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

const int N = 200000;

int n;
char s[N + 5];

int main() {
  scanf("%s", s);
  n = strlen(s);

  vector<int> a;
  int j = 0;
  for (int i = 0; i < n; ++i) {
    if (i && s[i] != s[i - 1])
      a.push_back(j), j = 1;
    else
      ++j;
  }
  a.push_back(j);

  int ans = 0;
  while (!a.empty()) {
    int x = a.back();
    a.pop_back();
    if (x % 3 == 0)
      ans += x / 3 * 2;
    if (x % 3 == 1)
      ans += x / 3 * 2 + 1;
    if (x % 3 == 2) {
      if (!a.empty())
        a.back()--, ans += x / 3 * 2 + 2;
      else
        ans += x / 3 * 2 + 1;
    }
  }
  printf("%d\n", ans);
  return 0;
}
Problem p02939 (Python)

Buggy submission (status: Runtime Error):
from sys import stdin


def main():
    S = stdin.readline().rstrip()
    S_len = len(S)

    sum = 0
    s = S[0]
    x = 1

    while True:
        if len(s) == 1:
            if s == S[x]:
                s = s + S[x]
            else:
                sum += 1
                s = S[x]
        else:
            sum += 2
            x += 1
            s = S[x]
        x += 1

        if x >= S_len:
            sum += 1
            break

    print(sum)


if __name__ == "__main__":
    main()


Fixed submission:
from sys import stdin


def main():
    S = stdin.readline().rstrip()
    S_len = len(S)

    sum = 0
    s = S[0]
    x = 1

    while True:
        if len(s) == 1:
            if s == S[x]:
                s = s + S[x]
            else:
                sum += 1
                s = S[x]
        else:
            sum += 2
            x += 1

            if x >= S_len:
                break
            s = S[x]
        x += 1

        if x >= S_len:
            sum += 1
            break

    print(sum)


if __name__ == "__main__":
    main()
Problem p02939 (Python)

Buggy submission (status: Runtime Error):
# https://atcoder.jp/contests/agc037/tasks/agc037_a

s = input()

ans = 0
pre = s[0]
i = 0
while i < len(s):
    cur = s[i]
    if pre == cur:
        pre = s[i] + s[i + 1]
        i += 2
    else:
        pre = cur
        i += 1
    ans += 1
print(ans)


Fixed submission:
# https://atcoder.jp/contests/agc037/tasks/agc037_a

s = input()

ans = 0
pre = ""
cur = ""
for i in range(len(s)):
    pre += s[i]
    if pre != cur:
        ans += 1
        cur = pre
        pre = ""
print(ans)
Problem p02939 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
#define rep(i, a, b) for (int i = (a); i < (b); i++)
typedef pair<int, int> P;
typedef long long ll;
const int INF = 100000000;

int dfs(string S, string s_pre, string s_post, int i) {
  if (i == S.size()) {
    return 0;
  } else {
    s_post.push_back(S[i]);
    if (s_pre == s_post) {
      return dfs(S, s_pre, s_post, i + 1);
    } else {
      s_pre = s_post;
      s_post = {};
      return dfs(S, s_pre, s_post, i + 1) + 1;
    }
  }
}

int main() {
  string S;
  cin >> S;
  string s_pre, s_post;
  int ans = dfs(S, s_pre, s_post, 0);
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
#define rep(i, a, b) for (int i = (a); i < (b); i++)
typedef pair<int, int> P;
typedef long long ll;
const int INF = 100000000;

int dfs(string &S, string &s_pre, string &s_post, int i) {
  if (i == S.size()) {
    return 0;
  } else {
    s_post.push_back(S[i]);
    if (s_pre == s_post) {
      return dfs(S, s_pre, s_post, i + 1);
    } else {
      s_pre = s_post;
      s_post = {};
      return dfs(S, s_pre, s_post, i + 1) + 1;
    }
  }
}

int main() {
  string S;
  cin >> S;
  string s_pre, s_post;
  int ans = dfs(S, s_pre, s_post, 0);
  cout << ans << endl;
}
Problem p02939 (C++)

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

using namespace std;

#define int long long
const double PI = 3.14159265358979323846;
typedef vector<int> vint;
typedef pair<int, int> pint;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
// cout << fixed << setprecision(20);

string S;
int dp[220000][5];

int dfs(int idx, int n) {
  if (idx + n == S.length())
    return 1;
  if (dp[idx][n] != -1)
    return dp[idx][n];
  int ret = 0;
  for (int i = 1; i < 5; i++) {
    if (idx + n + i > S.length())
      continue;
    if (S.substr(idx, n) == S.substr(idx + n, i))
      continue;
    ret = max(ret, dfs(idx + n, i) + 1);
  }
  return dp[idx][n] = ret;
}

signed main() {
  cin >> S;
  int ans = 0;
  for (int i = 0; i < 220000; i++)
    for (int j = 0; j < 11; j++)
      dp[i][j] = -1;
  for (int i = 1; i < 5; i++)
    ans = max(ans, dfs(0, i));
  cout << ans << endl;
}


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

using namespace std;

#define int long long
const double PI = 3.14159265358979323846;
typedef vector<int> vint;
typedef pair<int, int> pint;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
// cout << fixed << setprecision(20);

string S;
int dp[220000][5];

int dfs(int idx, int n) {
  if (idx + n == S.length())
    return 1;
  if (dp[idx][n] != -1)
    return dp[idx][n];
  int ret = 0;
  for (int i = 1; i < 5; i++) {
    if (idx + n + i > S.length())
      continue;
    if (S.substr(idx, n) == S.substr(idx + n, i))
      continue;
    ret = max(ret, dfs(idx + n, i) + 1);
  }
  return dp[idx][n] = ret;
}

signed main() {
  cin >> S;
  int ans = 0;
  for (int i = 0; i < 220000; i++)
    for (int j = 0; j < 5; j++)
      dp[i][j] = -1;
  for (int i = 1; i < 5; i++)
    ans = max(ans, dfs(0, i));
  cout << ans << endl;
}
Problem p02939 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define va first
#define vb second
using namespace std;
typedef pair<int, int> pii;
const int MN = 1e5 + 5;
int N, D[MN][2];
string S;

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  cin >> S;
  int N = S.size();
  D[0][1] = 1;
  D[0][2] = -1e9;
  D[1][1] = -1e9;
  if (S[0] != S[1])
    D[1][1] = 2;
  D[1][2] = 1;
  for (int i = 2; i < N; i++) {
    D[i][1] = D[i - 1][2] + 1;
    if (S[i - 1] != S[i])
      D[i][1] = max(D[i][1], D[i - 1][1] + 1);
    D[i][2] = D[i - 2][1] + 1;
    if (i >= 3 && (S[i - 2] != S[i] || S[i - 3] != S[i - 1]))
      D[i][2] = max(D[i][2], D[i - 2][2] + 1);
  }
  cout << max(D[N - 1][1], D[N - 1][2]);
}


Fixed submission:
#include <bits/stdc++.h>
#define va first
#define vb second
using namespace std;
typedef pair<int, int> pii;
const int MN = 2e5 + 5;
int N, D[MN][2];
string S;

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  cin >> S;
  int N = S.size();
  D[0][1] = 1;
  D[0][2] = -1e9;
  D[1][1] = -1e9;
  if (S[0] != S[1])
    D[1][1] = 2;
  D[1][2] = 1;
  for (int i = 2; i < N; i++) {
    D[i][1] = D[i - 1][2] + 1;
    if (S[i - 1] != S[i])
      D[i][1] = max(D[i][1], D[i - 1][1] + 1);
    D[i][2] = D[i - 2][1] + 1;
    if (i >= 3 && (S[i - 2] != S[i] || S[i - 3] != S[i - 1]))
      D[i][2] = max(D[i][2], D[i - 2][2] + 1);
  }
  cout << max(D[N - 1][1], D[N - 1][2]);
}
Problem p02939 (C++)

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

#define F first
#define S second

using namespace std;
typedef long double ld;
typedef long long ll;

const int max_n = 100011, max_m = 500011, inf = 1000111222;
const ll inff = 1000111222333444;

int dp[2][max_n];
int n;

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  string s;
  cin >> s;
  n = s.length();
  int cnt = 0;
  dp[0][0] = 1;
  dp[1][1] = 1;
  for (int i = 1; i < s.length(); ++i) {
    if (s[i] != s[i - 1]) {
      dp[0][i] = max(dp[0][i - 1], dp[1][i - 1]) + 1;
      if (i > 1)
        dp[1][i] = max(dp[1][i], max(dp[0][i - 2], dp[1][i - 2]) + 1);
    } else {
      dp[0][i] = dp[1][i - 1] + 1;
      dp[1][i] = max(dp[0][i - 2], dp[1][i - 2]) + 1;
    }
    // cout << dp[0][i] << " " << dp[1][i] << endl;
  }
  cout << max(dp[0][n - 1], dp[1][n - 1]) << endl;
  return 0;
}


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

#define F first
#define S second

using namespace std;
typedef long double ld;
typedef long long ll;

const int max_n = 200011, max_m = 500011, inf = 1000111222;
const ll inff = 1000111222333444;

int dp[2][max_n];
int n;

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  string s;
  cin >> s;
  n = s.length();
  int cnt = 0;
  dp[0][0] = 1;
  dp[1][1] = 1;
  for (int i = 1; i < s.length(); ++i) {
    if (s[i] != s[i - 1]) {
      dp[0][i] = max(dp[0][i - 1], dp[1][i - 1]) + 1;
      if (i > 1)
        dp[1][i] = max(dp[1][i], max(dp[0][i - 2], dp[1][i - 2]) + 1);
    } else {
      dp[0][i] = dp[1][i - 1] + 1;
      dp[1][i] = max(dp[0][i - 2], dp[1][i - 2]) + 1;
    }
    // cout << dp[0][i] << " " << dp[1][i] << endl;
  }
  cout << max(dp[0][n - 1], dp[1][n - 1]) << endl;
  return 0;
}
Problem p02939 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;

string s;
int dp[100005];

int main() {
  cin >> s;
  dp[0] = 1;
  if (s[0] == s[1]) {
    dp[1] = 1;
    dp[2] = 2;
  } else {
    dp[1] = 2;
    if (s[1] == s[2])
      dp[2] = 2;
    else
      dp[2] = 3;
  }
  for (int i = 3; i <= s.size(); i++) {
    if (s[i] != s[i - 1])
      dp[i] = dp[i - 1] + 1;
    else
      dp[i] = dp[i - 3] + 2;
  }
  cout << dp[s.size() - 1] << "\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;

string s;
ll dp[1000010];

int main() {
  cin >> s;
  dp[0] = 1;
  if (s[0] == s[1]) {
    dp[1] = 1;
    dp[2] = 2;
  } else {
    dp[1] = 2;
    if (s[1] == s[2])
      dp[2] = 2;
    else
      dp[2] = 3;
  }
  for (int i = 3; i <= s.size(); i++) {
    if (s[i] != s[i - 1])
      dp[i] = dp[i - 1] + 1;
    else
      dp[i] = dp[i - 3] + 2;
  }
  cout << dp[s.size() - 1] << "\n";
  return 0;
}
Problem p02939 (C++)

Buggy submission (status: Runtime Error):
/**
 *   author: bholuakku
 **/

#include <bits/stdc++.h>
#define Endl cout << "\n"
#define Test cout << "Hello\n"

using namespace std;
using ll = long long;

int main() {
#ifndef ONLINE_JUDGE
  freopen("C:\\Users\\ACER\\Desktop\\Codes\\input.txt", "r", stdin);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  string s;
  cin >> s;
  int n = s.size();
  vector<int> A(n + 1, 0), B(n + 1, 0);
  A[1] = 1;
  s = " " + s;
  for (int i = 2; i <= n; i++) {
    // taking 1 section
    int Max = 0;
    Max = max(Max, 1 + B[i - 1]);
    if (s[i] != s[i - 1])
      Max = max(Max, 1 + A[i - 1]);
    A[i] = Max;
    // taking 2 section
    Max = 0;
    Max = max(Max, 1 + A[i - 2]);
    // if(s[i]!=s[i-2]&&s[i-1]!=s[i-3])
    B[i] = Max;
  }
  // for(int i = 1; i<=n; i++
  cout << max(A[n], B[n]);
}


Fixed submission:
/**
 *   author: bholuakku
 **/

#include <bits/stdc++.h>
#define Endl cout << "\n"
#define Test cout << "Hello\n"

using namespace std;
using ll = long long;

int main() {
#ifndef ONLINE_JUDGE
  // freopen("C:\\Users\\ACER\\Desktop\\Codes\\input.txt", "r", stdin);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  string s;
  cin >> s;
  int n = s.size();
  vector<int> A(n + 1, 0), B(n + 1, 0);
  A[1] = 1;
  s = " " + s;
  for (int i = 2; i <= n; i++) {
    // taking 1 section
    int Max = 0;
    Max = max(Max, 1 + B[i - 1]);
    if (s[i] != s[i - 1])
      Max = max(Max, 1 + A[i - 1]);
    A[i] = Max;
    // taking 2 section
    Max = 0;
    Max = max(Max, 1 + A[i - 2]);
    // if(s[i]!=s[i-2]&&s[i-1]!=s[i-3])
    B[i] = Max;
  }
  // for(int i = 1; i<=n; i++
  cout << max(A[n], B[n]);
}
Problem p02939 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define abdelrahman010                                                         \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
typedef long long ll;
using namespace std;
const int N = 1e5 + 5;
string s;
vector<string> v;
int main() {
  abdelrahman010 cin >> s;
  string tmp = "";
  tmp += s[0];
  char c = '?';
  for (int i = 1; i < s.size(); i++) {
    if (c != s[i] && tmp.size() == 1 && s[i] == s[i - 1])
      tmp += s[i], c = s[i];
    else if (tmp == "" || tmp == v.back())
      tmp += s[i], c = '?';
    if (v.empty() || tmp != v.back()) {
      v.push_back(tmp);
      tmp = "";
    }
  }
  cout << v.size();
  return 0;
}

Fixed submission:
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define abdelrahman010                                                         \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
typedef long long ll;
using namespace std;
const int N = 1e5 + 5;
string s;
vector<string> v;
int main() {
  abdelrahman010 cin >> s;
  string tmp = "";
  for (int i = 0; i < s.size(); i++) {
    tmp += s[i];
    if (v.empty() || tmp != v.back()) {
      v.push_back(tmp);
      tmp = "";
    }
  }
  cout << v.size();
  return 0;
}
Problem p02939 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  char s[100005] = {};
  scanf("%s", s);
  int len = strlen(s);
  int rem = 0;
  int flag = 0;
  for (int i = 0; i < len; i++) {
    // printf("###%d\n",i);
    if (i >= 1) {
      if (s[i] == s[i - 1] && flag == 0) {
        rem++;
        flag = 1;
        i++;
      } else {
        flag = 0;
      }
    }
  }
  printf("%d\n", len - rem);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  char s[200005] = {};
  scanf("%s", s);
  int len = strlen(s);
  int rem = 0;
  int flag = 0;
  for (int i = 0; i < len; i++) {
    // printf("###%d\n",i);
    if (i >= 1) {
      if (s[i] == s[i - 1] && flag == 0) {
        rem++;
        flag = 1;
        i++;
      } else {
        flag = 0;
      }
    }
  }
  printf("%d\n", len - rem);
  return 0;
}
Problem p02939 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define ALL(v) (v).begin(), (v).end()
#define pb push_back
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repr(i, a, n) for (int i = a; i > n; i--)
#define leng(n) (int)(log10(n) + 1)
#define INF 9000000000000000000
using namespace std;

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } //  最大公約数gcd
int lcm(int a, int b) { return a / gcd(a, b) * b; }     //  最小公倍数lcm

signed main() {
  string s;
  cin >> s;
  int ans = 0;
  int same = 1;
  vector<int> sa(0);
  rep(i, 0, s.size() - 1) {
    if (s[i] == s[i + 1]) {
      same++;
      continue;
    }
    if (s[i] != s[i + 1]) {
      if (same % 3 != 0)
        sa.pb(same % 3);
      if (same % 3 == 0)
        ans += 2 * (same / 3);
      else
        ans += (2 * (same / 3) + 1);
      same = 1;
    }
  }
  if (same % 3 != 0)
    sa.pb(same % 3);
  if (same % 3 == 0)
    ans += 2 * (same / 3);
  else
    ans += (2 * (same / 3) + 1);
  rep(i, 0, sa.size() - 1) {
    if (sa[i] == 2 && sa[i + 1] == 2) {
      ans++;
      sa[i] = 1;
      sa[i + 1] = 1;
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define ALL(v) (v).begin(), (v).end()
#define pb push_back
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repr(i, a, n) for (int i = a; i > n; i--)
#define leng(n) (int)(log10(n) + 1)
#define INF 9000000000000000000
using namespace std;

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } //  最大公約数gcd
int lcm(int a, int b) { return a / gcd(a, b) * b; }     //  最小公倍数lcm

signed main() {
  string s;
  cin >> s;
  int ans = 0;
  int same = 1;
  vector<int> sa(0);
  rep(i, 0, s.size() - 1) {
    if (s[i] == s[i + 1]) {
      same++;
      continue;
    }
    if (s[i] != s[i + 1]) {
      if (same % 3 != 0)
        sa.pb(same % 3);
      if (same % 3 == 0)
        ans += 2 * (same / 3);
      else
        ans += (2 * (same / 3) + 1);
      same = 1;
    }
  }
  if (same % 3 != 0)
    sa.pb(same % 3);
  if (same % 3 == 0)
    ans += 2 * (same / 3);
  else
    ans += (2 * (same / 3) + 1);
  if (sa.size() != 0) {
    rep(i, 0, sa.size() - 1) {
      if (sa[i] == 2 && sa[i + 1] == 2) {
        ans++;
        sa[i] = 1;
        sa[i + 1] = 1;
      }
    }
  }
  cout << ans << endl;
}
Problem p02939 (C++)

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

// #pragma GCC optimize("O3")
// #pragma GCC target("sse4")

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RFOR(i, b, a) for (int i = (b)-1; i >= (a); --i)
#define ITER(it, a)                                                            \
  for (__typeof(a.begin()) it = a.begin(); it != a.end(); it++)
#define FILL(a, value) memset(a, value, sizeof(a))

#define SZ(a) (int)((a).size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair

typedef long long LL;
typedef pair<int, int> PII;

const double PI = acos(-1.0);
const int INF = 1000 * 1000 * 1000 + 7;
const LL LINF = (LL)INF * INF;

const int MAX = 1 << 17;
const int K = 4;

int dp[MAX][K];

void upd(int &x, int val) { x = max(x, val); }

int main() {
  // ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  string s;
  cin >> s;
  int n = SZ(s);
  FOR(i, 0, MAX)
  FOR(j, 0, K)
  dp[i][j] = -INF;
  FOR(i, 0, K)
  dp[i][i] = 1;
  FOR(i, 0, n)
  FOR(j, 0, K) {
    if (dp[i][j] >= 0 && i + j + 1 < n) {
      bool ok = false;
      FOR(k, i - j, i + 1)
      ok |= s[k] != s[k + j + 1];
      if (ok)
        upd(dp[i + j + 1][j], dp[i][j] + 1);
    }
    FOR(k, 0, K)
    if (k != j && i + k + 1 < n)
      upd(dp[i + k + 1][k], dp[i][j] + 1);
  }
  int ans = 0;
  FOR(i, 0, K)
  upd(ans, dp[n - 1][i]);
  cout << ans;
  return 0;
}


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

// #pragma GCC optimize("O3")
// #pragma GCC target("sse4")

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RFOR(i, b, a) for (int i = (b)-1; i >= (a); --i)
#define ITER(it, a)                                                            \
  for (__typeof(a.begin()) it = a.begin(); it != a.end(); it++)
#define FILL(a, value) memset(a, value, sizeof(a))

#define SZ(a) (int)((a).size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair

typedef long long LL;
typedef pair<int, int> PII;

const double PI = acos(-1.0);
const int INF = 1000 * 1000 * 1000 + 7;
const LL LINF = (LL)INF * INF;

const int MAX = 1 << 18;
const int K = 4;

int dp[MAX][K];

void upd(int &x, int val) { x = max(x, val); }

int main() {
  // ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  string s;
  cin >> s;
  int n = SZ(s);
  FOR(i, 0, MAX)
  FOR(j, 0, K)
  dp[i][j] = -INF;
  FOR(i, 0, K)
  dp[i][i] = 1;
  FOR(i, 0, n)
  FOR(j, 0, K) {
    if (dp[i][j] >= 0 && i + j + 1 < n) {
      bool ok = false;
      FOR(k, i - j, i + 1)
      ok |= s[k] != s[k + j + 1];
      if (ok)
        upd(dp[i + j + 1][j], dp[i][j] + 1);
    }
    FOR(k, 0, K)
    if (k != j && i + k + 1 < n)
      upd(dp[i + k + 1][k], dp[i][j] + 1);
  }
  int ans = 0;
  FOR(i, 0, K)
  upd(ans, dp[n - 1][i]);
  cout << ans;
  return 0;
}
Problem p02939 (C++)

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

using namespace std;

int main() {
  string s;
  cin >> s;
  long long int cnt = 1;
  char cur = s[0];
  int i = 1;
  while (1) {
    if (s[i] != cur) {
      cur = s[i];
      cnt++;
      i++;
      if (i == s.length()) {
        break;
      }
    } else {
      if (i == s.length() - 1) {
        break;
      } else if (i == s.length() - 2) {
        cnt++;
        break;
      } else {
        cur = s[i + 2];
        i += 3;
        cnt += 2;
      }
    }
  }
  cout << cnt << endl;
}

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

using namespace std;

int main() {
  string s;
  cin >> s;
  long long int cnt = 1;
  char cur = s[0];
  int i = 1;
  while (1) {
    if (s[i] != cur) {
      cur = s[i];
      cnt++;
      i++;
      if (i == s.length()) {
        break;
      }
    } else {
      if (i == s.length() - 1) {
        break;
      } else if (i == s.length() - 2) {
        cnt++;
        break;
      } else {
        cur = s[i + 2];
        i += 3;
        cnt += 2;
        if (i == s.length()) {
          break;
        }
      }
    }
  }
  cout << cnt << endl;
}
Problem p02939 (C++)

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

int main() {
  string s;
  cin >> s;
  int ret = 0;
  if (s.size() == 1) {
    ret = 1;
  } else if (s.size() == 2 && s[0] == s[1]) {
    ret = 1;
  } else if (s.size() == 2 && s[0] != s[1]) {
    ret = 2;
  } else {
    string bef = "";
    while (true) {
      if (s.size() == 3) {
        if (bef == s.substr(0, 1))
          return 2; // bef is 1 char: ab-c
        if (bef != s.substr(0, 1) && s[0] != s[1] && s[1] != s[2]) {
          ret += 3;
          break;
        } // a-b-c or a-b-a
        ret += 2;
        break;
      }
      if (s.size() == 2) {
        if (bef != s.substr(0, 1) && s[0] != s[1]) {
          ret += 2;
          break;
        } else {
          ret += 1;
          break;
        }
      }

      int i = 1;
      while (s.substr(0, i) == bef)
        i++;
      bef = s.substr(0, i);
      s = s.substr(i, s.size() - i);
      ++ret;
    }
  }
  cout << ret << endl;
  return 0;
}

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

int main() {
  string s;
  cin >> s;
  int ret = 0;
  if (s.size() == 1) {
    ret = 1;
  } else if (s.size() == 2 && s[0] == s[1]) {
    ret = 1;
  } else if (s.size() == 2 && s[0] != s[1]) {
    ret = 2;
  } else {
    string bef = "";
    while (true) {
      if (s.size() == 3) {
        if (bef == s.substr(0, 1)) {
          ret += 2;
          break;
        }; // bef is 1 char: ab-c
        if (bef != s.substr(0, 1) && s[0] != s[1] && s[1] != s[2]) {
          ret += 3;
          break;
        } // a-b-c or a-b-a
        ret += 2;
        break;
      }
      if (s.size() == 2) {
        if (bef != s.substr(0, 1) && s[0] != s[1]) {
          ret += 2;
          break;
        } else {
          ret += 1;
          break;
        }
      }

      int i = 1;
      while (s.substr(0, i) == bef)
        i++;
      bef = s.substr(0, i);
      s = s.substr(i, s.size() - i);
      ++ret;
    }
  }
  cout << ret << endl;
  return 0;
}
Problem p02939 (C++)

Buggy submission (status: Runtime Error):
/**
 *	  Name: Ajay
 *	  Institute: IIITH
 */

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string.h>
#include <vector>
#define ll long long
#define inf 0x7fffffff
#define mod 1000000007
#define pb push_back
#define ppi pair<int, int>
#define vi vector<int>
#define vvi vector<vector<int>>
#define vii vector<ppi>
#define vll vector<ll>
#define mp make_pair
#define fi first
#define se second
using namespace std;

void fastio() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
void input() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

int main() {
  fastio();
  input();
  string s;
  cin >> s;
  int n, i, k = 0;
  n = s.size();
  string temp = "", prev = "";
  for (i = 0; i < n; i++) {

    if (temp + s[i] != prev) {
      prev = temp + s[i];
      k++;
      temp = "";
    } else
      temp += s[i];
  }

  if (temp.size() > 0 && temp != prev)
    k++;
  cout << k;
  return 0;
}

Fixed submission:
/**
 *	  Name: Ajay
 *	  Institute: IIITH
 */

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string.h>
#include <vector>
#define ll long long
#define inf 0x7fffffff
#define mod 1000000007
#define pb push_back
#define ppi pair<int, int>
#define vi vector<int>
#define vvi vector<vector<int>>
#define vii vector<ppi>
#define vll vector<ll>
#define mp make_pair
#define fi first
#define se second
using namespace std;

void fastio() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
void input() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

int main() {
  fastio();
  string s;
  cin >> s;
  int n, i, k = 0;
  n = s.size();
  string temp = "", prev = "";
  for (i = 0; i < n; i++) {

    if (temp + s[i] != prev) {
      prev = temp + s[i];
      k++;
      temp = "";
    } else
      temp += s[i];
  }

  if (temp.size() > 0 && temp != prev)
    k++;
  cout << k;
  return 0;
}
Problem p02939 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
int len, ans;
char S[100100];
int main() {
  scanf("%s", S + 1);
  len = strlen(S + 1);
  int now = 1;
  while (now <= len) {
    ans++, now++;
    while (S[now] != S[now - 1] && now <= len)
      ans++, now++;
    if (now >= len)
      break;
    ans++, now += 2;
  }
  printf("%d\n", ans);
}

Fixed submission:
#include <cstdio>
#include <cstring>
int len, ans;
char S[200100];
int main() {
  scanf("%s", S + 1);
  len = strlen(S + 1);
  int now = 1;
  while (now <= len) {
    ans++, now++;
    while (S[now] != S[now - 1] && now <= len)
      ans++, now++;
    if (now >= len)
      break;
    ans++, now += 2;
  }
  printf("%d\n", ans);
}