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

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

// #define int long long
#define DBG 1
#define dump(o)                                                                \\
  if (DBG) {                                                                   \\
    cerr << #o << " " << (o) << " ";                                           \\
  }
#define dumpl(o)                                                               \\
  if (DBG) {                                                                   \\
    cerr << #o << " " << (o) << endl;                                          \\
  }
#define dumpc(o)                                                               \\
  if (DBG) {                                                                   \\
    cerr << #o;                                                                \\
    for (auto &e : (o))                                                        \\
      cerr << " " << e;                                                        \\
    cerr << endl;                                                              \\
  }
#define rep(t, a, b) for (int t = (a); t < (b); t++)
#define rrep(t, a, b) for (int t = (b)-1; t >= (a); t--)
#define all(c) begin(c), end(c)
const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);

// KMP??? O(S.size()+W.size())
// https://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm
int knuth_morris_pratt_search(string S, string W) {
  vector<int> T(W.size());
  T[0] = -1, T[1] = 0;
  for (int w = 2, j = 0; w < W.size();) {
    if (W[w - 1] == W[j]) {
      T[w] = j + 1;
      w++, j++;
    } else if (j > 0)
      j = T[j];
    else
      T[w] = 0, w++;
  }

  for (int i = 0, w = 0; i + w < S.size();) {
    if (W[w] == S[i + w]) {
      w++;
      if (w == W.size())
        return i;
    } else {
      i = i + w - T[w];
      if (w > 0)
        w = T[w];
    }
  }
  return -1;
}
vector<int> knuth_morris_pratt_search_all(string S, string W) {
  vector<int> T(W.size() + 1);
  T[0] = -1;
  if (T.size() >= 2)
    T[1] = 0;
  for (int t = 2, w = 0; t < T.size();) {
    if (W[t - 1] == W[w]) {
      T[t] = w + 1;
      t++, w++;
    } else if (w > 0)
      w = T[w];
    else
      T[t] = 0, t++;
  }
  vector<int> ret;
  for (int i = 0, w = 0; i + w < S.size();) {
    if (W[w] == S[i + w]) {
      w++;
      if (w == W.size()) {
        ret.emplace_back(i);
        i = i + w - 1 - T[w - 1];
        if (w - 1 > 0)
          w = T[w - 1];
      }
    } else {
      i = i + w - T[w];
      if (w > 0)
        w = T[w];
    }
  }
  return ret;
}

signed main() {
  string S, W;
  cin >> S >> W;
  vector<int> ans = knuth_morris_pratt_search_all(S, W);
  rep(i, 0, ans.size()) cout << ans[i] << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

// #define int long long
#define DBG 1
#define dump(o)                                                                \\
  if (DBG) {                                                                   \\
    cerr << #o << " " << (o) << " ";                                           \\
  }
#define dumpl(o)                                                               \\
  if (DBG) {                                                                   \\
    cerr << #o << " " << (o) << endl;                                          \\
  }
#define dumpc(o)                                                               \\
  if (DBG) {                                                                   \\
    cerr << #o;                                                                \\
    for (auto &e : (o))                                                        \\
      cerr << " " << e;                                                        \\
    cerr << endl;                                                              \\
  }
#define rep(w, a, b) for (int w = (a); w < (b); w++)
#define rrep(w, a, b) for (int w = (b)-1; w >= (a); w--)
#define all(c) begin(c), end(c)
const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);

// KMP??? O(S.size()+W.size())
// https://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm
int knuth_morris_pratt_search(string S, string W) {
  vector<int> T(W.size());
  T[0] = -1, T[1] = 0;
  for (int w = 2, j = 0; w < W.size();) {
    if (W[w - 1] == W[j]) {
      T[w] = j + 1;
      w++, j++;
    } else if (j > 0)
      j = T[j];
    else
      T[w] = 0, w++;
  }

  for (int i = 0, w = 0; i + w < S.size();) {
    if (W[w] == S[i + w]) {
      w++;
      if (w == W.size())
        return i;
    } else {
      i = i + w - T[w];
      if (w > 0)
        w = T[w];
    }
  }
  return -1;
}
vector<int> knuth_morris_pratt_search_all(string S, string W) {
  vector<int> T(W.size());
  T[0] = -1;
  if (W.size() >= 2)
    T[1] = 0;
  for (int w = 2, j = 0; w < W.size();) {
    if (W[w - 1] == W[j]) {
      T[w] = j + 1;
      w++, j++;
    } else if (j > 0)
      j = T[j];
    else
      T[w] = 0, w++;
  }
  vector<int> ret;
  for (int i = 0, w = 0; i + w < S.size();) {
    if (W[w] == S[i + w]) {
      w++;
      if (w == W.size()) {
        ret.emplace_back(i);
        i = i + w - 1 - T[w - 1];
        w = 0;
      }
    } else {
      i = i + w - T[w];
      if (w > 0)
        w = T[w];
    }
  }
  return ret;
}

signed main() {
  string S, W;
  cin >> S >> W;
  vector<int> ans = knuth_morris_pratt_search_all(S, W);
  rep(i, 0, ans.size()) cout << ans[i] << endl;
  return 0;
}