alexjercan/bugnet huggingface dataset:
Problem p02249 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>

using namespace std;

#define BASE1 124248592
#define BASE2 163282852
#define MOD 197436252

long long hash_temp[1000][1000], hash_field[1000][1000],
    hash_pattern[1000][1000];

void gen_hash(string str[], int H, int W, int R, int C,
              long long hash[][1000]) {
  for (int i = 0; i < H; i++) {
    long long temp = 0, multi = 1;
    for (int j = 0; j < W; j++) {
      temp = (temp * BASE1 + str[i][j]) % MOD;
      if (j < C) {
        multi = multi * BASE1 % MOD;
      } else {
        temp = ((temp - str[i][j - C] * multi) % MOD + MOD) % MOD;
      }
      hash_temp[i][j] = temp;
    }
  }
  for (int j = 0; j < W; j++) {
    long long temp = 0, multi = 1;
    for (int i = 0; i < H; i++) {
      temp = (temp * BASE2 + hash_temp[i][j]) % MOD;
      if (i < R) {
        multi = multi * BASE2 % MOD;
      } else {
        temp = ((temp - hash_temp[i - R][j] * multi) % MOD + MOD) % MOD;
      }
      hash[i][j] = temp;
    }
  }
}

int main() {
  int H, W, R, C;
  string field[1000], pattern[1000];

  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    cin >> field[i];
  }
  cin >> R >> C;
  for (int i = 0; i < R; i++) {
    cin >> pattern[i];
  }

  gen_hash(field, H, W, R, C, hash_field);
  gen_hash(pattern, R, C, R, C, hash_pattern);

  for (int i = R - 1; i < H; i++) {
    for (int j = C - 1; j < W; j++) {
      if (hash_field[i][j] == hash_pattern[R - 1][C - 1]) {
        cout << i - R + 1 << " " << j - C + 1 << "\\n";
      }
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int h, w, r, c;
  string f1[1000], f2[1000];

  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    cin >> f1[i];
  }
  cin >> r >> c;
  for (int i = 0; i < r; i++) {
    cin >> f2[i];
  }

  int minn = 1001, mini;
  for (int i = 0; i < r; i++) {
    int count[62] = {0}, maxn = 0;
    for (int j = 0; j < c; j++) {
      if ('0' <= f2[i][j] && f2[i][j] <= '9') {
        count[f2[i][j] - '0']++;
      }
      if ('A' <= f2[i][j] && f2[i][j] <= 'Z') {
        count[f2[i][j] - 55]++;
      }
      if ('a' <= f2[i][j] && f2[i][j] <= 'z') {
        count[f2[i][j] - 61]++;
      }
    }
    for (int j = 0; j < 62; j++) {
      maxn = max(maxn, count[j]);
    }
    if (minn > maxn) {
      minn = maxn;
      mini = i;
    }
  }

  for (int i = mini; i <= h - r + mini; i++) {
    for (int j = 0; j <= w - c; j++) {
      if (f1[i].substr(j, c) == f2[mini]) {
        bool success = true;
        for (int k = mini - 1; k >= 0; k--) {
          if (f1[i + k - mini].substr(j, c) != f2[k]) {
            success = false;
            break;
          }
        }
        if (success == false) {
          continue;
        }
        for (int k = mini + 1; k < r; k++) {
          if (f1[i + k - mini].substr(j, c) != f2[k]) {
            success = false;
            break;
          }
        }
        if (success == true) {
          cout << i - mini << " " << j << "\\n";
        }
      }
    }
  }

  return 0;
}
Problem p02249 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define MAX 1010
typedef unsigned long long ull;

int N, M, P, Q;
char field[MAX][MAX], pattern[MAX][MAX];
ull hash[MAX][MAX], tmp[MAX][MAX];

void compute_hash(char a[MAX][MAX], int n, int m) {
  const ull B1 = 63533;
  const ull B2 = 100007;

  ull t1 = 1;
  for (int j = 0; j < Q; j++)
    t1 *= B1;

  for (int i = 0; i < n; ++i) {
    ull e = 0;
    for (int j = 0; j < Q; j++)
      e = e * B1 + a[i][j];
    for (int j = 0; j + Q <= m; j++) {
      tmp[i][j] = e;
      if (j + Q < m)
        e = e * B1 - t1 * a[i][j] + a[i][j + Q];
    }
  }

  ull t2 = 1;
  for (int i = 0; i < P; ++i)
    t2 *= B2;

  for (int j = 0; j + Q <= m; ++j) {
    ull e = 0;
    for (int i = 0; i < P; ++i)
      e = e * B2 + tmp[i][j];
    for (int i = 0; i + P <= n; ++i) {
      hash[i][j] = e;
      if (i + P < n)
        e = e * B2 - t2 * tmp[i][j] + tmp[i + P][j];
    }
  }
}

