Problem p00317 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

#define NUM 50000

int N, M;
string WORD[NUM], rev_WORD[NUM];

int calc(const string slate) {

  int left, right;
  string pattern = slate;

  if (pattern[pattern.length() - 1] == '*') {

    pattern = pattern.substr(0, pattern.length() - 1);
    left = lower_bound(WORD, WORD + N, pattern) - WORD;
    pattern.push_back('z' + 1);
    right = lower_bound(WORD, WORD + N, pattern) - WORD;
    return right - left;

  } else if (pattern[0] == '*') {

    pattern = pattern.substr(1);
    reverse(pattern.begin(), pattern.end());
    left = lower_bound(rev_WORD, rev_WORD + N, pattern) - rev_WORD;
    pattern.push_back('z' + 1);
    right = lower_bound(rev_WORD, rev_WORD + N, pattern) - rev_WORD;
    return right - left;

  } else {

    left = lower_bound(WORD, WORD + N, pattern) - WORD;
    if (WORD[left] == slate) {
      return 1;
    } else {
      return 0;
    }
  }
}

int main() {

  scanf("%d %d", &N, &M);

  getchar();

  for (int i = 0; i < N; i++) {
    getline(cin, WORD[i]);
    rev_WORD[i] = WORD[i];
    reverse(rev_WORD[i].begin(), rev_WORD[i].end());
  }
  sort(WORD, WORD + N);
  sort(rev_WORD, rev_WORD + N);

  string slate;
  int loc, ans;

  for (int loop = 0; loop < M; loop++) {
    getline(cin, slate);

    loc = -1;

    for (int i = 0; i < slate.length(); i++) {
      if (slate[i] == '?') {
        loc = i;
        break;
      }
    }

    if (loc == -1) {
      printf("%d\n", calc(slate));
      continue;
    }

    ans = 0;
    for (int i = 0; i < 26; i++) {
      slate[loc] = 'a' + i;
      ans += calc(slate);
    }

    printf("%d\n", ans);
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

#define NUM 50005

int N, M;
string WORD[NUM], rev_WORD[NUM];

int calc(const string slate) {

  int left, right;
  string pattern = slate;

  if (pattern[pattern.length() - 1] == '*') {

    pattern = pattern.substr(0, pattern.length() - 1);
    left = lower_bound(WORD, WORD + N, pattern) - WORD;
    pattern.push_back('z' + 1);
    right = lower_bound(WORD, WORD + N, pattern) - WORD;
    return right - left;

  } else if (pattern[0] == '*') {

    pattern = pattern.substr(1);
    reverse(pattern.begin(), pattern.end());
    left = lower_bound(rev_WORD, rev_WORD + N, pattern) - rev_WORD;
    pattern.push_back('z' + 1);
    right = lower_bound(rev_WORD, rev_WORD + N, pattern) - rev_WORD;
    return right - left;

  } else {

    left = lower_bound(WORD, WORD + N, pattern) - WORD;
    if (WORD[left] == slate) {
      return 1;
    } else {
      return 0;
    }
  }
}

int main() {

  scanf("%d %d", &N, &M);

  getchar();

  for (int i = 0; i < N; i++) {
    getline(cin, WORD[i]);
    rev_WORD[i] = WORD[i];
    reverse(rev_WORD[i].begin(), rev_WORD[i].end());
  }
  sort(WORD, WORD + N);
  sort(rev_WORD, rev_WORD + N);

  string slate;
  int loc, ans;

  for (int loop = 0; loop < M; loop++) {
    getline(cin, slate);

    loc = -1;

    for (int i = 0; i < slate.length(); i++) {
      if (slate[i] == '?') {
        loc = i;
        break;
      }
    }

    if (loc == -1) {
      printf("%d\n", calc(slate));
      continue;
    }

    ans = 0;
    for (int i = 0; i < 26; i++) {
      slate[loc] = 'a' + i;
      ans += calc(slate);
    }

    printf("%d\n", ans);
  }

  return 0;
}