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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define ll long long
#define int long long
#define mp make_pair
#define inf 1000000007
using namespace std;
signed main() {
  string s;
  cin >> s;
  set<char> moji;
  vector<int> cut;
  cut.pb(s.size());
  for (int i = s.size() - 1; i >= 0; i--) {
    moji.insert(s[i]);
    if (moji.size() == 26) {
      moji.clear();
      cut.pb(i);
    }
  }
  moji.clear();
  reverse(cut.begin(), cut.end());
  string str = "";
  int cur = 0;
  for (int i = 0; i < cut.size(); i++) {
    moji.clear();
    char ta;
    for (int j = cur; j < cut[i]; j++) {
      moji.insert(s[j]);
    }
    for (int j = 0; j < 26; j++) {
      if (moji.find('a' + j) == moji.end()) {
        str.pb('a' + j);
        ta = 'a' + j;
        j = 26;
      }
    }
    cur = cut[i];
    while (s[cur - 1] != ta) {
      cur++;
    }
  }
  cout << str << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define ll long long
#define int long long
#define mp make_pair
#define inf 1000000007
using namespace std;
signed main() {
  string s;
  cin >> s;
  set<char> moji;
  vector<int> cut;
  cut.pb(s.size());
  for (int i = s.size() - 1; i >= 0; i--) {
    moji.insert(s[i]);
    if (moji.size() == 26) {
      moji.clear();
      cut.pb(i);
    }
  }
  moji.clear();
  reverse(cut.begin(), cut.end());
  string str = "";
  int cur = 0;
  for (int i = 0; i < cut.size(); i++) {
    moji.clear();
    char ta;
    for (int j = cur; j < cut[i]; j++) {
      moji.insert(s[j]);
    }
    for (int j = 0; j < 26; j++) {
      if (moji.find('a' + j) == moji.end()) {
        str.pb('a' + j);
        ta = 'a' + j;
        j = 26;
      }
    }
    cur = cut[i] + 1;
    while (s[cur - 1] != ta && cur < s.size() - 1) {
      cur++;
    }
  }
  cout << str << endl;
  return 0;
}
Problem p03629 (C++)

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

struct Node {
  int next[26]{}, length{1 << 30};
  int path{1 << 30};
};

int main() {
  char a[2000]{};
  scanf("%s", a);
  int n;
  n = strlen(a);
  std::vector<Node> dp(n + 1);
  for (int i = 0; i < 26; i++)
    dp[n].next[i] = n + 1;
  dp[n].length = 1;
  for (int i = n - 1; i >= 0; i--) {
    Node &node{dp[i]};
    for (int j = 0; j < 26; j++)
      node.next[j] = dp[i + 1].next[j];
    node.next[a[i] - 'a'] = i + 1;
    for (int j = 0; j < 26; j++)
      if (dp[node.next[j]].length + 1 < node.length) {
        node.length = dp[node.next[j]].length + 1;
        node.path = node.next[j];
      }
  }
  int index{};
  while (dp[index].path <= n) {
    index = dp[index].path;
    putchar(a[index - 1]);
  }
  for (int i = 0; i < 26; i++) {
    if (dp[index].next[i] == n + 1) {
      putchar(i + 'a');
      break;
    }
  }
  putchar('\n');

  return 0;
}

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

struct Node {
  int next[26]{}, length{1 << 30};
  int path{1 << 30};
};

int main() {
  char a[200000]{};
  scanf("%s", a);
  int n;
  n = strlen(a);
  std::vector<Node> dp(n + 1);
  for (int i = 0; i < 26; i++)
    dp[n].next[i] = n + 1;
  dp[n].length = 1;
  for (int i = n - 1; i >= 0; i--) {
    Node &node{dp[i]};
    for (int j = 0; j < 26; j++)
      node.next[j] = dp[i + 1].next[j];
    node.next[a[i] - 'a'] = i + 1;
    for (int j = 0; j < 26; j++)
      if (dp[node.next[j]].length + 1 < node.length) {
        node.length = dp[node.next[j]].length + 1;
        node.path = node.next[j];
      }
  }
  int index{};
  while (dp[index].path <= n) {
    index = dp[index].path;
    putchar(a[index - 1]);
  }
  for (int i = 0; i < 26; i++) {
    if (dp[index].next[i] == n + 1) {
      putchar(i + 'a');
      break;
    }
  }
  putchar('\n');

  return 0;
}
Problem p03629 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
char str[200005];
int dp[200005];
int Next[200005][30];
int main() {
  while (scanf("%s", str) != EOF) {

    int len = strlen(str);
    for (int i = 0; i < 26; i++) // 以Len结尾的i出现的位置
      Next[len][i] = len;
    for (int i = len - 1; i >= 0; i--) {
      for (int j = 0; j < 26; j++)
        Next[i][j] = ((str[i] == (j + 'a')) ? i : Next[i + 1][j]);
    }
    dp[len] = 1;
    for (int i = len - 1; i >= 0; i--) {
      dp[i] = dp[Next[i + 1][0] + 1] + 1;
      for (int j = 1; j < 26; j++)
        dp[i] = min(dp[i], dp[Next[i][j] + 1] + 1);
    }
    int flag = 0;
    while (dp[flag]) {
      for (int i = 0; i < 26; i++) {
        if (dp[flag] == dp[Next[flag][i] + 1] + 1) {
          printf("%c", 'a' + i);
          flag = Next[flag][i] + 1;
          break;
        }
      }
    }
    printf("\n");
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
char str[200005];
int dp[200005];
int Next[200005][30];
int main() {
  while (scanf("%s", str) != EOF) {

    int len = strlen(str);
    for (int i = 0; i < 26; i++) // 以Len结尾的i出现的位置
      Next[len][i] = len;
    for (int i = len - 1; i >= 0; i--) {
      for (int j = 0; j < 26; j++)
        Next[i][j] = ((str[i] == (j + 'a')) ? i : Next[i + 1][j]);
    }
    dp[len] = 1;
    for (int i = len - 1; i >= 0; i--) {
      dp[i] = dp[Next[i][0] + 1] + 1;
      for (int j = 1; j < 26; j++)
        dp[i] = min(dp[i], dp[Next[i][j] + 1] + 1);
    }
    int flag = 0;
    while (dp[flag]) {
      for (int i = 0; i < 26; i++) {
        if (dp[flag] == dp[Next[flag][i] + 1] + 1) {
          printf("%c", 'a' + i);
          flag = Next[flag][i] + 1;
          break;
        }
      }
    }
    printf("\n");
  }
  return 0;
}
Problem p03629 (Python)

Buggy submission (status: Time Limit Exceeded):
from collections import deque


A = input()
N = len(A)
a = ord("a")

edge = [[N + 1] * 26 for _ in range(N + 1)]
for i in range(N - 1, -1, -1):
    for j in range(26):
        edge[i][j] = edge[i + 1][j]
    c = ord(A[i]) - a
    edge[i][c] = i + 1


recon = [None] * (N + 2)  # DP復元用。(直前の頂点, 文字)を格納する。
q = deque()
q.append(0)  # 頂点
while q:
    i = q.popleft()
    if i == N + 1:  # 頂点N+1に到達したら処理終了
        break
    for j in range(26):
        ni = edge[i][j]
        if recon[ni] is None:  # その頂点に最短で到達した場合のみ次の処理を継続
            recon[ni] = (i, chr(a + j))
            q.append(ni)

i = N + 1
ans = []
while i > 0:
    pi, c = recon[i]
    ans.append(c)
    i = pi

print("".join(reversed(ans)))


Fixed submission:
from collections import deque


A = input()
N = len(A)
a = ord("a")

edge = [[N + 1] * 26 for _ in range(N + 1)]
for i in range(N - 1, -1, -1):
    for j in range(26):
        edge[i][j] = edge[i + 1][j]
    c = ord(A[i]) - a
    edge[i][c] = i + 1  # 文字cの直後の頂点


recon = [None] * (N + 2)  # DP復元用。(直前の頂点, 文字)を格納する。
q = deque()
q.append(0)  # 頂点
while q:
    i = q.popleft()
    if i == N + 1:  # 頂点N+1に到達したら処理終了
        break
    for j in range(26):
        ni = edge[i][j]
        if recon[ni] is None:  # その頂点に最短で到達した場合のみ次の処理を継続
            recon[ni] = (i, chr(a + j))
            q.append(ni)

i = N + 1
ans = []
while i > 0:
    pi, c = recon[i]
    ans.append(c)
    i = pi

print("".join(reversed(ans)))
Problem p03629 (C++)

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

using namespace std;

/*
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,
tree_order_statistics_node_update>;
*/
typedef long long ll;
const ll mk = 200054;
const ll logn = 29;
const ll mod = 1e9 + 7;

#define endl "\n"
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);
#define fi first
#define se second
#define pii pair<ll, ll>
#define mp make_pair
#define pq priority_queue<int, std::vector<int>, std::greater<int>>
#define rep(i, n) for (int i = 0; i < n; i++)
#define push_back pb

int n;
char ans[mk];
int cac[mk][26];
int f[mk], go[mk];
string s;

inline bool down(int &x, const int y) { return x > y ? x = y, 1 : 0; }

int main() {
  int i, j;
  cin >> s;
  n = s.length();
  fill(cac[n], cac[n] + 26, n);
  for (i = n - 1; i >= 0; --i)
    memcpy(cac[i], cac[i + 1], 101 /*calculated*/), cac[i][s[i] - 97] = i;
  f[n] = 1;
  for (i = n - 1; i >= 0; --i) {
    f[i] = INT_MAX;
    for (j = 0; j < 26; ++j)
      if (down(f[i], f[cac[i][j] + 1]))
        go[i] = j;
    ++f[i];
  }
  for (j = i = 0; i <= n; i = cac[i][go[i]] + 1)
    ans[j++] = go[i] + 97;
  return puts(ans), 0;
}

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

using namespace std;

/*
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,
tree_order_statistics_node_update>;
*/
typedef long long ll;
const ll mk = 200054;
const ll logn = 29;
const ll mod = 1e9 + 7;

#define endl "\n"
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);
#define fi first
#define se second
#define pii pair<ll, ll>
#define mp make_pair
#define pq priority_queue<int, std::vector<int>, std::greater<int>>
#define rep(i, n) for (int i = 0; i < n; i++)
#define push_back pb

int n;
char ans[mk];
int cac[mk][26];
int f[mk], go[mk];
string s;

inline bool down(int &x, const int y) { return x > y ? x = y, 1 : 0; }

int main() {
  int i, j;
  cin >> s;
  n = s.length();
  fill(cac[n], cac[n] + 26, n);
  for (i = n - 1; i >= 0; --i)
    memcpy(cac[i], cac[i + 1], 104 /*calculated*/), cac[i][s[i] - 97] = i;
  f[n] = 1;
  for (i = n - 1; i >= 0; --i) {
    f[i] = INT_MAX;
    for (j = 0; j < 26; ++j)
      if (down(f[i], f[cac[i][j] + 1]))
        go[i] = j;
    ++f[i];
  }
  for (j = i = 0; i <= n; i = cac[i][go[i]] + 1)
    ans[j++] = go[i] + 97;
  return puts(ans), 0;
}
Problem p03629 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int MaxN = 100005;
char s[MaxN];
int l, mn;
int nxt[MaxN][26];
int dp[MaxN], go[MaxN], ch[MaxN];
int main() {
  scanf("%s", s + 1);
  l = strlen(s + 1);
  mn = 1;
  for (int i = 1; i <= l; ++i)
    if (!nxt[0][s[i] - 'a'])
      nxt[0][s[i] - 'a'] = i;
  for (int i = 0; i < 26; ++i)
    if (!nxt[0][i])
      nxt[0][i] = l + 1;
  for (int i = 1; i <= l; ++i) {
    for (int j = 0; j < 26; ++j)
      nxt[i][j] = nxt[i - 1][j];
    nxt[i][s[i] - 'a']++;
    while (nxt[i][s[i] - 'a'] <= l && s[nxt[i][s[i] - 'a']] != s[i])
      nxt[i][s[i] - 'a']++;
  }
  memset(dp, 0x3f, sizeof dp);
  memset(go, 0xff, sizeof go);
  memset(ch, 'z' + 1, sizeof ch);
  dp[l + 1] = 0;
  for (int i = l; i >= 0; --i) {
    for (int j = 0; j < 26; ++j) {
      int t = nxt[i][j];
      if (dp[t] + 1 == dp[i] && j + 'a' < ch[i]) {
        ch[i] = j + 'a';
        go[i] = t;
      } else if (dp[t] + 1 < dp[i]) {
        dp[i] = dp[t] + 1;
        ch[i] = j + 'a';
        go[i] = t;
      }
    }
  }
  int p = 0;
  while (p != l + 1) {
    putchar(ch[p]);
    p = go[p];
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int MaxN = 200005;
char s[MaxN];
int l, mn;
int nxt[MaxN][26];
int dp[MaxN], go[MaxN], ch[MaxN];
int main() {
  scanf("%s", s + 1);
  l = strlen(s + 1);
  mn = 1;
  for (int i = 1; i <= l; ++i)
    if (!nxt[0][s[i] - 'a'])
      nxt[0][s[i] - 'a'] = i;
  for (int i = 0; i < 26; ++i)
    if (!nxt[0][i])
      nxt[0][i] = l + 1;
  for (int i = 1; i <= l; ++i) {
    for (int j = 0; j < 26; ++j)
      nxt[i][j] = nxt[i - 1][j];
    nxt[i][s[i] - 'a']++;
    while (nxt[i][s[i] - 'a'] <= l && s[nxt[i][s[i] - 'a']] != s[i])
      nxt[i][s[i] - 'a']++;
  }
  memset(dp, 0x3f, sizeof dp);
  memset(go, 0xff, sizeof go);
  memset(ch, 'z' + 1, sizeof ch);
  dp[l + 1] = 0;
  for (int i = l; i >= 0; --i) {
    for (int j = 0; j < 26; ++j) {
      int t = nxt[i][j];
      if (dp[t] + 1 == dp[i] && j + 'a' < ch[i]) {
        ch[i] = j + 'a';
        go[i] = t;
      } else if (dp[t] + 1 < dp[i]) {
        dp[i] = dp[t] + 1;
        ch[i] = j + 'a';
        go[i] = t;
      }
    }
  }
  int p = 0;
  while (p != l + 1) {
    putchar(ch[p]);
    p = go[p];
  }
  return 0;
}
Problem p03629 (C++)

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

int n, dp[N], st[30][N * 4];
char s[N];

int get(int node, int bas, int son, int x, int y, int c) {

  if (x > y)
    return n + 1;
  if (bas > y || son < x)
    return n + 1;
  if (bas >= x && son <= y)
    return st[c][node];
  int orta = (bas + son) / 2;
  return min(get(node * 2, bas, orta, x, y, c),
             get(node * 2 + 1, orta + 1, son, x, y, c));
}

void build(int node, int bas, int son, int c) {

  if (bas == son) {
    if (s[bas] == 'a' + c) {
      st[c][node] = bas;
    } else {
      st[c][node] = n + 1;
    }
    return;
  }
  int orta = (bas + son) / 2;
  build(node * 2, bas, orta, c);
  build(node * 2 + 1, orta + 1, son, c);
  st[c][node] = min(st[c][node * 2], st[c][node * 2 + 1]);
}

int dfs(int now) {

  int ret = inf;
  if (now > n)
    return dp[now] = 0;
  if (~dp[now])
    return dp[now];
  for (int i = 0; i < 26; i++) {
    int aft = get(1, 1, n, now + 1, n, i);
    ret = min(ret, dfs(aft) + 1);
  }
  return dp[now] = ret;
}

int main() {

  memset(dp, -1, sizeof(dp));
  scanf("%s", s + 1);
  n = strlen(s + 1);
  for (int i = 0; i < 26; i++) {
    build(1, 1, n, i);
  }
  dfs(0);
  int now = 0, cev = dp[0];
  while (now <= n) {
    for (int i = 0; i < 26; i++) {
      int aft = get(1, 1, n, now + 1, n, i);
      if (cev - 1 == dp[aft]) {
        printf("%c", i + 'a');
        cev = dp[aft];
        now = aft;
        break;
      }
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define N 200005
#define inf 1000000000
using namespace std;

int n, dp[N], st[30][N * 4];
char s[N];

int get(int node, int bas, int son, int x, int y, int c) {

  if (x > y)
    return n + 1;
  if (bas > y || son < x)
    return n + 1;
  if (bas >= x && son <= y)
    return st[c][node];
  int orta = (bas + son) / 2;
  return min(get(node * 2, bas, orta, x, y, c),
             get(node * 2 + 1, orta + 1, son, x, y, c));
}

void build(int node, int bas, int son, int c) {

  if (bas == son) {
    if (s[bas] == 'a' + c) {
      st[c][node] = bas;
    } else {
      st[c][node] = n + 1;
    }
    return;
  }
  int orta = (bas + son) / 2;
  build(node * 2, bas, orta, c);
  build(node * 2 + 1, orta + 1, son, c);
  st[c][node] = min(st[c][node * 2], st[c][node * 2 + 1]);
}

int dfs(int now) {

  int ret = inf;
  if (now > n)
    return dp[now] = 0;
  if (~dp[now])
    return dp[now];
  for (int i = 0; i < 26; i++) {
    int aft = get(1, 1, n, now + 1, n, i);
    ret = min(ret, dfs(aft) + 1);
  }
  return dp[now] = ret;
}

int main() {

  memset(dp, -1, sizeof(dp));
  scanf("%s", s + 1);
  n = strlen(s + 1);
  for (int i = 0; i < 26; i++) {
    build(1, 1, n, i);
  }
  dfs(0);
  int now = 0, cev = dp[0];
  while (now <= n) {
    for (int i = 0; i < 26; i++) {
      int aft = get(1, 1, n, now + 1, n, i);
      if (cev - 1 == dp[aft]) {
        printf("%c", i + 'a');
        cev = dp[aft];
        now = aft;
        break;
      }
    }
  }
}
Problem p03629 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define FOR(i, bg, ed) for (ll i = (bg); i < (ed); i++)
#define REP(i, n) FOR(i, 0, n)
#define REP1(i, n) for (ll i = 1; i <= (n); i++)
#define MOD 1000000007
#define int long long
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const int INF = 1e9;

string A;
int ne[202020][26];
int dp[202020];
P memo[202020];

int rec(int i) {
  if (i >= A.length()) {
    memo[i].second = -1;
    return 1;
  }
  if (dp[i] != 0)
    return dp[i];

  int minV = 90909090;
  char minC;

  REP(j, 26) {
    if (ne[i][j] == A.length()) { // 見つからなかった場合
      minV = 1;
      minC = j;
      memo[i].first = minC;
      memo[i].second = -1;
      break;
    }
    int V = 1 + rec(ne[i][j] + 1);
    if (V < minV) {
      minV = V;
      minC = j;
      memo[i].first = minC;
      memo[i].second = ne[i][j] + 1;
    }
  }

  return dp[i] = minV;
}

signed main() {
  cin >> A;

  REP(i, 26) { ne[A.length()][i] = A.length(); }
  for (int i = A.length() - 1; i >= 0; i--) {
    REP(j, 26) {
      ne[i][j] = ne[i + 1][j];
      if (A[i] - 'a' == j)
        ne[i][j] = i;
    }
  }

  /*
  REP(i,A.length()+1) {
      cout << ne[i][0] << " ";
  }
  */

  // cout << rec(0) << endl;
  for (int i = 0; i != -1; i = memo[i].second) {
    cout << (char)(memo[i].first + 'a');
  }
  cout << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define FOR(i, bg, ed) for (ll i = (bg); i < (ed); i++)
#define REP(i, n) FOR(i, 0, n)
#define REP1(i, n) for (ll i = 1; i <= (n); i++)
#define MOD 1000000007
#define int long long
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const int INF = 1e9;

string A;
int ne[202020][26];
int dp[202020];
P memo[202020];

int rec(int i) {
  if (i >= A.length()) {
    memo[i].second = -1;
    return 1;
  }
  if (dp[i] != 0)
    return dp[i];

  int minV = 90909090;
  char minC;

  REP(j, 26) {
    if (ne[i][j] == A.length()) { // 見つからなかった場合
      minV = 1;
      minC = j;
      memo[i].first = minC;
      memo[i].second = -1;
      break;
    }
    int V = 1 + rec(ne[i][j] + 1);
    if (V < minV) {
      minV = V;
      minC = j;
      memo[i].first = minC;
      memo[i].second = ne[i][j] + 1;
    }
  }

  return dp[i] = minV;
}

signed main() {
  cin >> A;

  REP(i, 26) { ne[A.length()][i] = A.length(); }
  for (int i = A.length() - 1; i >= 0; i--) {
    REP(j, 26) {
      ne[i][j] = ne[i + 1][j];
      if (A[i] - 'a' == j)
        ne[i][j] = i;
    }
  }

  /*
  REP(i,A.length()+1) {
      cout << ne[i][0] << " ";
  }
  */

  rec(0);
  // cout << rec(0) << endl;
  for (int i = 0; i != -1; i = memo[i].second) {
    cout << (char)(memo[i].first + 'a');
  }
  cout << endl;
}
Problem p03629 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long lli;
typedef vector<lli> vll;
typedef vector<bool> vbl;
typedef vector<vector<lli>> mat;
typedef vector<vector<bool>> matb;
typedef vector<string> vst;
typedef pair<lli, lli> pll;
typedef pair<double, double> pdd;
typedef vector<map<lli, lli>> graph;

string a;
vll dp;
vbl used;
vector<lli> r;
vector<char> ch;
mat m;

lli solve(lli x) {
  if (used[x])
    return dp[x];
  used[x] = true;
  for (char c = 'a'; c <= 'z'; c++) {
    lli y;
    if (m[x][c] == a.size())
      y = 1;
    else
      y = 1 + solve(m[x][c] + 1);
    if (dp[x] > y) {
      dp[x] = y;
      r[x] = m[x][c] + 1;
      ch[x] = c;
    }
  }
  return dp[x];
}

int main() {
  cin >> a;
  m = mat(a.size() + 1, vll(256, a.size()));
  for (lli i = a.size() - 1; i >= 0; i--) {
    for (char c = 'a'; c <= 'z'; c++) {
      if (a[i] == c) {
        m[i][c] = i;
      } else {
        m[i][c] = m[i + 1][c];
      }
    }
  }
  used = vbl(a.size() + 1);
  dp = vll(a.size() + 1, INT_MAX);
  r = vll(a.size() + 1);
  ch = vector<char>(a.size() + 1);
  solve(0);
  for (lli i = 0; i < a.size();) {
    cout << ch[i];
    i = r[i];
  }
  cout << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long lli;
typedef vector<lli> vll;
typedef vector<bool> vbl;
typedef vector<vector<lli>> mat;
typedef vector<vector<bool>> matb;
typedef vector<string> vst;
typedef pair<lli, lli> pll;
typedef pair<double, double> pdd;
typedef vector<map<lli, lli>> graph;

string a;
vll dp;
vbl used;
vector<lli> r;
vector<char> ch;
mat m;

lli solve(lli x) {
  if (used[x])
    return dp[x];
  used[x] = true;
  for (char c = 'a'; c <= 'z'; c++) {
    lli y;
    if (m[x][c] == a.size())
      y = 1;
    else
      y = 1 + solve(m[x][c] + 1);
    if (dp[x] > y) {
      dp[x] = y;
      r[x] = m[x][c] + 1;
      ch[x] = c;
    }
  }
  return dp[x];
}

int main() {
  cin >> a;
  m = mat(a.size() + 1, vll(128, a.size()));
  for (lli i = a.size() - 1; i >= 0; i--) {
    for (char c = 'a'; c <= 'z'; c++) {
      if (a[i] == c) {
        m[i][c] = i;
      } else {
        m[i][c] = m[i + 1][c];
      }
    }
  }
  used = vbl(a.size() + 1);
  dp = vll(a.size() + 1, INT_MAX);
  r = vll(a.size() + 1);
  ch = vector<char>(a.size() + 1);
  solve(0);
  for (lli i = 0; i < a.size();) {
    cout << ch[i];
    i = r[i];
  }
  cout << endl;
  return 0;
}
Problem p03629 (C++)

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

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 (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

using ll = long long;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int INF = 1 << 30;
const ll mod = 1000000007LL;

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

int main() {
  string S;
  cin >> S;
  int N = S.size();
  int cnt = 0;
  stack<int> memo;
  memo.push(N);
  vector<bool> check(26);
  for (int i = N - 1; i >= 0; i--) {
    int t = S[i] - 'a';
    if (!check[t]) {
      cnt++;
      check[t] = true;
    }
    if (cnt == 26) {
      for (int j = 0; j < 26; j++)
        check[j] = false;
      cnt = 0;
      memo.push(i);
    }
  }
  int p = 0;
  vector<char> ans;
  while (!memo.empty()) {
    int t = memo.top();
    memo.pop();
    vector<bool> alph(26, false);
    for (int i = p; i < t; i++) {
      int s = S[i] - 'a';
      alph[s] = true;
    }
    for (int i = 0; i < 26; i++) {
      if (!alph[i]) {
        char c = 'a' + i;
        ans.push_back(c);
        break;
      }
    }
    while (true) {
      if (S[t] == *ans.rbegin()) {
        p = t + 1;
        break;
      }
      t++;
    }
  }
  for (auto x : ans)
    cout << x;
  cout << endl;
}

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

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 (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

using ll = long long;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int INF = 1 << 30;
const ll mod = 1000000007LL;

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

int main() {
  string S;
  cin >> S;
  int N = S.size();
  int cnt = 0;
  stack<int> memo;
  memo.push(N);
  vector<bool> check(26);
  for (int i = N - 1; i >= 0; i--) {
    int t = S[i] - 'a';
    if (!check[t]) {
      cnt++;
      check[t] = true;
    }
    if (cnt == 26) {
      for (int j = 0; j < 26; j++)
        check[j] = false;
      cnt = 0;
      memo.push(i);
    }
  }
  int p = 0;
  vector<char> ans;
  while (!memo.empty()) {
    int t = memo.top();
    memo.pop();
    vector<bool> alph(26, false);
    for (int i = p; i < t; i++) {
      int s = S[i] - 'a';
      alph[s] = true;
    }
    for (int i = 0; i < 26; i++) {
      if (!alph[i]) {
        char c = 'a' + i;
        ans.push_back(c);
        break;
      }
    }
    while (true && t < N) {
      if (S[t] == *ans.rbegin()) {
        p = t + 1;
        break;
      }
      t++;
    }
  }
  for (auto x : ans)
    cout << x;
  cout << endl;
}
Problem p03629 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
char s[100005];
int n, f[100005], pos[100005][26];
int main() {
  scanf("%s", s);
  int n = strlen(s);
  f[n] = 1;
  for (int i = 0; i < 26; i++)
    pos[n][i] = n;
  for (int i = n - 1; i >= 0; i--) {
    for (int j = 0; j < 26; j++)
      pos[i][j] = pos[i + 1][j];
    pos[i][s[i] - 'a'] = i;
    f[i] = n;
    for (int j = 0; j < 26; j++)
      f[i] = std::min(f[i], f[pos[i][j] + 1] + 1);
  }
  int p = 0;
  for (int i = f[0]; i; i--) {
    for (int j = 0; j < 26; j++)
      if (f[pos[p][j] + 1] + 1 == i) {
        putchar(j + 'a');
        p = pos[p][j] + 1;
        break;
      }
  }
}

Fixed submission:
#include <bits/stdc++.h>
const int N = 200005;
char s[N];
int n, f[N], pos[N][26];
int main() {
  scanf("%s", s);
  int n = strlen(s);
  f[n] = 1;
  for (int i = 0; i < 26; i++)
    pos[n][i] = n;
  for (int i = n - 1; i >= 0; i--) {
    for (int j = 0; j < 26; j++)
      pos[i][j] = pos[i + 1][j];
    pos[i][s[i] - 'a'] = i;
    f[i] = n;
    for (int j = 0; j < 26; j++)
      f[i] = std::min(f[i], f[pos[i][j] + 1] + 1);
  }
  int p = 0;
  for (int i = f[0]; i; i--) {
    for (int j = 0; j < 26; j++)
      if (f[pos[p][j] + 1] + 1 == i) {
        putchar(j + 'a');
        p = pos[p][j] + 1;
        break;
      }
  }
}
Problem p03629 (C++)

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

using namespace std;

const int maxN = 1e5 + 10;

#define FORU(i, l, r) for (int i = l; i <= r; ++i)
#define FORD(i, r, l) for (int i = r; i >= l; --i)
#define REPU(i, r) for (int i = 0; i < r; ++i)
#define LL long long
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define PINT pair<int, int>
#define BIT(x, i) ((x >> i) & 1)
#define NUM_SET_BIT __builtin_popcountll

int f[maxN], pos[26], nxt[maxN][26];
string s;

int dp(int i) {
  if (i >= s.length())
    return 0;
  if (f[i] != -1)
    return f[i];
  f[i] = s.length() + 1;
  REPU(j, 26) { f[i] = min(f[i], 1 + dp(nxt[i][j])); }
  return f[i];
}

void trace(int i) {
  if (i >= s.length())
    return;
  REPU(j, 26)
  if (f[i] == dp(nxt[i][j]) + 1) {
    printf("%c", char(j + 'a'));
    trace(nxt[i][j]);
    return;
  }
}

int main() {
  cin >> s;
  s = "a" + s;
  REPU(i, 26) pos[i] = s.length();
  FORD(i, s.length() - 1, 0) {
    REPU(j, 26) nxt[i][j] = pos[j];
    pos[s[i] - 'a'] = i;
  }
  memset(f, 255, sizeof(f));
  dp(0);
  trace(0);
}


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

using namespace std;

const int maxN = 2e5 + 10;

#define FORU(i, l, r) for (int i = l; i <= r; ++i)
#define FORD(i, r, l) for (int i = r; i >= l; --i)
#define REPU(i, r) for (int i = 0; i < r; ++i)
#define LL long long
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define PINT pair<int, int>
#define BIT(x, i) ((x >> i) & 1)
#define NUM_SET_BIT __builtin_popcountll

int f[maxN], pos[26], nxt[maxN][26];
string s;

int dp(int i) {
  if (i >= s.length())
    return 0;
  if (f[i] != -1)
    return f[i];
  f[i] = s.length() + 1;
  REPU(j, 26) { f[i] = min(f[i], 1 + dp(nxt[i][j])); }
  return f[i];
}

void trace(int i) {
  if (i >= s.length())
    return;
  REPU(j, 26)
  if (f[i] == dp(nxt[i][j]) + 1) {
    printf("%c", char(j + 'a'));
    trace(nxt[i][j]);
    return;
  }
}

int main() {
  cin >> s;
  s = "a" + s;
  REPU(i, 26) pos[i] = s.length();
  FORD(i, s.length() - 1, 0) {
    REPU(j, 26) nxt[i][j] = pos[j];
    pos[s[i] - 'a'] = i;
  }
  memset(f, 255, sizeof(f));
  dp(0);
  trace(0);
}
Problem p03629 (C++)

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

const ll N = 2e5 + 10;
mat v(26);
ll n;
string s;
ll dp[N];

ll solve(ll idx) {
  if (idx == n) {
    return 0;
  }
  ll &ret = dp[idx];
  if (ret != -1) {
    return ret;
  }
  ret = 1e9;
  for (ll i = 0; i < 26; i++) {
    ll idx2 = upper_bound(v[i].begin(), v[i].end(), idx) - v[i].begin();
    ret = min(ret, solve(v[i][idx2]) + 1);
  }
  return ret;
}

void build(ll idx) {
  if (idx == n) {
    return;
  }
  for (ll i = 0; i < 26; i++) {
    ll idx2 = upper_bound(v[i].begin(), v[i].end(), idx) - v[i].begin();
    if (solve(idx) == solve(v[i][idx]) + 1) {
      cout << char(i + 'a');
      build(v[i][idx2]);
      break;
    }
  }
}

int main() {
  memset(dp, -1, sizeof(dp));
  cin >> s;
  s = "#" + s;
  n = s.size();
  for (ll i = 1; i < n; i++) {
    v[s[i] - 'a'].push_back(i);
  }
  for (ll i = 0; i < 26; i++) {
    v[i].push_back(n);
  }
  build(0);
}

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

const ll N = 2e5 + 10;
mat v(26);
ll n;
string s;
ll dp[N];

ll solve(ll idx) {
  if (idx == n) {
    return 0;
  }
  ll &ret = dp[idx];
  if (ret != -1) {
    return ret;
  }
  ret = 1e9;
  for (ll i = 0; i < 26; i++) {
    ll idx2 = upper_bound(v[i].begin(), v[i].end(), idx) - v[i].begin();
    ret = min(ret, solve(v[i][idx2]) + 1);
  }
  return ret;
}

void build(ll idx) {
  if (idx == n) {
    return;
  }
  for (ll i = 0; i < 26; i++) {
    ll idx2 = upper_bound(v[i].begin(), v[i].end(), idx) - v[i].begin();
    if (solve(idx) == solve(v[i][idx2]) + 1) {
      cout << (char)(i + 'a');
      build(v[i][idx2]);
      break;
    }
  }
}

int main() {
  memset(dp, -1, sizeof(dp));
  cin >> s;
  s = "#" + s;
  n = s.size();
  for (ll i = 1; i < n; i++) {
    v[s[i] - 'a'].push_back(i);
  }
  for (ll i = 0; i < 26; i++) {
    v[i].push_back(n);
  }
  build(0);
}
Problem p03629 (C++)

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

const int N = 2e5 + 5;
const int INF = 0x3f3f3f3f;

char s[N];
int dp[N], nxt[N], f[30], p[30];

int main() {
  memset(dp, INF, sizeof(dp));
  scanf("%s", s);
  int l = strlen(s);
  for (int i = 0; i < 26; i++) {
    dp[l + i] = f[i] = 0;
    p[i] = l + i;
    nxt[l + i] = -1;
    s[l + i] = i + 'a';
  }
  for (int i = l - 1; i >= 0; i--) {
    int c = s[i] - 'a';
    for (int j = 0; j < 26; j++) {
      if (dp[p[j]] + 1 < dp[i]) {
        dp[i] = dp[p[j]] + 1;
        nxt[i] = p[j];
        f[c] = dp[i];
      }
    }
    p[c] = i;
  }
  int j = 0;
  for (int i = 0; i < 26; i++)
    if (f[i] < f[j])
      j = i;
  int x = p[j];
  while (x != -1) {
    printf("%c", s[x]);
    x = nxt[x];
  }
  return 0;
}


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

const int N = 2e5 + 35;
const int INF = 0x3f3f3f3f;

char s[N];
int dp[N], nxt[N], f[30], p[30];

int main() {
  memset(dp, INF, sizeof(dp));
  scanf("%s", s);
  int l = strlen(s);
  for (int i = 0; i < 26; i++) {
    dp[l + i] = f[i] = 0;
    p[i] = l + i;
    nxt[l + i] = -1;
    s[l + i] = i + 'a';
  }
  for (int i = l - 1; i >= 0; i--) {
    int c = s[i] - 'a';
    for (int j = 0; j < 26; j++) {
      if (dp[p[j]] + 1 < dp[i]) {
        dp[i] = dp[p[j]] + 1;
        nxt[i] = p[j];
        f[c] = dp[i];
      }
    }
    p[c] = i;
  }
  int j = 0;
  for (int i = 0; i < 26; i++)
    if (f[i] < f[j])
      j = i;
  int x = p[j];
  while (x != -1) {
    printf("%c", s[x]);
    x = nxt[x];
  }
  return 0;
}
Problem p03629 (C++)

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

using namespace std;

using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;

int dp[101010] = {0};
char bdp[101010] = {0};
int bbdp[101010] = {0};

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

  string s;
  cin >> s;
  int n = s.size();
  vector<int> pos(26, n + 1);
  dp[n] = 1;
  dp[n + 1] = 0;
  bdp[n] = 'a';
  bbdp[n] = n + 1;
  for (int i = n - 1; i >= 0; i--) {
    pos[s[i] - 'a'] = i + 1;
    char small;
    int next = n, index;
    for (int j = 0; j < 26; j++) {
      if (dp[pos[j]] < next) {
        next = dp[pos[j]];
        small = 'a' + j;
        index = pos[j];
      }
    }
    dp[i] = next + 1;
    bdp[i] = small;
    bbdp[i] = index;
  }
  int i = 0;
  while (i <= n) {
    cout << bdp[i];
    i = bbdp[i];
  }
  cout << endl;

  return 0;
}


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

using namespace std;

using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;

int dp[201010] = {0};
char bdp[201010] = {0};
int bbdp[201010] = {0};

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

  string s;
  cin >> s;
  int n = s.size();
  vector<int> pos(26, n + 1);
  dp[n] = 1;
  dp[n + 1] = 0;
  bdp[n] = 'a';
  bbdp[n] = n + 1;
  for (int i = n - 1; i >= 0; i--) {
    pos[s[i] - 'a'] = i + 1;
    char small;
    int next = n, index;
    for (int j = 0; j < 26; j++) {
      if (dp[pos[j]] < next) {
        next = dp[pos[j]];
        small = 'a' + j;
        index = pos[j];
      }
    }
    dp[i] = next + 1;
    bdp[i] = small;
    bbdp[i] = index;
  }
  int i = 0;
  while (i <= n) {
    cout << bdp[i];
    i = bbdp[i];
  }
  cout << endl;

  return 0;
}
Problem p03629 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;

const int N = 100005;

int n, k;
int dp[N], f[N], g[N], a[N];
char s[N];

int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  dp[n + 1] = 1;
  f[n + 1] = n + 2;
  for (int i = 1; i <= 26; i++)
    a[i] = n + 1;
  for (int i = n; i; i--) {
    g[i] = 1;
    a[s[i] - 'a' + 1] = i;
    for (int j = 2; j <= 26; j++)
      if (dp[a[j] + 1] < dp[a[g[i]] + 1])
        g[i] = j;
    dp[i] = dp[a[g[i]] + 1] + 1;
    f[i] = a[g[i]];
  }
  k = 1;
  while (k <= n) {
    printf("%c", g[k] + 'a' - 1);
    k = f[k] + 1;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;

const int N = 200005;

int n, k;
int dp[N], f[N], g[N], a[N];
char s[N];

int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  dp[n + 1] = 1;
  f[n + 1] = n + 2;
  for (int i = 1; i <= 26; i++)
    a[i] = n + 1;
  for (int i = n; i; i--) {
    g[i] = 1;
    a[s[i] - 'a' + 1] = i;
    for (int j = 2; j <= 26; j++)
      if (dp[a[j] + 1] < dp[a[g[i]] + 1])
        g[i] = j;
    dp[i] = dp[a[g[i]] + 1] + 1;
    f[i] = a[g[i]];
  }
  k = 1;
  while (k <= n) {
    printf("%c", g[k] + 'a' - 1);
    k = f[k] + 1;
  }
  return 0;
}
Problem p03629 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
std::vector<int> alpha[26];
std::string S, ans;
bool had[26];
int suff[100010];
int main() {
  std::cin >> S;
  int sf = 0;
  int ac = 1;
  for (int i = S.size() - 1; i >= 0; i--) {
    alpha[S[i] - 'a'].emplace_back(i + 1);
    suff[i + 1] = ac;
    if (!had[S[i] - 'a']) {
      had[S[i] - 'a'] = true;
      sf++;
    }
    if (sf == 26) {
      for (int j = 0; j < 26; j++)
        had[j] = false;
      sf = 0;
      ac++;
    }
  }
  for (int i = 0; i < 26; i++) {
    alpha[i].emplace_back(S.size() + 1);
    std::sort(alpha[i].begin(), alpha[i].end());
  }
  int now = 0;
  suff[0] = ac;
  for (int i = 1; i <= ac; ac--) {
    for (int j = 0; j < 26; j++) {
      auto it = std::upper_bound(alpha[j].begin(), alpha[j].end(), now);
      if (suff[now] - 1 == suff[*it]) {
        ans += ('a' + j);
        now = *it;
        break;
      }
    }
  }
  std::cout << ans << std::endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
std::vector<int> alpha[26];
std::string S, ans;
bool had[26];
int suff[200010];
int main() {
  std::cin >> S;
  int sf = 0;
  int ac = 1;
  for (int i = S.size() - 1; i >= 0; i--) {
    alpha[S[i] - 'a'].emplace_back(i + 1);
    suff[i + 1] = ac;
    if (!had[S[i] - 'a']) {
      had[S[i] - 'a'] = true;
      sf++;
    }
    if (sf == 26) {
      for (int j = 0; j < 26; j++)
        had[j] = false;
      sf = 0;
      ac++;
    }
  }
  for (int i = 0; i < 26; i++) {
    alpha[i].emplace_back(S.size() + 1);
    std::sort(alpha[i].begin(), alpha[i].end());
  }
  int now = 0;
  suff[0] = ac;
  for (int i = 1; i <= ac; ac--) {
    for (int j = 0; j < 26; j++) {
      auto it = std::upper_bound(alpha[j].begin(), alpha[j].end(), now);
      if (suff[now] - 1 == suff[*it]) {
        ans += ('a' + j);
        now = *it;
        break;
      }
    }
  }
  std::cout << ans << std::endl;
  return 0;
}
Problem p03629 (C++)

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

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;

#define fill(a, x) memset(a, x, sizeof(a))
#define pb push_back
#define sz(x) (int)x.size()
#define F first
#define S second
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define NFOR(i, a, b) for (int i = a; i >= b; --i)
#define fast ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)
const ll INF = 1e18;
const ll mod = 1e9 + 7;
const int N = 1e5 + 10;

int nxt[N][26];
int dp[N];
int main() {
  fast;
  string s;
  cin >> s;

  FOR(i, 0, 25)
  nxt[sz(s)][i] = sz(s);

  NFOR(i, sz(s) - 1, 0) {
    FOR(j, 0, 25) {
      if (s[i] - 'a' == j)
        nxt[i][j] = i;
      else
        nxt[i][j] = nxt[i + 1][j];
    }
  }

  dp[sz(s)] = 1;
  dp[sz(s) + 1] = 0;
  NFOR(i, sz(s) - 1, 0) {
    dp[i] = sz(s) - i;
    FOR(j, 0, 25) { dp[i] = min(dp[i], dp[nxt[i][j] + 1] + 1); }
    // cout<<i<<" "<<dp[i]<<endl;
  }

  int cur = 0;
  string ans = "";
  while (cur < sz(s)) {
    FOR(j, 0, 25) {
      if (dp[cur] == dp[nxt[cur][j] + 1] + 1) {
        // cout<<j<<" "<<nxt[cur][j]<<endl;
        ans += 'a' + j;
        cur = nxt[cur][j] + 1;
        break;
      }
    }
  }

  cout << ans;
  return 0;
}

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

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;

#define fill(a, x) memset(a, x, sizeof(a))
#define pb push_back
#define sz(x) (int)x.size()
#define F first
#define S second
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define NFOR(i, a, b) for (int i = a; i >= b; --i)
#define fast ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)
const ll INF = 1e18;
const ll mod = 1e9 + 7;
const int N = 2e5 + 10;

int nxt[N][26];
int dp[N];
int main() {
  fast;
  string s;
  cin >> s;

  FOR(i, 0, 25)
  nxt[sz(s)][i] = sz(s);

  NFOR(i, sz(s) - 1, 0) {
    FOR(j, 0, 25) {
      if (s[i] - 'a' == j)
        nxt[i][j] = i;
      else
        nxt[i][j] = nxt[i + 1][j];
    }
  }

  dp[sz(s)] = 1;
  dp[sz(s) + 1] = 0;
  NFOR(i, sz(s) - 1, 0) {
    dp[i] = sz(s) - i;
    FOR(j, 0, 25) { dp[i] = min(dp[i], dp[nxt[i][j] + 1] + 1); }
    // cout<<i<<" "<<dp[i]<<endl;
  }

  int cur = 0;
  string ans = "";
  while (cur < sz(s)) {
    FOR(j, 0, 25) {
      if (dp[cur] == dp[nxt[cur][j] + 1] + 1) {
        // cout<<j<<" "<<nxt[cur][j]<<endl;
        ans += 'a' + j;
        cur = nxt[cur][j] + 1;
        break;
      }
    }
  }

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

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

using namespace std;

const int INF = 1 << 30;

string S;
vector<int> app[26];
int dp[8000], pre[8000], st[8000];

int rec(int idx) {
  if (idx == S.size() + 1)
    return (0);
  if (~dp[idx])
    return (dp[idx]);
  int ret = INF, nt = -1;
  for (int j = 0; j < 26; j++) {
    auto pos = rec(*upper_bound(begin(app[j]), end(app[j]), idx)) + 1;
    if (pos < ret) {
      ret = pos;
      nt = j;

      dp[idx] = ret;
      pre[idx] = nt;
      st[idx] = *upper_bound(begin(app[j]), end(app[j]), idx);
    }
  }
  return (ret);
}

void rec2(int idx) {
  if (pre[idx] == -1)
    return;
  cout << (char)(pre[idx] + 'a');
  rec2(st[idx]);
}

int main() {
  cin >> S;
  for (int i = 0; i < S.size(); i++) {
    app[S[i] - 'a'].push_back(i + 1);
  }
  for (int i = 0; i < 26; i++) {
    app[i].push_back((int)S.size() + 1);
  }

  memset(dp, -1, sizeof(dp));
  memset(pre, -1, sizeof(pre));
  cerr << rec(0) << endl;
  rec2(0);
  cout << endl;
}

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

using namespace std;

const int INF = 1 << 30;

string S;
vector<int> app[26];
int dp[300000], pre[300000], st[300000];

int rec(int idx) {
  if (idx == S.size() + 1)
    return (0);
  if (~dp[idx])
    return (dp[idx]);
  int ret = INF, nt = -1;
  for (int j = 0; j < 26; j++) {
    auto pos = rec(*upper_bound(begin(app[j]), end(app[j]), idx)) + 1;
    if (pos < ret) {
      ret = pos;
      nt = j;

      dp[idx] = ret;
      pre[idx] = nt;
      st[idx] = *upper_bound(begin(app[j]), end(app[j]), idx);
    }
  }
  return (ret);
}

void rec2(int idx) {
  if (pre[idx] == -1)
    return;
  cout << (char)(pre[idx] + 'a');
  rec2(st[idx]);
}

int main() {
  cin >> S;
  for (int i = 0; i < S.size(); i++) {
    app[S[i] - 'a'].push_back(i + 1);
  }
  for (int i = 0; i < 26; i++) {
    app[i].push_back((int)S.size() + 1);
  }

  memset(dp, -1, sizeof(dp));
  memset(pre, -1, sizeof(pre));
  cerr << rec(0) << endl;
  rec2(0);
  cout << endl;
}
Problem p03629 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define LL long long
#define PII pair<int, int>
using namespace std;
const int MAXN = 100005;
const int INF = (LL)1e9 + 5;

string s;
int dp[MAXN][30];
char add[MAXN][30] = {0};

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> s;
  for (int i = 0; i < 26; i++) {
    dp[s.size()][i] = 1;
  }

  for (int i = s.size() - 1; i >= 0; i--) {
    int pos = min_element(dp[i + 1], dp[i + 1] + 26) - dp[i + 1];
    int len = dp[i + 1][pos];
    //		cout << "LEN " << i + 1 << " = " << len << "\n";
    for (int j = 0; j < 26; j++) {
      if (s[i] == j + 'a') {
        dp[i][j] = len + 1;
        add[i][j] = pos + 'a';
        //				cout << i << " " << (char)('a' + j) << " comes
        //from " << len << " " << (char)('a' + pos) << "\n";
      } else {
        dp[i][j] = dp[i + 1][j];
        add[i][j] = 0;
      }
    }
  }

  string ans;
  int cur = min_element(dp[0], dp[0] + 26) - dp[0];
  ans.push_back(cur + 'a');
  for (int i = 0; i < s.size(); i++) {
    if (add[i][cur] != 0) {
      //			cout << (char)(cur + 'a') << " match at " << i
      //<< " " << s[i] << "\n";
      assert(s[i] == cur + 'a');
      ans.push_back(add[i][cur]);
      cur = add[i][cur] - 'a';
    }
  }

  cout << ans << "\n";

  return 0;

  /**** Note MAXN ****/
}


Fixed submission:
#include <bits/stdc++.h>
#define LL long long
#define PII pair<int, int>
using namespace std;
const int MAXN = 200005;
const int INF = (LL)1e9 + 5;

string s;
int dp[MAXN][30];
char add[MAXN][30] = {0};

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> s;
  for (int i = 0; i < 26; i++) {
    dp[s.size()][i] = 1;
  }

  for (int i = s.size() - 1; i >= 0; i--) {
    int pos = min_element(dp[i + 1], dp[i + 1] + 26) - dp[i + 1];
    int len = dp[i + 1][pos];
    //		cout << "LEN " << i + 1 << " = " << len << "\n";
    for (int j = 0; j < 26; j++) {
      if (s[i] == j + 'a') {
        dp[i][j] = len + 1;
        add[i][j] = pos + 'a';
        //				cout << i << " " << (char)('a' + j) << " comes
        //from " << len << " " << (char)('a' + pos) << "\n";
      } else {
        dp[i][j] = dp[i + 1][j];
        add[i][j] = 0;
      }
    }
  }

  string ans;
  int cur = min_element(dp[0], dp[0] + 26) - dp[0];
  ans.push_back(cur + 'a');
  for (int i = 0; i < s.size(); i++) {
    if (add[i][cur] != 0) {
      //			cout << (char)(cur + 'a') << " match at " << i
      //<< " " << s[i] << "\n";
      assert(s[i] == cur + 'a');
      ans.push_back(add[i][cur]);
      cur = add[i][cur] - 'a';
    }
  }

  cout << ans << "\n";

  return 0;

  /**** Note MAXN ****/
}
Problem p03629 (C++)

Buggy submission (status: Runtime Error):
// IOI 2021

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

#define sync ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define endl "\n"
#define ends ' '
#define die(x) return cout << x << endl, 0
#define all(v) v.begin(), v.end()
#define sz(x) (int)(x.size())
#define debug(x) cerr << #x << ": " << x << endl
#define debugP(p)                                                              \
  cerr << #p << ": {" << p.first << ", " << p.second << '}' << endl
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll INF = 1e9, MOD = 1e6 + 7;

/////////////////////////////////////////////////////////////////////

const int N = 1e5 + 5, C = 26;

int NXT[N][C], DP[N], PAR[N];

int main() {

  sync;

  string s;
  cin >> s;
  int n = sz(s);

  for (int i = 0; i < N; i++)
    DP[i] = INF;
  for (int c = 0; c < C; c++)
    NXT[n][c] = n;
  for (int i = n - 1; i >= 0; i--) {
    for (int c = 0; c < C; c++)
      NXT[i][c] = NXT[i + 1][c];
    NXT[i][s[i] - 'a'] = i;
  }
  DP[n + 1] = 0;
  for (int i = n - 1; i >= 0; i--) {
    for (int c = 0; c < C; c++)
      if (DP[i] > DP[NXT[i][c] + 1] + 1) {
        DP[i] = DP[NXT[i][c] + 1] + 1;
        PAR[i] = c;
      }
  }
  for (int i = 0; i < n; i = NXT[i][PAR[i]] + 1)
    cout << (char)('a' + PAR[i]);
  cout << endl;

  return 0;
}


Fixed submission:
// IOI 2021

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

#define sync ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define endl "\n"
#define ends ' '
#define die(x) return cout << x << endl, 0
#define all(v) v.begin(), v.end()
#define sz(x) (int)(x.size())
#define debug(x) cerr << #x << ": " << x << endl
#define debugP(p)                                                              \
  cerr << #p << ": {" << p.first << ", " << p.second << '}' << endl
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll INF = 1e9, MOD = 1e6 + 7;

/////////////////////////////////////////////////////////////////////

const int N = 2e5 + 5, C = 26;

int NXT[N][C], DP[N], PAR[N];

int main() {

  sync;

  string s;
  cin >> s;
  int n = sz(s);

  for (int i = 0; i < N; i++)
    DP[i] = INF;
  for (int c = 0; c < C; c++)
    NXT[n][c] = n;
  for (int i = n - 1; i >= 0; i--) {
    for (int c = 0; c < C; c++)
      NXT[i][c] = NXT[i + 1][c];
    NXT[i][s[i] - 'a'] = i;
  }
  DP[n + 1] = 0;
  for (int i = n - 1; i >= 0; i--) {
    for (int c = 0; c < C; c++)
      if (DP[i] > DP[NXT[i][c] + 1] + 1) {
        DP[i] = DP[NXT[i][c] + 1] + 1;
        PAR[i] = c;
      }
  }
  for (int i = 0; i < n; i = NXT[i][PAR[i]] + 1)
    cout << (char)('a' + PAR[i]);
  cout << endl;

  return 0;
}