Problem p00795 (C++)

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

int main() {
  string str, pat;
  vector<char> num;

  str.reserve(1000001);
  num.reserve(1000001);
  pat.reserve(73);

  ios::sync_with_stdio(false);
  cin.tie(0);

  while (true) {
    str.clear();
    pat.clear();

    while (true) {
      getline(cin, pat);
      if (pat.empty() || !cin)
        break;
      str += pat;
    }
    if (!cin)
      break;

    getline(cin, pat);
    cin.ignore();

    sort(pat.begin(), pat.end());
    pat.erase(unique(pat.begin(), pat.end()), pat.end());

    num.resize(str.size());
    int j;
    for (j = 0; j < str.size(); ++j) {
      int x = lower_bound(pat.begin(), pat.end(), str[j]) - pat.begin();
      if (x < pat.size() && str[j] == pat[x]) {
        num[j] = x;
      } else {
        num[j] = pat.size();
      }
    }

    int cnt[73] = {};
    cnt[pat.size()] = 10000000;
    int d = 0;
    for (j = 0; j < num.size() && d < pat.size(); ++j) {
      if (++cnt[num[j]] == 1) {
        ++d;
      }
    }

    if (d < pat.size()) {
      cout << "0\n\n";
      continue;
    }

    int a = 0, b = j;
    int shortest = 1;

    for (int i = 0; j <= num.size();) {
      char x = num[i++];
      if (--cnt[x] == 0) {
        bool found = false;
        while (j < num.size()) {
          ++cnt[num[j]];
          if (x == num[j++]) {
            found = true;
            break;
          }
        }
        if (!found)
          break;
      }

      if (b - a > j - i) {
        a = i;
        b = j;
        shortest = 1;
      } else if (b - a == j - i) {
        ++shortest;
      }
    }

    cout << shortest << "\n\n";
    while (a < b) {
      int len = min(b - a, 72);
      cout << str.substr(a, len) << '\n';
      a += len;
    }

    cout << '\n';
  }
}

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

int main() {
  string str, pat;
  vector<char> num;

  str.reserve(1000001);
  num.reserve(1000001);
  pat.reserve(73);

  ios::sync_with_stdio(false);
  cin.tie(0);

  while (true) {
    str.clear();
    pat.clear();

    while (true) {
      getline(cin, pat);
      if (pat.empty() || !cin)
        break;
      str += pat;
    }
    if (str.empty() || !cin)
      break;

    getline(cin, pat);
    cin.ignore();

    sort(pat.begin(), pat.end());
    pat.erase(unique(pat.begin(), pat.end()), pat.end());

    num.resize(str.size());
    int j;
    for (j = 0; j < str.size(); ++j) {
      int x = lower_bound(pat.begin(), pat.end(), str[j]) - pat.begin();
      if (x < pat.size() && str[j] == pat[x]) {
        num[j] = x;
      } else {
        num[j] = pat.size();
      }
    }

    int cnt[73] = {};
    cnt[pat.size()] = 10000000;
    int d = 0;
    for (j = 0; j < num.size() && d < pat.size(); ++j) {
      if (++cnt[num[j]] == 1) {
        ++d;
      }
    }

    if (d < pat.size()) {
      cout << "0\n\n";
      continue;
    }

    int a = 0, b = j;
    int shortest = 1;

    for (int i = 0; j <= num.size();) {
      char x = num[i++];
      if (--cnt[x] == 0) {
        bool found = false;
        while (j < num.size()) {
          ++cnt[num[j]];
          if (x == num[j++]) {
            found = true;
            break;
          }
        }
        if (!found)
          break;
      }

      if (b - a > j - i) {
        a = i;
        b = j;
        shortest = 1;
      } else if (b - a == j - i) {
        ++shortest;
      }
    }

    cout << shortest << "\n\n";
    while (a < b) {
      int len = min(b - a, 72);
      cout << str.substr(a, len) << '\n';
      a += len;
    }

    cout << '\n';
  }
}