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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n - 1; i > 0; --i)
#define yesno(flg)                                                             \\
  if (flg) {                                                                   \\
    cout << "Yes" << endl;                                                     \\
  } else {                                                                     \\
    cout << "No" << endl;                                                      \\
  }
#define Vi vector<int>
#define Vd vector<double>
#define Vl vector<long long>
#define pb(a) push_back(a)
using namespace std;

typedef long long ll;
typedef pair<int, int> Pi;
typedef pair<int, Pi> Pii;
typedef pair<ll, int> Pl;
typedef pair<int, Pl> Pil;
const int inf = 1000000000;
const ll inf2 = 1000000000000000;
const int np = 1000000007;
struct edge {
  int myc, to, c, cost;
};
struct pos {
  int to, cost, color;
};
int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};

vector<string> word;
vector<string> rword;

int rev(int high, int low, string str1) {
  if (high - low < 2)
    return low;
  int mid = (high + low) / 2;
  if (word[mid] >= str1) {
    return rev(mid, low, str1);
  } else {
    return rev(high, mid, str1);
  }
}
int revR(int high, int low, string str1) {
  if (high - low < 2)
    return low;
  int mid = (high + low) / 2;
  if (rword[mid] >= str1) {
    return revR(mid, low, str1);
  } else {
    return revR(high, mid, str1);
  }
}

int main() {

  string al = "abcdefghijklmnopqrstuvwxyz{";
  int n, m;
  cin >> n >> m;

  rep(i, n) {
    string str;
    cin >> str;
    word.push_back(str);
    if (str.size() > 1)
      reverse(str.begin(), str.end());
    rword.push_back(str);
  }
  sort(word.begin(), word.end());
  sort(rword.begin(), rword.end());

  rep(i, m) {
    string str;
    int total = 0;
    cin >> str;
    int num = str.length();
    bool flg = true;
    if (str[0] == '*') {
      reverse(str.begin(), str.end());
      rep(j, num) {
        if (flg && (str[j] == '?')) {
          rep(k, 26) {
            str[j] = al[k];
            str.pop_back();
            int it1 = revR(n, -1, str);
            str = str + '{';
            int it2 = revR(n, -1, str);
            total += it2 - it1;
          }
          flg = false;
        }
      }
      if (flg) {
        str.pop_back();
        int it1 = revR(n, -1, str);
        str = str + '{';
        int it2 = revR(n, -1, str);
        total += it2 - it1;
        //				cout<<it2<<" "<<it1<<endl;
      }
    } else {
      rep(j, num) {
        if (str[j] == '?') {
          if (str[num - 1] != '*') {
            rep(k, 26) {
              str[j] = al[k];
              int it1 = rev(n, -1, str);
              if (it1 < n - 1 && word[it1 + 1] == str)
                total++;
            }
          } else {
            rep(k, 26) {
              str[j] = al[k];
              str.pop_back();
              int it1 = rev(n, -1, str);
              str = str + '{';
              int it2 = rev(n, -1, str);
              total += it2 - it1;
              //							cout<<total<<endl;
            }
          }
          flg = false;
        }
      }
      if (flg) {
        //				cout<<str<<endl;

        if (str[num - 1] != '*') {
          int it1 = rev(n, -1, str);
          if (it1 < n - 1 && word[it1 + 1] == str)
            total++;
        } else {
          str.pop_back();
          int it1 = rev(n, -1, str);
          str = str + '{';
          int it2 = rev(n, -1, str);
          total += it2 - it1;
        }
      }
    }
    cout << total << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n - 1; i > 0; --i)
#define yesno(flg)                                                             \\
  if (flg) {                                                                   \\
    cout << "Yes" << endl;                                                     \\
  } else {                                                                     \\
    cout << "No" << endl;                                                      \\
  }
#define Vi vector<int>
#define Vd vector<double>
#define Vl vector<long long>
#define pb(a) push_back(a)
using namespace std;

typedef long long ll;
typedef pair<int, int> Pi;
typedef pair<int, Pi> Pii;
typedef pair<ll, int> Pl;
typedef pair<int, Pl> Pil;
const int inf = 1000000000;
const ll inf2 = 1000000000000000;
const int np = 1000000007;
struct edge {
  int myc, to, c, cost;
};
struct pos {
  int to, cost, color;
};
int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};

vector<string> word;
vector<string> rword;

int rev(int high, int low, string str1) {
  if (high - low < 2)
    return low;
  int mid = (high + low) / 2;
  if (word[mid] >= str1) {
    return rev(mid, low, str1);
  } else {
    return rev(high, mid, str1);
  }
}
int revR(int high, int low, string str1) {
  if (high - low < 2)
    return low;
  int mid = (high + low) / 2;
  if (rword[mid] >= str1) {
    return revR(mid, low, str1);
  } else {
    return revR(high, mid, str1);
  }
}

int main() {

  string al = "abcdefghijklmnopqrstuvwxyz{";
  int n, m;
  cin >> n >> m;

  rep(i, n) {
    string str;
    cin >> str;
    word.push_back(str);
    if (str.size() > 1)
      reverse(str.begin(), str.end());
    rword.push_back(str);
  }
  sort(word.begin(), word.end());
  sort(rword.begin(), rword.end());

  rep(i, m) {
    string str;
    int total = 0;
    cin >> str;
    int num = str.length();
    bool flg = true;
    if (str[0] == '*') {
      reverse(str.begin(), str.end());
      rep(j, num) {
        if (flg && (str[j] == '?')) {
          rep(k, 26) {
            str[j] = al[k];
            str.pop_back();
            int it1 = revR(n, -1, str);
            str = str + '{';
            int it2 = revR(n, -1, str);
            total += it2 - it1;
          }
          flg = false;
        }
      }
      if (flg) {
        str.pop_back();
        int it1 = revR(n, -1, str);
        str = str + '{';
        int it2 = revR(n, -1, str);
        total += it2 - it1;
        //				cout<<it2<<" "<<it1<<endl;
      }
    } else {
      rep(j, num) {
        if (str[j] == '?') {
          if (str[num - 1] != '*') {
            rep(k, 26) {
              str[j] = al[k];
              auto it = lower_bound(word.begin(), word.end(), str);
              if (*it == str)
                total++;
            }
          } else {
            rep(k, 26) {
              str[j] = al[k];
              str.pop_back();
              int it1 = rev(n, -1, str);
              str = str + '{';
              int it2 = rev(n, -1, str);
              total += it2 - it1;
              //							cout<<total<<endl;
            }
          }
          flg = false;
        }
      }
      if (flg) {
        //				cout<<str<<endl;

        if (str[num - 1] != '*') {
          auto it = lower_bound(word.begin(), word.end(), str);
          if (*it == str)
            total++;
        } else {
          str.pop_back();
          int it1 = rev(n, -1, str);
          str = str + '{';
          int it2 = rev(n, -1, str);
          total += it2 - it1;
        }
      }
    }
    cout << total << endl;
  }
  return 0;
}