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

#include <algorithm>
#include <iostream>
#include <string>
#define rep(i, n) for (i = 0; i < (int)n; i++)
using namespace std;

void reverse(string &s) {
  int l = 0, r = s.length() - 1;
  while (l < r) {
    swap(s[l], s[r]);
    l++;
    r--;
  }
}

int n, m;
string w[50000], wr[50000];
string s;

int solve0(string s) {
  int id = lower_bound(w, w + n, s) - w;
  return id < n && w[id] == s;
}

int solve1(string s) {
  if (s.length() == 1)
    return n;
  string t, u;
  int i;
  rep(i, s.length() - 1) t += s[i];
  u = t;
  u[u.length() - 1] = t[t.length() - 1] + 1;

  int l = lower_bound(w, w + n, t) - w;
  int r = lower_bound(w, w + n, u) - w;
  return r - l;
}

int solve2(string s) {
  reverse(s);
  string t, u;
  int i;
  rep(i, s.length() - 1) t += s[i];
  u = t;
  u[u.length() - 1] = t[t.length() - 1] + 1;

  int l = lower_bound(wr, wr + n, t) - wr;
  int r = lower_bound(wr, wr + n, u) - wr;
  return r - l;
}

int main() {
  int i, j;

  cin >> n >> m;
  rep(i, n) {
    cin >> w[i];
    string tmp = w[i];
    reverse(tmp);
    wr[i] = tmp;
  }
  sort(w, w + n);
  sort(wr, wr + n);
  rep(i, m) {
    cin >> s;
    rep(j, s.length()) if (s[j] == '?') break;
    int pos = j;
    int ans = 0;
    if (pos < s.length()) {
      rep(j, 26) {
        s[pos] = j + 'a';
        if (s[s.length() - 1] == '*')
          ans += solve1(s);
        else if (s[0] == '*')
          ans += solve2(s);
        else
          ans += solve0(s);
      }
    } else {
      if (s[s.length() - 1] == '*')
        ans += solve1(s);
      else if (s[0] == '*')
        ans += solve2(s);
      else
        ans += solve0(s);
    }
    cout << ans << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#define rep(i, n) for (i = 0; i < (int)n; i++)
using namespace std;

void reverse(string &s) {
  int l = 0, r = s.length() - 1;
  while (l < r) {
    swap(s[l], s[r]);
    l++;
    r--;
  }
}

int n, m;
string w[50000], wr[50000];
string s;

int solve0(string s) {
  int id = lower_bound(w, w + n, s) - w;
  return w[id] == s;
}

int solve1(string s) {
  if (s.length() == 1)
    return n;
  string t, u;
  int i;
  rep(i, s.length() - 1) t += s[i];
  u = t;
  u[u.length() - 1] = t[t.length() - 1] + 1;

  int l = lower_bound(w, w + n, t) - w;
  int r = lower_bound(w, w + n, u) - w;
  return r - l;
}

int solve2(string s) {
  reverse(s);
  string t, u;
  int i;
  rep(i, s.length() - 1) t += s[i];
  u = t;
  u[u.length() - 1] = t[t.length() - 1] + 1;

  int l = lower_bound(wr, wr + n, t) - wr;
  int r = lower_bound(wr, wr + n, u) - wr;
  return r - l;
  return 0;
}

int main() {
  int i, j;

  cin >> n >> m;
  rep(i, n) {
    cin >> w[i];
    string tmp = w[i];
    reverse(tmp);
    wr[i] = tmp;
  }
  sort(w, w + n);
  sort(wr, wr + n);
  rep(i, m) {
    cin >> s;
    rep(j, s.length()) if (s[j] == '?') break;
    int pos = j;
    int ans = 0;
    if (pos < s.length()) {
      rep(j, 26) {
        s[pos] = j + 'a';
        if (s[s.length() - 1] == '*')
          ans += solve1(s);
        else if (s[0] == '*')
          ans += solve2(s);
        else
          ans += solve0(s);
      }
    } else {
      if (s[s.length() - 1] == '*')
        ans += solve1(s);
      else if (s[0] == '*')
        ans += solve2(s);
      else
        ans += solve0(s);
    }
    cout << ans << endl;
  }
  return 0;
}