Problem p03629 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<unsigned char, unsigned char> pcc;
typedef pair<double, double> pdd;
typedef vector<vector<ll>> matrix;

string s;
int dp[200005];
int from[256];
queue<int> allLocs[256];
void solve() {
  cin >> s;
  s = "!" + s;
  dp[s.size()] = 1;
  for (int i = 'a'; i <= 'z'; i++)
    from[i] = s.size() + 1;
  for (int i = s.size() - 1; i >= 0; i--) {
    from[s[i]] = i;
    dp[i] = 1e9;
    for (int a = 'a'; a <= 'z'; a++) {
      if (s[i] == a)
        continue;
      dp[i] = min(dp[i], dp[from[a]] + 1);
    }
  }
  for (int i = 0; i < s.size(); i++)
    allLocs[s[i]].push(i);
  int currIdx = 0;
  while (true) {
    bool printed = false;
    for (int a = 'a'; a <= 'z'; a++) {
      while (allLocs[a].size() && allLocs[a].front() < currIdx)
        allLocs[a].pop();
      if (allLocs[a].empty()) {
        cout << (char)a;
        return;
      }
      int nextIdx = allLocs[a].front() + 1;
      if (dp[nextIdx] == dp[currIdx] - 1) {
        cout << (char)a;
        printed = true;
        currIdx = nextIdx;
        break;
      }
    }
    assert(printed);
  }
  cout << "\n";
}

void casesolve() {
  int t;
  cin >> t;
  for (int i = 1; i <= t; i++) {
    cout << "Case #" << i << ":"
         << "\n";
    solve();
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
  // casesolve();
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<unsigned char, unsigned char> pcc;
typedef pair<double, double> pdd;
typedef vector<vector<ll>> matrix;

string s;
int dp[200005];
int from[256];
queue<int> allLocs[256];
void solve() {
  cin >> s;
  s = "!" + s;
  dp[s.size()] = 1;
  for (int i = 'a'; i <= 'z'; i++)
    from[i] = s.size() + 1;
  for (int i = s.size() - 1; i >= 0; i--) {
    from[s[i]] = i;
    dp[i] = 1e9;
    for (int a = 'a'; a <= 'z'; a++) {
      dp[i] = min(dp[i], dp[from[a] + 1] + 1);
    }
  }
  for (int i = 0; i < s.size(); i++)
    allLocs[s[i]].push(i);
  int currIdx = 0;
  while (true) {
    bool printed = false;
    for (int a = 'a'; a <= 'z'; a++) {
      while (allLocs[a].size() && allLocs[a].front() < currIdx)
        allLocs[a].pop();
      if (allLocs[a].empty()) {
        cout << (char)a;
        return;
      }
      int nextIdx = allLocs[a].front() + 1;
      if (dp[nextIdx] == dp[currIdx] - 1) {
        cout << (char)a;
        printed = true;
        currIdx = nextIdx;
        break;
      }
    }
    assert(printed);
  }
  cout << "\n";
}

void casesolve() {
  int t;
  cin >> t;
  for (int i = 1; i <= t; i++) {
    cout << "Case #" << i << ":"
         << "\n";
    solve();
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
  // casesolve();
}