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

/*
 * b.cc:
 */

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/* constant */

const int MAX_N = 1000000;
const int MAX_M = 10000;

typedef long long ll;

const ll BS0 = 2549;
const ll BS1 = 4283;
const ll MOD0 = 1000000007;
const ll MOD1 = 1000000009;

/* typedef */

typedef vector<int> vi;
typedef queue<int> qi;
typedef pair<int, int> pii;

/* global variables */

/* subroutines */

ll rh(string &s, int m, ll bs, ll mod) {
  ll h = 0;
  for (int i = 0; i < m; i++)
    h = (h * bs + s[i]) % mod;
  return h;
}

ll powmod(ll a, int b, ll mod) {
  ll pm = 1;
  while (b > 0) {
    if (b & 1)
      pm = (pm * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return pm;
}

/* main */

int main() {
  string s, p;
  cin >> s >> p;

  int n = s.size(), m = p.size();

  ll ph0 = rh(p, m, BS0, MOD0);
  ll ph1 = rh(p, m, BS1, MOD1);
  // printf("%lld %lld\\n", ph0, ph1);

  ll e0 = powmod(BS0, m, MOD0);
  ll e1 = powmod(BS1, m, MOD1);

  ll sh0 = rh(s, m, BS0, MOD0);
  ll sh1 = rh(s, m, BS1, MOD1);

  for (int i = 0; i <= n - m; i++) {
    if (sh0 == ph0 && sh1 == ph1)
      printf("%d\\n", i);

    sh0 = (sh0 * BS0 % MOD0 + s[i + m] - s[i] * e0 % MOD0 + MOD0) % MOD0;
    sh1 = (sh1 * BS1 % MOD1 + s[i + m] - s[i] * e1 % MOD1 + MOD1) % MOD1;
  }
  return 0;
}

Error: TLE

Fixed submission:

/*
 * b.cc:
 */

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/* constant */

const int MAX_N = 1000000;
const int MAX_M = 10000;

typedef long long ll;

const ll BS0 = 2549;
const ll BS1 = 4283;
const ll MOD0 = 1000000007;
const ll MOD1 = 1000000009;

/* typedef */

typedef vector<int> vi;
typedef queue<int> qi;
typedef pair<int, int> pii;

/* global variables */

/* subroutines */

ll rh(string &s, int m, ll bs, ll mod) {
  ll h = 0;
  for (int i = 0; i < m; i++)
    h = (h * bs + s[i]) % mod;
  return h;
}

ll powmod(ll a, int b, ll mod) {
  ll pm = 1;
  while (b > 0) {
    if (b & 1)
      pm = (pm * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return pm;
}

/* main */

int main() {
  string s, p;
  cin >> s >> p;

  int n = s.size(), m = p.size();

  ll ph0 = rh(p, m, BS0, MOD0);
  ll ph1 = rh(p, m, BS1, MOD1);
  // printf("%lld %lld\\n", ph0, ph1);

  ll e0 = powmod(BS0, m, MOD0);
  ll e1 = powmod(BS1, m, MOD1);

  ll sh0 = rh(s, m, BS0, MOD0);
  ll sh1 = rh(s, m, BS1, MOD1);

  for (int i = 0; i <= n - m; i++) {
    if (sh0 == ph0 && sh1 == ph1) {
      bool ok = true;
      for (int j = 0; j < m; j++)
        if (s[i + j] != p[j]) {
          ok = false;
          break;
        }
      if (ok)
        printf("%d\\n", i);
    }

    sh0 = (sh0 * BS0 % MOD0 + s[i + m] - s[i] * e0 % MOD0 + MOD0) % MOD0;
    sh1 = (sh1 * BS1 % MOD1 + s[i + m] - s[i] * e1 % MOD1 + MOD1) % MOD1;
  }
  return 0;
}