alexjercan/bugnet huggingface dataset:
Problem p02248 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>

std::string GetSub(const std::string &P) {
  for (std::size_t i = 1; i < P.size(); ++i) {
    int n = P.size() - i;
    if (P.substr(i) == P.substr(0, n)) {
      return P.substr(n);
    }
  }
  return P;
}
int main() {
  std::string T, P;

  std::cin >> T >> P;
  const int Skip = P.size();
  const std::string Sub = GetSub(P);
  // std::cerr << "Skip:" << Skip << ", Sub:" << Sub << std::endl;
  int Pos = T.find(P, 0);
  while (Pos != std::string::npos) {
    std::cout << Pos << std::endl;
    if (T.substr(Pos + Skip, Sub.size()) == Sub) {
      Pos += Sub.size();
    } else {
      Pos = T.find(P, Pos + Skip);
    }
  }
  return 0;
}

Error: 0
FitSkip:2, Sub:a


Fixed submission:

#include <iostream>
#include <string>

std::string GetSub(const std::string &P) {
  for (std::size_t i = 1; i < P.size(); ++i) {
    int n = P.size() - i;
    if (P.substr(i) == P.substr(0, n)) {
      return P.substr(n);
    }
  }
  return P;
}
int main() {
  std::string T, P;

  std::cin >> T >> P;
  const int FitSkip = P.size();
  const std::string Sub = GetSub(P);
  std::cerr << "FitSkip:" << FitSkip << ", Sub:" << Sub << std::endl;
  int Pos = T.find(P, 0);
  while (Pos != std::string::npos) {
    std::cout << Pos << std::endl;
    if (Sub.size() && T.substr(Pos + FitSkip, Sub.size()) == Sub) {
      Pos += Sub.size();
    } else {
      Pos = T.find(P, Pos + FitSkip);
    }
  }
  return 0;
}
Problem p02248 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define base 950527
using namespace std;
typedef unsigned long long ll;
map<ll, ll> map;

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

  ll hs = 0, n = 0, t = 1;
  for (int i = 0; i < b.size(); i++) {
    hs = hs * base + b[i];
    n = n * base + a[i];
    t *= base;
  }

  if (hs == n)
    cout << 0 << endl;
  for (int i = b.size(); i < a.size(); i++) {
    n = n * base - t * a[i - b.size()];
    n += a[i];
    if (hs == n)
      printf("%d\\n", i - (int)b.size() + 1);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define base 950527
using namespace std;
typedef unsigned long long ll;
map<ll, ll> map;

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

  ll hs = 0, n = 0, t = 1;
  for (int i = 0; i < b.size(); i++) {
    hs = hs * base + b[i];
    n = n * base + a[i];
    t *= base;
  }

  if (hs == n)
    cout << 0 << endl;
  for (int i = b.size(); i < a.size(); i++) {
    n = n * base - t * a[i - b.size()];
    n += a[i];
    if (hs == n)
      cout << i - b.size() + 1 << endl;
  }
  return 0;
}
Problem p02248 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (int i = a; i <= b; i++)
using namespace std;

