Problem p00795 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define M 1000005
#define N 300
using namespace std;
string s, t, temp;
int scnt[N], tcnt[N], ans[M], B[M], E[M];

int main() {
  while (1) {
    getline(cin, temp);
    if (temp == "")
      break;
    s = "";
    s += temp;
    while (1) {
      getline(cin, temp);
      s += temp;
      if (temp == "")
        break;
    }
    getline(cin, temp);
    t = temp;
    getline(cin, temp);
    int flag = 0;
    memset(tcnt, 0, sizeof(tcnt));
    for (int i = 0; i < (int)t.size(); i++)
      tcnt[(int)t[i]]++;
    int cnt = 0, idx = 0;
    memset(scnt, 0, sizeof(scnt));
    memset(ans, 0, sizeof(ans));
    for (int i = 0; i < (int)s.size(); i++) {
      if (scnt[(int)s[i]] < tcnt[(int)s[i]])
        cnt++;
      scnt[(int)s[i]]++;
      if (cnt == t.size()) {
        flag = 1;
        if (!ans[i - idx + 1]) {
          B[i - idx + 1] = idx;
          E[i - idx + 1] = i;
          ans[i - idx + 1]++;
        } else
          ans[i - idx + 1]++;
        while (1) {
          if (scnt[(int)s[idx]] == tcnt[(int)s[idx]]) {
            scnt[(int)s[idx]]--;
            idx++;
            cnt--;
            break;
          }
          scnt[(int)s[idx]]--;
          idx++;
          if (cnt == t.size()) {
            if (!ans[i - idx + 1]) {
              B[i - idx + 1] = idx;
              E[i - idx + 1] = i;
              ans[i - idx + 1]++;
            } else
              ans[i - idx + 1]++;
          }
        }
      }
    }
    if (!flag)
      cout << 0 << endl << endl;
    for (int i = 0; i < M; i++)
      if (ans[i]) {
        cout << ans[i] << endl << endl;
        string str = s.substr(B[i], E[i] - B[i] + 1);
        for (int j = 0; j < str.size(); j++) {
          cout << str[j];
          if (!((j + 1) % 72))
            cout << endl;
        }
        if (str.size() % 72)
          cout << endl;
        cout << endl;
        break;
      }
  }
  return 0;
}

Error: -6
f7e5688a-9e51-4c21-8ad8-e7eadfefe7e3.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p00795/C++/s838627598.cpp:20: int main(): Assertion `t!=""' failed.


Fixed submission:

#include <bits/stdc++.h>
#define M 1000005
#define N 300
using namespace std;
string s, t, temp;
int scnt[N], tcnt[N], ans[M], B[M], E[M];

int main() {
  while (getline(cin, temp)) {
    s = "";
    s += temp;
    while (1) {
      getline(cin, temp);
      s += temp;
      if (temp == "")
        break;
    }
    getline(cin, temp);
    t = temp;
    getline(cin, temp);
    assert(t != "");
    int flag = 0;
    memset(tcnt, 0, sizeof(tcnt));
    for (int i = 0; i < (int)t.size(); i++)
      tcnt[(int)t[i]]++;
    int cnt = 0, idx = 0;
    memset(scnt, 0, sizeof(scnt));
    memset(ans, 0, sizeof(ans));
    for (int i = 0; i < (int)s.size(); i++) {
      if (scnt[(int)s[i]] < tcnt[(int)s[i]])
        cnt++;
      scnt[(int)s[i]]++;
      if (cnt == t.size()) {
        flag = 1;
        if (!ans[i - idx + 1]) {
          B[i - idx + 1] = idx;
          E[i - idx + 1] = i;
          ans[i - idx + 1]++;
        } else
          ans[i - idx + 1]++;
        while (1) {
          if (scnt[(int)s[idx]] == tcnt[(int)s[idx]]) {
            scnt[(int)s[idx]]--;
            idx++;
            cnt--;
            break;
          }
          scnt[(int)s[idx]]--;
          idx++;
          if (cnt == t.size()) {
            if (!ans[i - idx + 1]) {
              B[i - idx + 1] = idx;
              E[i - idx + 1] = i;
              ans[i - idx + 1]++;
            } else
              ans[i - idx + 1]++;
          }
        }
      }
    }
    if (!flag)
      cout << 0 << endl << endl;
    for (int i = 0; i < M; i++)
      if (ans[i]) {
        cout << ans[i] << endl << endl;
        string str = s.substr(B[i], E[i] - B[i] + 1);
        for (int j = 0; j < str.size(); j++) {
          cout << str[j];
          if (!((j + 1) % 72))
            cout << endl;
        }
        if (str.size() % 72)
          cout << endl;
        cout << endl;
        break;
      }
  }
  return 0;
}