void solve() {
  compute_hash(pattern, P, Q);
  ull res = hash[0][0];
  compute_hash(field, N, M);
  for (int i = 0; i <= N; ++i) {
    for (int j = 0; j <= M; ++j) {
      if (res == hash[i][j]) {
        cout << i << " " << j << endl;
      }
    }
  }
}

int main() {
  cin >> N >> M;
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < M; ++j) {
      cin >> field[i][j];
    }
  }
  cin >> P >> Q;
  for (int i = 0; i < P; i++) {
    for (int j = 0; j < Q; ++j) {
      cin >> pattern[i][j];
    }
  }
  if (N < P && M < Q)
    return 0;
  solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <string>
using namespace std;

int main() {
  long H, W, R, C;
  string T[1000], P[1000];
  cin >> H >> W;
  for (long i = 0; i < H; i++) {
    cin >> T[i];
  }
  cin >> R >> C;
  if (H < R || W < C)
    return 0;
  for (long i = 0; i < R; i++) {
    cin >> P[i];
  }
  for (long i = 0; i < H - R + 1; i++) {
    long j = T[i].find(P[0], 0);
    if (j != -1) {
      while (j < W - C + 1 && j != -1) {
        if (R == 1)
          printf("%ld %ld\\n", i, j);
        else {
          for (long k = 1; k < R; k++) {
            if (T[i + k].find(P[k], j) != j) { // ayashi
              // cout << "miss: find: " << T[i + k].find(P[k], j) << " " << j <<
              // endl; cout << "miss: i = " << i << ", j = " << j << endl;
              break;
            }
            if (k == R - 1)
              printf("%ld %ld\\n", i, j);
          }
        }
        j = T[i].find(P[0], j + 1);
      }
    }
  }
  return 0;
}
Problem p02249 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
#define MOD 100000000000007LL
long long dp[1001][1001];
long long x[1001][1001];
long long y[1001][1001];
long long z[1001];
long long H, W, N, M;
long long modpow[1001];
int main() {
  modpow[0] = 1;
  for (int i = 1; i <= 1000; i++) {
    modpow[i] = modpow[i - 1] * 256;
    modpow[i] %= MOD;
  }
  cin >> H >> W;
  unsigned char c;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> c;
      x[i][j] = c;
    }
  }
  cin >> N >> M;
  for (int i = 0; i < N; i++) {
    long long sum = 0;
    for (int j = 0; j < M; j++) {
      cin >> c;
      y[i][j] = c;
      sum *= 256;
      sum %= MOD;
      sum += y[i][j];
    }
    z[i] = sum;
  }
  for (int i = 0; i < H; i++) {
    long long sum = 0;
    for (int j = 0; j < M; j++) {
      sum *= 256;
      sum %= MOD;
      sum += x[i][j];
    }
    dp[i][0] = sum;
    for (int j = M; j < W; j++) {
      dp[i][j - M + 1] = dp[i][j - M];
      dp[i][j - M + 1] += MOD * 256 - modpow[M - 1] * x[i][j - M];
      dp[i][j - M + 1] *= 256;
      dp[i][j - M + 1] %= MOD;
      dp[i][j - M + 1] += x[i][j];
    }
  }
  for (int i = 0; i <= H - N; i++) {
    for (int j = 0; j <= W - M; j++) {
      for (int k = 0; k < N; k++) {
        if (dp[i + k][j] != z[k]) {
          goto E;
        }
      }
      cout << i << ' ' << j << endl;
    E:;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;

string S[10000];
string T[10000];

int main() {
  int n, m, r, c, sum, t = 0;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> S[i];
  }
  cin >> r >> c;
  for (int i = 0; i < r; i++) {
    cin >> T[i];
  }
  for (int i = 0; i <= n - r; i++) {
    for (int j = 0; j <= m - c; j++) {
      for (int k = 0; k < r; k++) {
        if (T[k] != S[k + i].substr(j, c)) {
          goto Exit;
        }
        if (t >= 500 && k >= r) {
          goto Exit2;
        }
      }
    Exit2:;
      cout << i << ' ' << j << endl;
      t++;
    Exit:;
    }
  }
  return 0;
}
Problem p02249 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
#pragma warning(disable : 4996)
using namespace std;
int H, W, R, C;
string s[1000], t[1000];
unsigned long long power[1001], h1[1000][1000], h2[1000];
int main() {
  scanf("%d%d", &H, &W);
  for (int i = 0; i < H; i++)
    cin >> s[i];
  scanf("%d%d", &R, &C);
  for (int i = 0; i < R; i++)
    cin >> t[i];
  power[0] = 1;
  for (int i = 1; i <= 1000; i++)
    power[i] = power[i - 1] * 257;
  for (int i = 0; i < H; i++) {
    unsigned long long val = 0;
    for (int j = 0; j < C; j++)
      val = val * 257 + s[i][j];
    h1[i][0] = val;
    for (int j = C; j < W; j++)
      val = val * 257 + s[i][j] - power[C] * s[i][j - C],
      h1[i][j - C + 1] = val;
  }
  for (int i = 0; i < R; i++) {
    for (int j = 0; j < C; j++)
      h2[i] = h2[i] * 257 + t[i][j];
  }
  for (int i = 0; i <= H - R; i++) {
    for (int j = 0; j <= W - C; j++) {
      bool flag = true;
      for (int k = i; k < i + R; k++) {
        if (h1[k][j] != h2[k - i]) {
          flag = false;
          break;
        }
      }
      if (flag) {
        printf("%d %d\\n", i, j);
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  int H, W, R, C;

  cin >> H >> W;

  vector<string> p1(H);

  for (int i = 0; i < H; i++) {
    cin >> p1[i];
  }

  cin >> R >> C;

  vector<string> p2(R);

  for (int i = 0; i < R; i++) {
    cin >> p2[i];
  }

  for (int i = 0; i <= H - R; i++) {
    for (int j = 0; j <= W - C; j++) {
      for (int k = 0; k < R; k++) {
        if (p1[i + k].substr(j, C) != p2[k]) {
          goto Exit;
        }
      }

      printf("%d %d\\n", i, j);

    Exit:;
    }
  }

  return 0;
}
Problem p02250 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define S 2500005
#define N 1005
using namespace std;
typedef pair<int, int> P;
struct d {
  int a, b, c;
  d() {}
  d(int A, int B, int C) { a = A, b = B, c = C; };
  bool operator<(const d &r) const {
    if (a != r.a)
      return a < r.a;
    return b < r.b;
  }
};

int n, slen, tlen, r[S];
char s[S], t[N];
d ran[S];
P a[S];

void init() {

  for (int j = 0; j < slen; j++)
    r[j] = s[j];

  int x = 1;
  r[slen] = 0;
  while (1) {
    for (int i = 0; i < slen; i++) {
      int o = i + x;
      if (o > slen)
        o = slen;
      ran[i] = d(r[i], r[o], i);
    }
    sort(ran, ran + slen);
    int cnt = 0;
    for (int i = 0; i < slen; i++) {
      r[ran[i].c] = cnt;
      if (ran[i].a == ran[i + 1].a && ran[i].b == ran[i + 1].b)
        continue;
      cnt++;
    }
    if (x > slen)
      break;
    x *= 2;
  }
  for (int i = 0; i < slen; i++)
    a[i] = P(r[i], i);
  sort(a, a + slen);
}

bool bynary_search() {
  int L = 0, R = slen;
  while (L < R) {
    int M = (L + R) / 2;
    if (strncmp(s + a[M].second, t, tlen) < 0)
      L = M + 1;
    else
      R = M;
  }
  if (!strncmp(s + a[L].second, t, tlen))
    return true;
  return false;
}

int main() {
  scanf("%s%d", s, &n);
  slen = strlen(s);
  init();
  while (n--) {
    scanf("%s", t);
    tlen = strlen(t);
    if (bynary_search())
      printf("1\\n");
    else
      printf("0\\n");
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define S 1000005
#define N 1005
using namespace std;
typedef pair<int, int> P;
struct d {
  int a, b, c;
  d() {}
  d(int A, int B, int C) { a = A, b = B, c = C; };
  bool operator<(const d &r) const {
    if (a != r.a)
      return a < r.a;
    return b < r.b;
  }
};

int n, slen, tlen, r[N];
char s[S], t[N];
d ran[N];
P a[N];

void init() {
  for (int i = 0; i < 26; i++)
    for (int j = 0; j < slen; j++)
      if ('a' + i == s[j])
        r[j] = i + 1;
  int x = 1;
  while (x < slen) {
    for (int i = 0; i < slen; i++) {
      int a = i + x;
      if (a > slen)
        a = slen;
      ran[i] = d(r[i], r[a], i);
    }
    sort(ran, ran + slen);
    int cnt = 1;
    for (int i = 0; i < slen; i++) {
      r[ran[i].c] = cnt;
      if (ran[i].a == ran[i + 1].a && ran[i].b == ran[i + 1].b)
        continue;
      cnt++;
    }
    x *= 2;
  }
  for (int i = 0; i < slen; i++)
    a[i] = P(r[i], i);
  sort(a, a + slen);
}

bool bynary_search() {
  int L = 0, R = slen;
  while (L < R) {
    int M = (L + R) / 2;
    if (strncmp(s + a[M].second, t, tlen) < 0)
      L = M + 1;
    else
      R = M;
  }
  if (!strncmp(s + a[L].second, t, tlen))
    return true;
  return false;
}

int main() {
  scanf("%s%d", s, &n);
  slen = strlen(s);
  init();
  while (n--) {
    scanf("%s", t);
    tlen = strlen(t);
    if (bynary_search())
      printf("1\\n");
    else
      printf("0\\n");
  }
  return 0;
}
Problem p02250 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

// #define int long long
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)
const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

struct SuffixArray {
  int n, k;
  string s;
  vector<int> ord, tmp, left;
  SuffixArray(const string &s)
      : s(s), n(s.size()), ord(n + 1), tmp(n + 1), left(n + 1) {
    for (int i = 0; i <= n; i++) {
      left[i] = i;
      ord[i] = i < n ? s[i] : -1;
    }
    auto compare = [&](int i, int j) {
      if (ord[i] != ord[j])
        return ord[i] < ord[j];
      else {
        int ri = i + k <= n ? ord[i + k] : -1;
        int rj = j + k <= n ? ord[j + k] : -1;
        return ri < rj;
      }
    };
    for (k = 1; k <= n; k *= 2) {
      sort(left.begin(), left.end(), compare);
      tmp[left[0]] = 0;
      for (int i = 1; i <= n; i++) {
        tmp[left[i]] =
            tmp[left[i - 1]] + (compare(left[i - 1], left[i]) ? 1 : 0);
      }
      ord = tmp;
    }
    dump(ord, tmp, left);
  }
  int search(const string &pattern) {
    int m = pattern.size();
    auto f = [&](int x) {
      string t = s.substr(left[x], min(m, n - left[x]));
      dump(x, t);
      return pattern <= t;
    };
    auto binarySearch = [&](int ng, int ok) {
      if (f(ng))
        return ng;
      while (ng + 1 < ok) {
        int m = (ng + ok) / 2;
        if (f(m))
          ok = m;
        else
          ng = m;
      }
      return ok;
    };
    int ok = binarySearch(0, n + 1);
    if (ok > n)
      return -1;
    else {
      string t = s.substr(left[ok], min(m, n - left[ok]));
      return pattern == t ? left[ok] : -1;
    }
  }
};

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string T;
  cin >> T;
  int Q;
  cin >> Q;
  SuffixArray sa(T);
  rep(i, 0, Q) {
    string P;
    cin >> P;
    cout << (sa.search(P) != -1) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL

int main() {
  string T;
  cin >> T;
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    string P;
    cin >> P;
    if (T.find(P) == string::npos)
      cout << 0 << endl;
    else
      cout << 1 << endl;
  }
  return 0;
}
Problem p02250 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
using namespace std;
struct SAComp {
  const int h, *g;
  SAComp(const int h, const int *g) : h(h), g(g) {}
  bool operator()(const int a, const int b) {
    return a == b ? false : g[a] != g[b] ? g[a] < g[b] : g[a + h] < g[b + h];
  }
};
vector<int> buildSA(string &t) {
  int n = t.size();
  int g[n], b[n];
  vector<int> suff(n);
  for (int i = 0; i < n; i++)
    suff[i] = i, g[i] = t[i];
  b[0] = b[n - 1] = 0;
  sort(suff.begin(), suff.end(), SAComp(0, g));
  for (int h = 1; b[n - 1] != n - 1; h <<= 1) {
    SAComp comp(h, g);
    sort(suff.begin(), suff.end(), comp);
    for (int i = 0; i < n - 1; i++)
      b[i + 1] = b[i] + comp(suff[i], suff[i + 1]);
    for (int i = 0; i < n; i++)
      g[suff[i]] = b[i];
  }
  return suff;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  string s;
  cin >> s;
  s += '$';
  int n = s.size();
  vector<int> suff = buildSA(s);
  vector<pair<char, int>> sorted(n);
  for (int i = 0; i < n; i++)
    sorted[i] = {s[(suff[i] - 1 + n) % n], i};
  sort(sorted.begin(), sorted.end());

  int T;
  for (cin >> T; T--;) {
    string q;
    cin >> q;
    int start = 0, stop = n, idx = q.size() - 1;
    for (; idx >= 0; idx--) {
      pair<char, int> ql = {q[idx], start}, qr = {q[idx], stop};
      start = lower_bound(sorted.begin(), sorted.end(), ql) - sorted.begin();
      stop = lower_bound(sorted.begin(), sorted.end(), qr) - sorted.begin();
      if (start == stop)
        break;
    }
    if (idx < 0) {
      //{vector<int>v;for(;start<stop;start++)v.push_back(suff[start]);sort(v.begin(),v.end());for(auto
      //&e:v)printf("%d\\n",e);}
      puts("1");
    } else {
      puts("0");
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
using namespace std;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  string s;
  cin >> s;
  s += '$';
  int n = s.size();
  vector<int> suff(n);
  iota(suff.begin(), suff.end(), 0);
  {
    const char *str = s.c_str();
    sort(suff.begin(), suff.end(),
         [&](int a, int b) { return strcmp(str + a, str + b) < 0; });
  }
  vector<pair<char, int>> sorted(n);
  for (int i = 0; i < n; i++)
    sorted[i] = {s[(suff[i] - 1 + n) % n], i};
  sort(sorted.begin(), sorted.end());

  int T;
  for (cin >> T; T--;) {
    string q;
    cin >> q;
    int start = 0, stop = n, idx = q.size() - 1;
    for (; idx >= 0; idx--) {
      pair<char, int> ql = {q[idx], start}, qr = {q[idx], stop};
      start = lower_bound(sorted.begin(), sorted.end(), ql) - sorted.begin();
      stop = lower_bound(sorted.begin(), sorted.end(), qr) - sorted.begin();
      if (start == stop)
        break;
    }
    if (idx < 0) {
      // for(;start<stop;start++)printf("%d\\n",suff[start]);
      puts("1");
    } else {
      puts("0");
    }
  }
}
Problem p02250 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
typedef pair<P, int> PP;

int res[1000005];
PP tmp[1000005];
int ans[1000005];

void suffixArray(char *s) {

  int len = strlen(s);

  for (int i = 0; i <= len; i++)
    res[i] = s[i];

  for (int k = 1; k <= len; k *= 2) {

    for (int i = 0; i <= len; i++) {
      tmp[i].first.first = res[i];
      tmp[i].first.second = (i + k <= len ? res[i + k] : 0);
      tmp[i].second = i;
    }
    sort(tmp, tmp + len + 1);

    int C = 0;
    for (int i = 0; i <= len; i++) {
      int id = tmp[i].second;
      if (i && (tmp[i - 1].first < tmp[i].first))
        C++;
      res[id] = C;
    }
  }

  for (int i = 0; i <= len; i++)
    ans[res[i]] = i;
}

char s[1000005];
char t[1000005];
int main() {
  scanf("%s", s);
  int lenS = strlen(s);

  suffixArray(s);

  int Q;
  scanf("%d", &Q);

  while (Q--) {
    scanf("%s", t);
    int len = strlen(t);
    int l = 0, r = lenS + 1, m;
    while (r - l > 1) {
      m = (l + r) / 2;
      if (strncmp(s + ans[m], t, len) <= 0)
        l = m;
      else
        r = m;
    }
    if (strncmp(s + ans[l], t, len) == 0)
      printf("1\\n");
    else
      printf("0\\n");
  }

  /*
  for(int i=0;i<(int)ans.size();i++){
    printf("%d %s\\n",ans[i], (s+ans[i]) );
  }
  */

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
typedef pair<P, int> PP;

void suffixArray(char *s, vector<int> &res2) {

  int len = strlen(s);
  vector<int> res(len + 1);
  vector<PP> tmp(len + 1);
  res2 = vector<int>(len + 1);

  for (int i = 0; i <= len; i++)
    res[i] = s[i];

  for (int k = 1; k <= len; k *= 2) {

    for (int i = 0; i <= len; i++) {
      tmp[i].first.first = res[i];
      tmp[i].first.second = (i + k <= len ? res[i + k] : 0);
      tmp[i].second = i;
    }
    sort(tmp.begin(), tmp.end());
    int C = 0;
    for (int i = 0; i <= len; i++) {
      int id = tmp[i].second;
      if (i && (tmp[i - 1].first < tmp[i].first))
        C++;
      res[id] = C;
    }
  }

  //  vector<int> res2(len+1);
  for (int i = 0; i <= len; i++)
    res2[res[i]] = i;
  // return res2;
}

char s[1000005];
char t[1000005];
int main() {
  scanf("%s", s);
  vector<int> ans;
  suffixArray(s, ans);

  int Q;
  scanf("%d", &Q);

  while (Q--) {
    scanf("%s", t);
    int len = strlen(t);
    int l = 0, r = ans.size(), m;
    while (r - l > 1) {
      m = (l + r) / 2;
      if (strncmp(s + ans[m], t, len) <= 0)
        l = m;
      else
        r = m;
    }
    if (strncmp(s + ans[l], t, len) == 0)
      printf("1\\n");
    else
      printf("0\\n");
  }

  /*
  for(int i=0;i<(int)ans.size();i++){
    printf("%d %s\\n",ans[i], (s+ans[i]) );
  }
  */

  return 0;
}
Problem p02250 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

#pragma warning(disable : 4996)

using namespace std;

vector<int> dat;
vector<int> tmp;

int doubles, lengths;

bool compare_suffix(int i, int j) {
  if (dat[i] == dat[j]) {
    int ri = (i + doubles <= lengths ? dat[i + doubles] : -1);
    int rj = (j + doubles <= lengths ? dat[j + doubles] : -1);

    return ri < rj;
  }

  return dat[i] < dat[j];
}

vector<int> suffix_array(string S) {
  lengths = S.size();

  vector<int> arrays(lengths + 1);

  dat.resize(lengths + 1);
  tmp.resize(lengths + 1);

  for (int i = 0; i <= lengths; i++) {
    arrays[i] = i;

    dat[i] = i < lengths ? S[i] : -1;
  }

  for (doubles = 1; doubles <= lengths; doubles <<= 1) {
    sort(arrays.begin(), arrays.begin() + lengths + 1, compare_suffix);

    tmp[arrays[0]] = 0;

    for (int i = 1; i <= lengths; i++) {
      tmp[arrays[i]] = tmp[arrays[i - 1]] +
                       (compare_suffix(arrays[i - 1], arrays[i]) ? 1 : 0);
    }

    for (int i = 0; i <= lengths; i++) {
      dat[i] = tmp[i];
    }
  }

  return arrays;
}

int main() {
  string S, T;
  int Q;

  cin >> S >> Q;

  vector<int> sa = suffix_array(S);

  for (int i = 0; i < Q; i++) {
    cin >> T;

    int l = 0;
    int r = sa.size();

    bool flag = false;

    while (r - l > 1) {
      int m = (l + r) / 2;

      string S1 = S.substr(sa[m], T.size());

      if (S1 == T) {
        flag = true;
        break;
      }

      if (S1 < T) {
        l = m;
      } else {
        r = m;
      }
    }

    if (flag) {
      printf("1\\n");
    } else {
      printf("0\\n");
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>

using namespace std;

string S;
int Q;
char STR[1005];

int main() {
  cin >> S >> Q;

  for (int i = 0; i < Q; i++) {
    scanf("%s", STR);

    if (S.find(STR) == -1) {
      printf("0\\n");
    } else {
      printf("1\\n");
    }
  }

  return 0;
}