Problem p02250 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <typeinfo>
#include <vector>
using namespace std;

const int MAX = 1000000;

int n, k;
int rank[MAX + 1];
int preRank[MAX + 1];

bool comp(int x, int y) {
  if (rank[x] == rank[y]) {
    int xx = (x + k <= n ? rank[x + k] : -1);
    int yy = (y + k <= n ? rank[y + k] : -1);
    return xx < yy;
  } else {
    return rank[x] < rank[y];
  }
}

vector<int> makeSA(string &T) {
  n = T.size();
  vector<int> SA(n + 1);

  for (int i = 0; i < n + 1; i++) {
    SA[i] = i;
    rank[i] = (i < n) ? (int)T[i] : -1;
  }

  for (k = 1; k < n + 1; k *= 2) {
    sort(SA.begin(), SA.begin() + n + 1, comp);
    preRank[SA[0]] = 0;
    for (int j = 1; j < n + 1; j++) {
      preRank[SA[j]] = preRank[SA[j - 1]] + (comp(SA[j - 1], SA[j]) ? 1 : 0);
    }
    for (int j = 0; j < n + 1; j++) {
      rank[j] = preRank[j];
    }
  }
  return SA;
}

bool searchSA(string &T, string &Pat, vector<int> &SA) {
  int l = -1;
  int r = T.size();
  while (r - l > 1) {
    int c = (l + r) / 2;
    if (T.compare(SA[c], Pat.length(), Pat) < 0) {
      l = c;
    } else {
      r = c;
    }
  }
  return T.compare(SA[r], Pat.length(), Pat) == 0;
}

int main() {
  string T;
  cin >> T;
  vector<int> SA = makeSA(T);
  int Q;
  cin >> Q;
  string Pat;
  for (int i = 0; i < Q; i++) {
    cin >> Pat;
    cout << searchSA(T, Pat, SA) << endl;
  }

  /*
  for (int i=0;i<SA.size();i++){
      cout << SA[i] << endl;
  }
  */
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <typeinfo>
#include <vector>
using namespace std;

struct Node {
  Node *child[256];
  vector<int> output;
  Node() { memset(child, 0, sizeof(child)); }
  ~Node() {
    for (int i = 0; i < 256; i++)
      if (child[i])
        delete child[i];
  }
};

void concatIntVector(vector<int> &v1, vector<int> &v2) {
  v1.insert(v1.begin(), v2.begin(), v2.end());
}

vector<int> set_union(vector<int> &a, vector<int> &b) {
  vector<int> res;
  set_union(a.begin(), a.end(), b.begin(), b.end(), back_inserter(res));
  return res;
}

Node *makePMA(vector<string> &P) {
  Node *root = new Node;
  root->child[0] = 0;
  Node *now;
  for (int i = 0; i < P.size(); i++) {
    now = root;
    for (int j = 0; j < P[i].size(); j++) {
      if (now->child[(int)P[i][j]] == 0) {
        now->child[(int)P[i][j]] = new Node;
      }
      now = now->child[(int)P[i][j]];
    }
    now->output.push_back(i);
  }

  queue<Node *> qu;
  for (int i = 1; i < 256; i++) {
    if (root->child[i]) {
      root->child[i]->child[0] = root;
      qu.push(root->child[i]);
    } else {
      root->child[i] = root;
    }
  }
  while (!qu.empty()) {
    now = qu.front();
    qu.pop();
    for (int i = 1; i < 256; i++) {
      if (now->child[i]) {
        Node *fail = now->child[0];
        while (!fail->child[i]) {
          fail = fail->child[0];
        }
        now->child[i]->child[0] = fail->child[i];
        now->child[i]->output =
            set_union(now->child[i]->output, fail->child[i]->output);
        qu.push(now->child[i]);
      }
    }
  }
  return root;
}

void searchPMA(Node *&root, string &T, vector<bool> &res) {
  Node *now = root;
  for (int i = 0; i < T.size(); i++) {
    while (!now->child[(int)T[i]]) {
      now = now->child[0];
    }
    now = now->child[(int)T[i]];
    for (int j = 0; j < (now->output).size(); j++) {
      res[(now->output)[j]] = true;
    }
  }
  return;
}

int main() {
  string T;
  cin >> T;
  int Q;
  cin >> Q;
  vector<string> P;
  string tmp;
  for (int i = 0; i < Q; i++) {
    cin >> tmp;
    P.push_back(tmp);
  }
  Node *root = makePMA(P);
  vector<bool> res(Q, false);
  searchPMA(root, T, res);
  for (int i = 0; i < res.size(); i++) {
    cout << res[i] << endl;
  }
  return 0;
}