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

#include <bits/stdc++.h>

using namespace std;

#ifdef DEBUG_MODE
#define DBG(n) n;
#else
#define DBG(n) ;
#endif
#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define rep(i, s, g) for (ll(i) = (s); (i) < (g); ++i)
#define rrep(i, s, g) for (ll(i) = (s); i >= (g); --(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n)                                                           \\
  {                                                                            \\
    for (int WWW = 0; WWW < (n); WWW++)                                        \\
      cerr << v[WWW] << ' ';                                                   \\
    cerr << endl << endl;                                                      \\
  }
#define SHOW2d(v, i, j)                                                        \\
  {                                                                            \\
    for (int aaa = 0; aaa < i; aaa++) {                                        \\
      for (int bbb = 0; bbb < j; bbb++)                                        \\
        cerr << v[aaa][bbb] << ' ';                                            \\
      cerr << endl;                                                            \\
    }                                                                          \\
    cerr << endl;                                                              \\
  }
#define ALL(v) v.begin(), v.end()
#define Decimal fixed << setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL

typedef long long ll;
typedef pair<ll, ll> P;

#define NODE 555555
pair<ll, ll> f[2][NODE];
ll mod[] = {1000000007, 1000000009};
ll kake[] = {114514, 1919810};
ll inv[2];
int n;
string str;
map<pair<ll, ll>, ll> mp;

ll m_pow(ll a, ll b, ll p) {
  ll ret = 1;
  ll tmp = a;
  for (ll i = 0LL; i < 60; i++) {
    if (b & (1LL << i)) {
      ret *= tmp;
      ret %= p;
    }
    tmp *= tmp;
    tmp %= p;
  }
  return ret;
}

vector<int> RollingHash(string str, string s) {
  vector<int> ret;
  ll mark[2] = {};
  REP(i, s.size()) {
    REP(j, 2) {
      mark[j] *= kake[j];
      mark[j] += (int)s[i];
      mark[j] %= mod[j];
    }
  }
  ll po[2];
  REP(i, 2) po[i] = m_pow(kake[i], s.size() - 1, mod[i]);
  ll now[2] = {};
  REP(i, str.size()) {
    bool flag = true;
    REP(j, 2) {
      if (i >= s.size()) {
        ll tmp = po[j] * (int)str[i - s.size()] % mod[j];
        now[j] = (now[j] + mod[j] - tmp) % mod[j];
      }
      now[j] *= kake[j];
      now[j] += (int)str[i];
      now[j] %= mod[j];
      if (now[j] != mark[j])
        flag = false;
    }
    if (flag)
      ret.PB(i - s.size() + 1);
  }
  return ret;
}

int main() {

  string a, b;
  cin >> a >> b;
  vector<int> ans = RollingHash(a, b);
  REP(i, ans.size()) { cout << ans[i] << endl; }

  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  int def;
  string a, b;
  cin >> a >> b;
  int last = a.size() - b.size() + 1;
  for (int i = 0; i < last; i++) {
    if (a[i] == b[0]) {
      for (int j = 0; j < b.size(); j++) {
        if (a[i + j] != b[j]) {
          break;
        }
        if (j == b.size() - 1)
          cout << i << endl;
      }
    }
  }
  return 0;
}