int main() {
  string t, p;
  cin >> t >> p;
  int nt = t.size();
  int np = p.size();
  if (nt < np)
    return 0;
  REP(i, nt - np + 1)
  if (t[i] == p[0]) if (t.substr(i, np) == p) printf("%d\\n", i);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define SIZE 300005
#define MOD 1000000007LL
#define INF 1 << 30
#define LLINF 1LL << 60
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define DOWN(i, b, a) for (int i = b; i >= a; i--)
#define SET(a, c) memset(a, c, sizeof a)
#define BIT(i, j) ((i) >> (j)) & 1
#define ALL(o) (o).begin(), (o).end()
#define ERASE(o) (o).erase(unique((o).begin(), (o).end()), (o).end())
#define SQ(x) ((x) * (x))
using namespace std;
typedef long long ll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pii;
typedef pair<double, double> Pdd;
typedef complex<double> dcomplex;
template <typename T> inline void priv(vector<T> a) {
  REP(i, a.size()) { cerr << a[i] << ((i == a.size() - 1) ? "\\n" : " "); }
}
ll gcd(ll a, ll b) {
  int c = max(a, b);
  int d = min(a, b);
  return c == 0 || d == 0 ? c : gcd(c % d, d);
}
ll lcm(ll a, ll b) { return a == 0 || b == 0 ? 0 : a * b / gcd(a, b); }
ll fact(ll a) {
  ll b = 1;
  FOR(i, 1, a) b *= i;
  return b;
}

int main() {
  string t, p;
  cin >> t >> p;
  int nt = t.size();
  int np = p.size();
  if (nt < np)
    return 0;
  REP(i, nt - np + 1) {
    if (t[i] == p[0]) {
      bool f = true;
      REP(j, np) if (t[i + j] != p[j]) {
        f = false;
        break;
      }
      if (f)
        printf("%d\\n", i);
    }
  }
  return 0;
}
Problem p02248 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main(){
         ios::sync_with_stdio(false);
        string a, b;
        int x, i = 0;
        cin >> a >> b;
        for(int i = 0;a[i];i++){
                i = a.find(b, i);
                if(i == -1)break;
                cout << i << endl;
        }
        while((x = a.find(b, i)) != -1){
                cout << x << endl;
                i = x + 1;
        }
        return 0;
}*/

#include <iostream>
using namespace std;
string t, p;
int main() {
  ios::sync_with_stdio(false);
  cin >> t >> p;
  for (int i = 0; t[i]; i++) {
    i = t.find(p, i);
    if (i == string::npos)
      break;
    cout << i << '\\n';
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  ios::sync_with_stdio(false);
  string a, b;
  int x, i = 0;
  cin >> a >> b;
  for (int i = 0; a[i]; i++) {
    i = a.find(b, i);
    if (i == -1)
      break;
    cout << i << endl;
  }
  /*while((x = a.find(b, i)) != -1){
          cout << x << endl;
          i = x + 1;
  }*/
  return 0;
}
Problem p02248 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  string a, b;
  int x, i = 0;
  cin >> a >> b;
  while ((x = a.find(b, i)) != -1) {
    cout << x << '\\n';
    i = x + 1;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main(){
         ios::sync_with_stdio(false);
        string a, b;
        int x, i = 0;
        cin >> a >> b;
        for(int i = 0;a[i];i++){
                i = a.find(b, i);
                if(i == -1)break;
                cout << i << endl;
        }
        while((x = a.find(b, i)) != -1){
                cout << x << endl;
                i = x + 1;
        }
        return 0;
}*/

#include <iostream>
#include <string>
using namespace std;
string t, p;
int main() {
  // ios::sync_with_stdio(false);
  cin >> t >> p;
  for (int i = 0; t[i]; i++) {
    i = t.find(p, i);
    if (i == -1)
      break;
    cout << i << endl;
  }
  return 0;
}
Problem p02248 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
#define all(v) (v).begin(), (v).end()
typedef vector<int> vint;
typedef pair<int, int> pint;

typedef unsigned long long ull;
const ull p = 1000000007;
const int SIZE = 1000000;
ull H[SIZE + 1], power[SIZE + 1];
char S[SIZE + 1], T[SIZE + 1];

signed main() {
  scanf("%s%s", S, T);
  int ls = strlen(S), lt = strlen(T);
  power[0] = 1;
  for (int i = 1; i <= ls; i++) {
    power[i] = power[i - 1] * p;
    H[i] = H[i - 1] * p + S[i - 1];
  }

  ull ht = 0;
  rep(i, lt) ht = ht * p + T[i];

  for (int i = lt; i <= ls; i++) {
    ull hs = H[i] - H[i - lt] * power[lt];
    if (hs == ht)
      printf("%d\\n", i - lt);
  }
  return 0;
}

Error: TLE

Fixed submission:

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

#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
#define all(v) (v).begin(), (v).end()
typedef vector<int> vint;
typedef pair<int, int> pint;

typedef unsigned long long ull;
const ull p = 1000000007;
const int SIZE = 1000000;
ull H[SIZE + 1], power[SIZE + 1];

signed main() {
  power[0] = 1;
  string S, T;
  cin >> S >> T;
  for (int i = 1; i <= S.size(); i++) {
    power[i] = power[i - 1] * p;
    H[i] = H[i - 1] * p + S[i - 1];
  }

  ull ht = 0;
  rep(i, T.size()) ht = ht * p + T[i];

  for (int i = T.size(); i <= S.size(); i++) {
    ull hs = H[i] - H[i - T.size()] * power[T.size()];
    if (hs == ht)
      cout << i - T.size() << endl;
  }
  return 0;
}
Problem p02248 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <string>
#include <vector>

using namespace std;

const long long MOD = 1e9 + 7;
const long long BASE = 0x234567;

struct RollingHash {
  string s;
  long long mod, base;
  vector<long long> hash, pow;
  RollingHash() {}
  RollingHash(string s, long long m, long long b) : s(s), mod(m), base(b) {
    pow.resize(s.length() + 1);
    pow[0] = 1;
    for (size_t i = 0; i < s.length(); ++i)
      (pow[i + 1] = pow[i] * base) %= mod;

    hash.assign(s.length() + 1, 0);
    for (size_t i = 0; i < s.length(); ++i)
      (hash[i + 1] = hash[i] + pow[i] * s[i]) %= mod;
  }
  long long get_hash(size_t first, size_t last) {
    return (hash[last] - hash[first] + mod) % mod;
  }
  long long make_hash(string t) {
    long long h = 0;
    for (size_t i = 0; i < t.length(); ++i)
      (h += pow[i] * t[i]) %= mod;

    return h;
  }
  long long diff(long long h, size_t pos, size_t len) {
    return (h * pow[pos] - get_hash(pos, pos + len)) % mod;
  }
  bool is_equiv(long long h, size_t pos, size_t len) {
    return !diff(h, pos, len);
  }
};

int main() {
  char buf[1 << 20];
  scanf("%s", buf);
  string T(buf);

  RollingHash rhash(T, MOD, BASE);

  scanf("%s", buf);
  string P(buf);

  long long h = rhash.make_hash(P);
  for (size_t i = 0; i + P.length() <= T.length(); ++i)
    if (rhash.is_equiv(h, i, P.length()))
      printf("%zd\\n", i);

  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <string>
#include <vector>

using namespace std;

const long long MOD = 1e9 + 7;
const long long BASE = 0x234567;

struct RollingHash {
  string s;
  long long mod, base;
  vector<long long> hash, pow;
  RollingHash() {}
  RollingHash(string s, long long m, long long b) : s(s), mod(m), base(b) {
    pow.resize(s.length() + 1);
    pow[0] = 1;
    for (size_t i = 0; i < s.length(); ++i)
      (pow[i + 1] = pow[i] * base) %= mod;

    hash.assign(s.length() + 1, 0);
    for (size_t i = 0; i < s.length(); ++i)
      (hash[i + 1] = hash[i] + pow[i] * s[i]) %= mod;
  }
  long long get_hash(size_t first, size_t last) {
    return (hash[last] - hash[first] + mod) % mod;
  }
  long long make_hash(string t) {
    long long h = 0;
    for (size_t i = 0; i < t.length(); ++i)
      (h += pow[i] * t[i]) %= mod;

    return h;
  }
  long long diff(long long h, size_t pos, size_t len) {
    return (h * pow[pos] - get_hash(pos, pos + len)) % mod;
  }
  bool is_equiv(long long h, size_t pos, size_t len) {
    return !diff(h, pos, len);
  }
};

int main() {
  char buf[1 << 10];
  scanf("%s", buf);
  string T(buf);

  RollingHash rhash(T, MOD, BASE);

  scanf("%s", buf);
  string P(buf);

  long long h = rhash.make_hash(P);
  for (size_t i = 0; i + P.length() <= T.length(); ++i)
    if (rhash.is_equiv(h, i, P.length()))
      printf("%zd\\n", i);

  return 0;
}
Problem p02248 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL

int main() {
  string T, P;
  cin >> T >> P;
  int t = T.size(), p = P.size();
  for (int i = 0; i <= t - p; i++) {
    if (T[i] == P[0]) {
      if (T.substr(i, p) == P) {
        printf("%d\\n", i);
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL

int main() {
  string T, P;
  cin >> T >> P;
  for (int i = 0; i < T.size(); i++) {
    for (int j = 0; j < P.size(); j++) {
      if (T[i + j] != P[j])
        break;
      else if (j == P.size() - 1) {
        cout << i << endl;
      }
    }
  }
  return 0;
}
Problem p02248 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
int main() {
  string T, P;
  cin >> T >> P;
  for (int i = 0; i < int(T.size() - P.size()) + 1; ++i) {
    if (P == T.substr(i, P.size())) {
      printf("%d\\n", i);
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
int main() {
  string T, P;
  int p = 0;
  cin >> T >> P;
  while (p < int(T.length() - P.length() + 1)) {
    if (p == (int)T.find(P, p)) {
      cout << (int)T.find(P, p) << endl;
    }
    p++;
  }
  return 0;
}
Problem p02248 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef unsigned long long ull;

const ull B = 10007;

void RollingHash(string T, string P) {
  int t = T.size();
  int p = P.size();

  ull x = 1;
  for (int i = 0; i < p; i++)
    x *= B;

  ull th = 0, ph = 0;
  for (int i = 0; i < p; i++)
    th = th * B + T[i];
  for (int i = 0; i < p; i++)
    ph = ph * B + P[i];

  for (int i = 0; i + p <= t; i++) {
    if (th == ph)
      printf("%d\\n", i);
    if (i + p < t)
      th = th * B + T[i + p] - T[i] * x;
  }
}

int main() {
  string T, P;
  cin >> T >> P;
  RollingHash(T, P);
  return 0;
}

Error: TLE

Fixed submission:

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

typedef unsigned long long ull;

const ull B = 10007;

void RollingHash(string T, string P) {
  int t = T.size();
  int p = P.size();

  ull x = 1;
  for (int i = 0; i < p; i++)
    x *= B;

  ull th = 0, ph = 0;
  for (int i = 0; i < p; i++)
    th = th * B + T[i];
  for (int i = 0; i < p; i++)
    ph = ph * B + P[i];

  for (int i = 0; i + p <= t; i++) {
    if (th == ph)
      cout << i << endl;
    if (i + p < t)
      th = th * B + T[i + p] - T[i] * x;
  }
}

int main() {
  string T, P;
  cin >> T >> P;
  RollingHash(T, P);
  return 0;
}
Problem p02248 (C++) — buggy submission, verdict: Time Limit Exceeded

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

void KMP(string T, string P) {
  int t = T.size();
  int p = P.size();
  int i, j, k;

  //?????????????????????
  vector<int> next(p);
  int nex;
  i = 2;
  j = 0;
  if (p == 1) {
    next[0] = -1;
    nex = 0;
  } else {
    next[0] = -1;
    next[1] = 0;
    while (i < p + 1) {
      if (P[i - 1] == P[j]) {
        if (i == p) {
          nex = j + 1;
        } else {
          next[i] = j + 1;
        }
        i++;
        j++;
      } else if (j > 0) {
        j = next[j];
      } else {
        if (i == p) {
          nex = 0;
        } else {
          next[i] = 0;
        }
        i++;
      }
    }
  }
  i = 0;
  j = 0;
  while (i < t) {
    // cout << i << " "<< j<<endl;
    if (T[i + j] == P[j]) {
      j++;
      if (j == p) {
        printf("%d\\n", i);
        i++;
        j = nex;
      }
    } else {
      i = i + j - next[j];
      if (j > 0) {
        j = next[j];
      }
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string T, P;
  cin >> T >> P;
  KMP(T, P);
  return 0;
}

Error: TLE

Fixed submission:

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

void KMP(string T, string P) {
  int t = T.size();
  int p = P.size();
  int i, j, k;

  //?????????????????????
  vector<int> next(p);
  i = 2;
  j = 0;
  next[0] = -1;
  next[1] = 0;
  while (i < p) {
    if (P[i - 1] == P[j]) {
      next[i] = j + 1;
      i++;
      j++;
    } else if (j > 0) {
      j = next[j];
    } else {
      next[i] = 0;
      i++;
    }
  }

  i = 0;
  j = 0;
  while (i < t) {
    // cout << i << " "<< j<<endl;
    if (T[i + j] == P[j]) {
      j++;
      if (j == p) {
        printf("%d\\n", i);
        i++;
        j = 0;
      }
    } else {
      i = i + j - next[j];
      if (j > 0) {
        j = next[j];
      }
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string T, P;
  cin >> T >> P;
  KMP(T, P);
  return 0;
}
Problem p02248 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
const ll B = 1000000007;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string s, t;
  cin >> s >> t;
  int n = s.length();
  int m = t.length();
  ll key = 0, num = 0, C = 1;
  FOR(i, 0, m) {
    key = key * B + t[i];
    C *= B;
  }
  for (int i = 0; i < n; i++) {
    num = num * B + s[i];
    if (i >= m) {
      num -= C * s[i - m];
    }
    // cout << i - m + 1 << " " << key << " " << num << endl;
    if (key == num) {
      cout << i - m + 1 << endl;
    }
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string t, p;
  cin >> t >> p;
  FOR(i, 0, t.length()) {
    int cnt = 0;
    for (int j = 0; j < p.length() && i + j < t.length(); j++) {
      if (t[i + j] == p[j])
        cnt++;
    }
    if (cnt == p.length()) {
      cout << i << endl;
    }
  }
  return 0;
}
Problem p02248 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define STRMAXT 1000000
#define STRMAXP 10000

void KMP(const string T, const string P) {
  int tcur = 0;
  int pcur = 2;
  int tablecur = 0;
  int plen = P.length();
  int tlen = T.length();
  int *Table = new int[plen + 1];
  Table[0] = -1;
  if (plen >= 2) {
    Table[1] = 0;
    while (pcur <= plen) {
      if (P[pcur - 1] == P[tablecur]) {
        Table[pcur] = tablecur + 1;
        pcur = pcur + 1;
        tablecur = tablecur + 1;
      } else if (tablecur > 0) {
        tablecur = Table[tablecur];
      } else {
        Table[pcur] = 0;
        pcur = pcur + 1;
      }
    }
  }
  pcur = 0;
  while (tcur + pcur < tlen) {
    if (P[pcur] == T[tcur + pcur]) {
      pcur++;
      if (pcur == plen) {
        cout << tcur << endl;
        tcur = tcur + pcur - Table[pcur];
        if (pcur > 0) {
          pcur = Table[pcur];
        }
      }
    } else {
      tcur = tcur + pcur - Table[pcur];
      if (pcur > 0) {
        pcur = Table[pcur];
      }
    }
  }
  delete[] Table;
}

int main() {
  string T;
  string P;
  std::cin >> T >> P;
  KMP(T, P);

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

#define STRMAXT 1000000
#define STRMAXP 10000

void SolvePuzzle(const char *T, const char *P) {
  int tlen = STRMAXT;
  int plen = STRMAXP;
  for (int i = 0; i < STRMAXT; i++) {
    if (T[i] == '\\0') {
      tlen = i;
      break;
    }
  }
  for (int i = 0; i < STRMAXP; i++) {
    if (P[i] == '\\0') {
      plen = i;
      break;
    }
  }
  if (tlen < plen)
    return;
  for (int i = 0; i < tlen - plen + 1; i++) {
    bool b = true;
    for (int j = 0; j < plen; j++) {
      if (T[i + j] != P[j]) {
        b = false;
        break;
      }
    }
    if (b) {
      std::cout << i << std::endl;
    }
  }
}

int main() {
  char T[STRMAXT + 2];
  char P[STRMAXP + 2];
  std::cin >> T >> P;
  SolvePuzzle(T, P);

  return 0;
}
Problem p02248 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef unsigned long long ull;

ull b = 1000000007;

int main() {
  string s, p;
  int n, m;
  ull t = 1, sh = 0, ph = 0;

  cin >> s >> p;

  n = s.length();
  m = p.length();

  if (n < m)
    return 0;

  for (int i = 0; i < m; i++)
    t *= b;
  for (int i = 0; i < m; i++)
    sh = sh * b + s[i];
  for (int i = 0; i < m; i++)
    ph = ph * b + p[i];

  for (int i = 0; i + m <= n; i++) {
    if (sh == ph)
      printf("%d\\n", i);
    if (i + m < n)
      sh = sh * b + s[i + m] - s[i] * t;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

typedef unsigned long long ull;

ull b = 1000000007;

int main() {
  string s, p;
  int n, m;
  ull t = 1, sh = 0, ph = 0;

  cin >> s >> p;

  n = s.length();
  m = p.length();

  for (int i = 0; i < m; i++)
    t *= b;
  for (int i = 0; i < m; i++)
    sh = sh * b + s[i];
  for (int i = 0; i < m; i++)
    ph = ph * b + p[i];

  for (int i = 0; i + m <= n; i++) {
    if (sh == ph)
      cout << i << endl;
    if (i + m < n)
      sh = sh * b + s[i + m] - s[i] * t;
  }

  return 0;
}
Problem p02248 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int main() {
  string T[2];
  cin >> T[0] >> T[1];

  typedef unsigned int int_type;
  const int_type base = 9973;

  vector<int_type> hashed[2], power[2];

  for (int i = 0; i < 2; i++) {
    int sz = T[i].size();
    hashed[i].assign(sz + 1, 0);
    power[i].assign(sz + 1, 0);

    hashed[i][0] = 0;
    power[i][0] = 1;
    for (int j = 0; j < sz; j++) {
      power[i][j + 1] = power[i][j] * base;
      hashed[i][j + 1] = (hashed[i][j] + T[i][j]) * base;
    }
  }

  for (int i = 0; i <= (int)(T[0].size() - T[1].size()); i++) {
    if (hashed[0][i + T[1].size()] - hashed[0][i] * power[0][T[1].size()] ==
        hashed[1][T[1].size()] - hashed[1][0] * power[0][T[1].size()]) {
      printf("%d\\n", i);
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  string T[2];
  cin >> T[0] >> T[1];

  typedef unsigned int int_type;
  const int_type base = 9973;

  vector<int_type> hashed[2], power[2];

  for (int i = 0; i < 2; i++) {
    int sz = T[i].size();
    hashed[i].assign(sz + 1, 0);
    power[i].assign(sz + 1, 0);

    hashed[i][0] = 0;
    power[i][0] = 1;
    for (int j = 0; j < sz; j++) {
      power[i][j + 1] = power[i][j] * base;
      hashed[i][j + 1] = (hashed[i][j] + T[i][j]) * base;
    }
  }

  for (int i = 0; i <= (int)(T[0].size() - T[1].size()); i++) {
    if (hashed[0][i + T[1].size()] - hashed[0][i] * power[0][T[1].size()] ==
        hashed[1][T[1].size()] - hashed[1][0] * power[0][T[1].size()]) {
      cout << i << endl;
    }
  }

  return 0;
}