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

#include <bits/stdc++.h>
using namespace std;

using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using LL = long long;
using VL = vector<LL>;
using VVL = vector<VL>;
using PLL = pair<LL, LL>;
using VS = vector<string>;

#define ALL(a) begin((a)), end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

#define FF first
#define SS second
template <class S, class T> istream &operator>>(istream &is, pair<S, T> &p) {
  return is >> p.FF >> p.SS;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  return os << p.FF << " " << p.SS;
}
template <class T> void maxi(T &x, T y) {
  if (x < y)
    x = y;
}
template <class T> void mini(T &x, T y) {
  if (x > y)
    x = y;
}

const double EPS = 1e-10;
const double PI = acos(-1.0);
const LL MOD = 1e9 + 7;

void build(const string &pattern, vector<int> &table) {
  int M = pattern.size();
  table.assign(M + 1, 0);
  for (int i = 2, j = 0; i <= M;) {
    if (pattern[i - 1] == pattern[j]) {
      table[i] = j + 1;
      ++i;
      ++j;
    } else if (j > 0)
      j = table[j];
    else {
      table[i] = 0;
      ++i;
    }
  }
}

int kmp_search(const string &str, const string &pattern,
               const vector<int> &table) {
  int N = SZ(str), M = SZ(pattern);
  int res = 0;
  for (int m = 0, i = 0; m + i < N;) {
    if (str[m + i] == pattern[i]) {
      ++i;
      if (i == M) {
        cout << m << endl;
        ++res;
        m += i - table[i];
        i = table[i];
      }
    } else {
      if (i > 0) {
        m += i - table[i];
        i = table[i];
      } else
        ++m;
    }
  }
  return res;
}

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

  string S, T;
  cin >> S >> T;
  VI tb;
  build(T, tb);
  kmp_search(S, T, tb);

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using LL = long long;
using VL = vector<LL>;
using VVL = vector<VL>;
using PLL = pair<LL, LL>;
using VS = vector<string>;

#define ALL(a) begin((a)), end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

#define FF first
#define SS second
template <class S, class T> istream &operator>>(istream &is, pair<S, T> &p) {
  return is >> p.FF >> p.SS;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  return os << p.FF << " " << p.SS;
}
template <class T> void maxi(T &x, T y) {
  if (x < y)
    x = y;
}
template <class T> void mini(T &x, T y) {
  if (x > y)
    x = y;
}

const double EPS = 1e-10;
const double PI = acos(-1.0);
const LL MOD = 1e9 + 7;

void build(const string &pattern, vector<int> &table) {
  int M = pattern.size();
  table[0] = -1, table[1] = 0;
  for (int i = 2, j = 0; i < M;) {
    if (pattern[i - 1] == pattern[j]) {
      table[i] = j + 1;
      ++i;
      ++j;
    } else if (j > 0)
      j = table[j];
    else { // j == 0
      table[i] = 0;
      ++i;
    }
  }
}

int kmp_search(const string &str, const string &pattern,
               const vector<int> &table) {
  int N = SZ(str), M = SZ(pattern);
  int res = 0;
  for (int m = 0, i = 0; m + i < N;) {
    if (str[m + i] == pattern[i]) {
      ++i;
      if (i == M) {
        cout << m << endl;
        ++res;
        --i;
        m = m + i - table[i];
        i = table[i];
      }
    } else {
      m = m + i - table[i];
      if (i > 0)
        i = table[i];
    }
  }
  return res;
}

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

  string S, T;
  cin >> S >> T;
  VI tb(SZ(T));
  build(T, tb);
  kmp_search(S, T, tb);

  return 0;
}