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

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i, s, e) for (int(i) = (s); (i) <= (e); (i)++)
#define all(x) x.begin(), x.end()

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

int none(string s) {
  return upper_bound(all(word), s) - lower_bound(all(word), s);
}

int left(string s) {
  string t = s;
  t[s.size() - 1]++;
  return lower_bound(all(word), t) - lower_bound(all(word), s);
}

int right(string s) {
  reverse(all(s));
  string t = s;
  t[s.size() - 1]++;
  return lower_bound(all(rword), t) - lower_bound(all(rword), s);
}

int main() {
  int N, M;
  cin >> N >> M;
  word.resize(N);
  rword.resize(N);
  rep(i, 0, N - 1) {
    cin >> word[i];
    rword[i] = word[i];
    reverse(all(rword[i]));
  }
  sort(all(word));
  sort(all(rword));
  rep(i, 0, M - 1) {
    string s;
    cin >> s;
    int ans = 0;
    if (s.find('?') == s.npos) {
      if (s[0] == '*') {
        ans += right(s.substr(1, s.size() - 1));
      } else if (s[s.size() - 1] == '*') {
        ans += left(s.substr(0, s.size() - 1));
      } else {
        ans += none(s);
      }
    } else {
      int idx = s.find('?');
      for (char c = 'a'; c <= 'z'; c++) {
        s[idx] = c;
        if (s[0] == '*') {
          ans += right(s.substr(1, s.size() - 1));
        } else if (s[s.size() - 1] == '*') {
          ans += left(s.substr(0, s.size() - 1));
        } else {
          ans += none(s);
        }
      }
    }
    cout << ans << endl;
  }
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i, s, e) for (int(i) = (s); (i) <= (e); (i)++)
#define all(x) x.begin(), x.end()

int ans[101010];

class Trie {
public:
  Trie *next[26];
  vector<int> idx;
  vector<int> act;

  Trie() { fill(next, next + 26, (Trie *)0); }

  void insert(char *s, int i, int isAct) {
    if (*s == '\\0') {
      if (!isAct)
        idx.push_back(i);
      else {
        act.push_back(i);
      }
      return;
    }
    if (this->next[*s - 'a'] == NULL)
      this->next[*s - 'a'] = new Trie();
    this->next[*s - 'a']->insert(s + 1, i, isAct);
  }

  void find(char *s) {
    for (int i : act) {
      ans[i]++;
    }
    if (*s == '\\0') {
      for (int i : idx) {
        ans[i]++;
      }
    }
    if (this->next[*s - 'a'] == NULL) {
      return;
    }
    return this->next[*s - 'a']->find(s + 1);
  }
};

string word[101010];
string slate[101010];
int isR[101010];

int main() {
  int N, M;
  cin >> N >> M;
  rep(i, 0, N - 1) { cin >> word[i]; }

  Trie trie[26];
  Trie rtrie[26];

  rep(j, 0, M - 1) {
    cin >> slate[j];
    string s = slate[j];
    if (s[0] != '*') {
      if (s.find('?') == s.npos) {
        char b[505];
        for (int i = 1; i < s.size(); i++) {
          if (s[i] == '*') {
            b[i - 1] = '\\0';
            break;
          }
          b[i - 1] = s[i];
        }
        b[s.size()] = '\\0';
        trie[s[0] - 'a'].insert(b, j, s[s.size() - 1] == '*');
      } else {
        int idx = s.find('?');
        for (char c = 'a'; c <= 'z'; c++) {
          s[idx] = c;
          char b[505];
          for (int i = 1; i < s.size(); i++) {
            if (s[i] == '*') {
              b[i - 1] = '\\0';
              break;
            }
            b[i - 1] = s[i];
          }
          b[s.size()] = '\\0';
          trie[s[0] - 'a'].insert(b, j, s[s.size() - 1] == '*');
        }
      }

    } else {
      isR[j] = 1;
      reverse(all(s));
      if (s.find('?') == s.npos) {
        char b[505];
        for (int i = 1; i < s.size(); i++) {
          if (s[i] == '*') {
            b[i - 1] = '\\0';
            break;
          }
          b[i - 1] = s[i];
        }
        b[s.size()] = '\\0';
        rtrie[s[0] - 'a'].insert(b, j, s[s.size() - 1] == '*');
      } else {
        int idx = s.find('?');
        for (char c = 'a'; c <= 'z'; c++) {
          s[idx] = c;
          char b[505];
          for (int i = 1; i < s.size(); i++) {
            if (s[i] == '*') {
              b[i - 1] = '\\0';
              break;
            }
            b[i - 1] = s[i];
          }
          b[s.size()] = '\\0';
          rtrie[s[0] - 'a'].insert(b, j, s[s.size() - 1] == '*');
        }
      }
    }
  }

  rep(i, 0, N - 1) {
    {
      char b[505];
      string s = word[i];
      for (int i = 1; i < s.size(); i++) {
        b[i - 1] = s[i];
      }
      b[s.size()] = '\\0';
      trie[s[0] - 'a'].find(b);
    }
    {
      char b[505];
      string s = word[i];
      reverse(all(s));
      for (int i = 1; i < s.size(); i++) {
        b[i - 1] = s[i];
      }
      b[s.size()] = '\\0';
      rtrie[s[0] - 'a'].find(b);
    }
  }

  rep(i, 0, M - 1) { cout << ans[i] << endl; }
}