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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define rep(i, a) REP(i, 0, a)
#define REP(i, a, b) for (int i = a; i < b; ++i)
#define rrep(i, a) RREP(i, a, 0)
#define RREP(i, a, b) for (int i = a; i >= b; --i)
#define repll(i, a) REPLL(i, 0, a)
#define REPLL(i, a, b) for (ll i = a; i < b; ++i)
#define rrepll(i, a) RREPLL(i, a, 0)
#define RREPLL(i, a, b) for (ll i = a; i >= b; --i)

typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int, int> P;
typedef std::pair<int, P> PP;
const double PI = 3.14159265358979323846;
const double esp = 1e-9;
const int infi = (int)1e+9 + 10;
const ll infll = (ll)1e+18 + 10;

const int max = 1000005;

std::string text;
int textSize, k = 1;
int rank[max + 1], tmp[max + 1], sa[max + 1];

bool compare_sa(int i, int j) {
  if (rank[i] != rank[j])
    return rank[i] < rank[j];
  else {
    int ri = (i + k <= textSize ? rank[i + k] : -1);
    int rj = (j + k <= textSize ? rank[j + k] : -1);
    return ri < rj;
  }
}

void construct_sa() {
  textSize = text.size();
  rep(i, textSize + 1) {
    sa[i] = i;
    rank[i] = (i < textSize ? text[i] : -1);
  }

  for (; k <= textSize; k *= 2) {
    std::sort(sa, sa + textSize + 1, compare_sa);

    tmp[sa[0]] = 0;
    REP(j, 1, textSize + 1)
    tmp[sa[j]] = tmp[sa[j - 1]] + (compare_sa(sa[j - 1], sa[j]) ? 1 : 0);
    rep(j, textSize + 1) rank[j] = tmp[j];
  }
}

bool find(std::string t) {
  int l = 0, r = text.size();
  while (l <= r) {
    int m = (l + r) / 2;
    if (text.compare(sa[m], t.size(), t) < 0)
      l = m + 1;
    else
      r = m - 1;
  }
  return (text.compare(sa[r + 1], t.size(), t) == 0);
}

int n;

int main() {
  std::cin >> text >> n;
  construct_sa();
  rep(i, n) {
    std::string str;
    std::cin >> str;
    std::cout << (find(str) ? 1 : 0) << std::endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define rep(i, a) REP(i, 0, a)
#define REP(i, a, b) for (int i = a; i < b; ++i)
#define rrep(i, a) RREP(i, a, 0)
#define RREP(i, a, b) for (int i = a; i >= b; --i)
#define repll(i, a) REPLL(i, 0, a)
#define REPLL(i, a, b) for (ll i = a; i < b; ++i)
#define rrepll(i, a) RREPLL(i, a, 0)
#define RREPLL(i, a, b) for (ll i = a; i >= b; --i)

typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int, int> P;
typedef std::pair<int, P> PP;
const double PI = 3.14159265358979323846;
const double esp = 1e-9;
const int infi = (int)1e+9 + 10;
const ll infll = (ll)1e+18 + 10;

class RollingHash {
private:
  static const ull B = (ull)1e+8 + 7;

  std::string text;
  int texSize;
  std::vector<ull> pow, phash;

  inline ull TextHash(int s, int e) { return phash[e] - phash[s] * pow[e - s]; }

public:
  RollingHash(std::string tex)
      : text(tex), texSize(text.size()), pow(texSize + 1), phash(texSize + 1) {
    pow[0] = 1;
    phash[0] = 0;
    rep(i, texSize) {
      phash[i + 1] = phash[i] * B + text[i];
      pow[i + 1] = pow[i] * B;
    }
  }

  bool find(std::string pat) {
    int patSize = pat.size();
    if (patSize > texSize)
      return false;

    ull patHash = 0;
    rep(i, patSize) patHash = patHash * B + pat[i];

    rep(i, texSize - patSize + 1) {
      if (TextHash(i, i + patSize) == patHash) {
        return true;
      }
    }
    return false;
  }
};

int n;

int main() {
  std::string a;
  std::cin >> a >> n;

  RollingHash rh(a);

  rep(i, n) {
    char b[1005];
    scanf("%s", &b);
    printf("%d\\n", (rh.find(b) ? 1 : 0));
  }

  return 0;
}