Problem p02248 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define rep(i, a) REP(i, 0, a)
#define REP(i, a, b) for (int i = a; i < b; ++i)
#define rrep(i, a) RREP(i, a, 0)
#define RREP(i, a, b) for (int i = a; i >= b; --i)
#define repll(i, a) REPLL(i, 0, a)
#define REPLL(i, a, b) for (ll i = a; i < b; ++i)
#define rrepll(i, a) RREPLL(i, a, 0)
#define RREPLL(i, a, b) for (ll i = a; i >= b; --i)

typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int, int> P;
typedef std::pair<int, P> PP;
const double PI = 3.14159265358979323846;
const double esp = 1e-9;
const int infi = (int)1e+9 + 10;
const ll infll = (ll)1e+18 + 10;

class RollingHash {
private:
  static const ull B = (ull)1e+8 + 7;

  std::string text;
  int texSize;
  std::vector<ull> pow, phash;

  inline ull TextHash(int s, int e) { return phash[e] - phash[s] * pow[e - s]; }

public:
  RollingHash(std::string tex)
      : text(tex), texSize(text.size()), pow(texSize + 1), phash(texSize + 1) {
    pow[0] = 1;
    phash[0] = 0;
    rep(i, texSize) {
      phash[i + 1] = phash[i] * B + text[i];
      pow[i + 1] = pow[i] * B;
    }
  }

  void find(std::string pat) {
    int patSize = pat.size();
    if (patSize > texSize)
      return;

    ull patHash = 0;
    rep(i, patSize) patHash = patHash * B + pat[i];

    rep(i, texSize - patSize + 1) {
      if (TextHash(i, i + patSize) == patHash) {
        std::cout << i << std::endl;
      }
    }
  }
};

int main() {
  std::string a, b;
  std::cin >> a >> b;

  RollingHash rh(a);
  rh.find(b);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define rep(i, a) REP(i, 0, a)
#define REP(i, a, b) for (int i = a; i < b; ++i)
#define rrep(i, a) RREP(i, a, 0)
#define RREP(i, a, b) for (int i = a; i >= b; --i)
#define repll(i, a) REPLL(i, 0, a)
#define REPLL(i, a, b) for (ll i = a; i < b; ++i)
#define rrepll(i, a) RREPLL(i, a, 0)
#define RREPLL(i, a, b) for (ll i = a; i >= b; --i)

typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int, int> P;
typedef std::pair<int, P> PP;
const double PI = 3.14159265358979323846;
const double esp = 1e-9;
const int infi = (int)1e+9 + 10;
const ll infll = (ll)1e+18 + 10;

class RollingHash {
private:
  static const ull B = (ull)1e+8 + 7;

  std::string text;
  int texSize;
  std::vector<ull> pow, phash;

  inline ull TextHash(int s, int e) { return phash[e] - phash[s] * pow[e - s]; }

public:
  RollingHash(std::string tex)
      : text(tex), texSize(text.size()), pow(texSize + 1), phash(texSize + 1) {
    pow[0] = 1;
    phash[0] = 0;
    rep(i, texSize) {
      phash[i + 1] = phash[i] * B + text[i];
      pow[i + 1] = pow[i] * B;
    }
  }

  void find(std::string pat) {
    int patSize = pat.size();
    if (patSize > texSize)
      return;

    ull patHash = 0;
    rep(i, patSize) patHash = patHash * B + pat[i];

    rep(i, texSize - patSize + 1) {
      if (TextHash(i, i + patSize) == patHash) {
        printf("%d\n", i);
      }
    }
  }
};

int main() {
  std::string a, b;
  std::cin >> a >> b;

  RollingHash rh(a);
  rh.find(b);

  return 0;
}