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

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  string s;
  cin >> s;
  string q;
  cin >> q;

  vector<int> a;
  a.push_back(-1);
  for (int i = 1, k = -1; i <= q.size(); i++) {
    for (; k >= 0 && q[i - 1] != q[k];)
      k = a[k];
    a.push_back(++k);
  }

  vector<int> r;
  for (int i = 0, k = 0; i < s.size(); i++) {
    for (; k >= 0 && (k == q.size() || q[k] != s[i]);)
      k = a[k];
    if (++k == q.size())
      r.push_back(i + 1 - k);
  }
  for (int i = 0; i < r.size(); i++)
    printf("%d\\n", r[i]);
}

Error: WA

Fixed submission:

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  string s;
  cin >> s;
  string q;
  cin >> q;

  vector<int> a;
  a.push_back(-1);
  a.push_back(0);
  int k = 0;
  for (int i = 2; i < q.size(); i++) {
    for (; k > 0 && q[i - 1] != q[k];)
      k = a[k - 1];
    if (q[i - 1] == q[k])
      k++;
    a.push_back(k);
  }

  vector<int> r;
  int m = 0, i = 0;
  for (; m + i < s.size();) {
    if (q[i] == s[m + i]) {
      i++;
      if (i == q.size())
        r.push_back(m);
    } else {
      m = m + i - a[i];
      if (i)
        i = a[i];
    }
  }
  for (int i = 0; i < r.size(); i++)
    printf("%d\\n", r[i]);
}
Problem p02248 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
#include <string>

using namespace std;

void solve() {
  string T, P;
  cin >> T >> P;
  int size = T.size() - P.size() + 1;
  for (int i = 0; i < size; ++i) {
    i = T.find(P, i);
    if (i != string::npos) {
      printf("%d\\n", i);
    } else {
      return;
    }
  }
}

int main() {
  solve();
  return (0);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
#include <math.h>

using namespace std;

void solve() {
  string T, P;
  cin >> T >> P;
  int size = T.size() - P.size() + 1;
  for (int i = 0; i < size; ++i) {
    int pos = T.find(P, i);
    if (pos != string::npos) {
      cout << pos << endl;
      i = pos;
    } else {
      return;
    }
  }
}

int main() {
  solve();
  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);
  cin.tie(0);
  string t, p;
  cin >> t >> p;
  for (int pos = 0; pos < t.length(); pos++) {
    pos = t.find(p, pos);
    if (pos == string::npos)
      break;
    cout << pos << '\\n';
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string t, p;
  cin >> t >> p;
  int tl = t.length();
  int pl = p.length();
  int skip[256];
  for (int i = 0; i < 256; i++)
    skip[i] = pl;
  for (int i = 0; i < pl - 1; i++)
    skip[(unsigned)p[i]] = pl - 1 - i;
  for (int pos = 0; pos <= tl - pl; pos += skip[(unsigned)t[pos + pl - 1]]) {
    for (int i = pl - 1; i >= 0; i--) {
      if (t[pos + i] != p[i])
        break;
      if (i == 0)
        cout << pos << '\\n';
    }
  }
  return 0;
}
Problem p02248 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define pb emplace_back
typedef unsigned long long ll;
typedef pair<int, int> pint;

const ll x = 1000000007;
string t, p;
int main() {
  cin >> t >> p;
  ll st = t.size(), sp = p.size();
  if (st < sp)
    return 0;
  ll q = 1;
  rep(i, sp) q = q * x;
  ll ht = 0, hp = 0;
  rep(i, sp) {
    ht = (ht * x + t[i]);
    hp = (hp * x + p[i]);
  }
  rep(i, st - sp + 1) {
    if (ht == hp)
      cout << i << endl;
    if (i < st - sp)
      ht = ht * x + t[i + sp] - q * t[i];
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define pb emplace_back
typedef unsigned long long ll;
typedef pair<int, int> pint;

const ll x = 1000000007;
string t, p;
int main() {
  cin >> t >> p;
  ll st = t.size(), sp = p.size();
  ll q = 1;
  rep(i, sp) q = q * x;
  ll ht = 0, hp = 0;
  rep(i, sp) {
    ht = (ht * x + t[i]);
    hp = (hp * x + p[i]);
  }
  rep(i, st - sp + 1) {
    if (ht == hp)
      cout << i << endl;
    if (i < st - sp)
      ht = ht * x + t[i + sp] - q * t[i];
  }
  return 0;
}
Problem p02248 (C++) — buggy submission, verdict: Time Limit Exceeded

// 参考了用户 tk4114 的代码
#include <bits/stdc++.h>
using namespace std;

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);

  char T[1000005], P[10005];
  cin.getline(T, 1000005);
  int Tl = cin.gcount() - 1;
  cin.getline(P, 10005);
  int Pl = cin.gcount() - 1;
  if (Pl > Tl)
    return 0;

  int jmp[10005];
  jmp[1] = 0;
  int i = 2, j = 0;
  while (i <= Pl) {
    if (P[i - 1] == P[j]) {
      i++;
      j++;
      jmp[i - 1] = j;
    } else if (j > 0) {
      j = jmp[j];
    } else {
      jmp[i] = 0;
      i++;
    }
  }

  i = 0;
  j = 0;
  while (i < Tl) {
    if (T[i] == P[j]) {
      i++;
      j++;
      if (j == Pl) {
        cout << i - Pl << endl;
        j = jmp[j];
      }
    } else if (j > 0) {
      j = jmp[j];
    } else {
      i++;
    }
  }
}


Error: TLE

Fixed submission:

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

int main(void) {
  string T, P;
  getline(cin, T);
  getline(cin, P);
  int p, cnt = -1;
  while ((p = T.find(P)) != string::npos) {
    cnt += p + 1;
    cout << cnt << endl;
    T.erase(T.begin(), T.begin() + p + 1);
  }
}
Problem p02248 (C++) — buggy submission, verdict: Time Limit Exceeded

//
// Created by ?????°?????? on 2016/06/21.
//

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

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

Error: TLE

Fixed submission:

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

int main() {
  string t, p;
  cin >> t >> p;
  int nt = t.size();
  int np = p.size();
  if (nt < np)
    return 0;
  for (int i = 0; i < nt - np + 1; ++i) {
    if (t[i] == p[0]) {
      if (t.substr(i, np) == p)
        cout << i << endl;
    }
  }
  return 0;
}
Problem p02248 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
const int INF = 1 << 30;
const int MOD = (int)1e9 + 7;
const int MAX_N = (int)1e5 + 5;
// 文字列Sの中から文字列Tと一致する位置を返す
vector<int> RollingHash(const string &s, const string &t) {
  using ull = unsigned long long;
  int s_len = s.size(), t_len = t.size();
  if (s_len < t_len)
    return vector<int>();
  ull ss = 1;
  for (int i = 0; i < t_len; i++)
    ss *= MOD;
  ull s_hash = 0, t_hash = 0;
  for (int i = 0; i < t_len; i++)
    s_hash = s_hash * MOD + s[i];
  for (int i = 0; i < t_len; i++)
    t_hash = t_hash * MOD + t[i];
  vector<int> res;
  for (int i = 0; i + t_len <= s_len; i++) {
    if (s_hash == t_hash)
      res.push_back(i);
    if (i + t_len < s_len)
      s_hash = s_hash * MOD + s[i + t_len] - s[i] * ss;
  }
  return res;
}
signed main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string s, t;
  cin >> s >> t;
  vector<int> ans = RollingHash(s, t);
  for (int &x : ans)
    cout << x << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
int main() {
  string t, p;
  cin >> t >> p;
  rep(i, t.size()) {
    if (t.substr(i, p.size()) == p)
      cout << i << endl;
  }
  return 0;
}
Problem p02248 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>

using namespace std;

int main() {
  string S, T;
  cin >> S >> T;

  if (S.size() >= T.size()) {
    for (int i = 0; i <= S.size() - T.size(); i++) {
      if (T == S.substr(i, T.size())) {
        printf("%d\\n", i);
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>

using namespace std;

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

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

#include <cmath>
#include <iostream>
#include <stdio.h>
#include <string>

int main(void) {
  std::string T;
  std::string P;

  std::cin >> T;
  std::cin >> P;

  if (T.size() < P.size()) {
    return 0;
  }

  int index = 0;

  for (int i = 0; i <= T.size() - P.size(); i++) {
    if (P == T.substr(i, P.size())) {
      printf("%d\\n", i);
      // std::cout << i << std::endl;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <string>

int main(void) {
  std::ios::sync_with_stdio(false);

  std::string T;
  std::string P;

  std::cin >> T;
  std::cin >> P;

  if (T.size() < P.size()) {
    return 0;
  }

  int index = 0;

  for (int i = 0; i <= T.size() - P.size(); i++) {
    if (P == T.substr(i, P.size())) {
      std::cout << i << std::endl;
    }
  }

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

#include <bits/stdc++.h>
using namespace std;
string S, T;
int main() {
  cin >> S >> T;
  if (S.size() >= T.size()) {
    for (int i = 0; i <= S.size() - T.size(); i++) {
      if (T == S.substr(i, T.size())) {
        printf("%d\\n", i);
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <string>

int main(void) {
  std::ios_base::sync_with_stdio(false);

  std::string T;
  std::string P;

  std::cin >> T;
  std::cin >> P;

  if (T.size() < P.size()) {
    return 0;
  }

  int index = 0;

  for (int i = 0; i <= T.size() - P.size(); i++) {
    if (P == T.substr(i, P.size())) {
      std::cout << i << std::endl;
    }
  }

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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <vector>
using namespace std;
long long int inf = 10000000000;

int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};

vector<long long int> rollinghash(string s, int a) {
  int n = s.size();
  vector<long long int> rh(n + 1, 0);
  for (int i = 0; i < n; i++) {
    rh[i + 1] = rh[i] * a + s[i];
  }
  return rh;
}

int main() {
  string s, t;
  cin >> s >> t;
  int n = s.size();
  int m = t.size();
  vector<long long int> p(n + 1, 0);
  p[0] = 1;
  long long int a = 1000000007;
  for (int i = 0; i < n; i++) {
    p[i + 1] = p[i] * a;
  }

  vector<long long int> rhs = rollinghash(s, a);
  vector<long long int> rht = rollinghash(t, a);

  for (int i = 0; i < n; i++) {
    int l = i;
    int r = i + m - 1;
    if (rhs[r + 1] - rhs[l] * p[r - l + 1] == rht[m])
      cout << i << endl;
  }

  return 0;
}

// EOF

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <vector>
using namespace std;
long long int inf = 10000000000;

int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};

int main() {
  string t, p;
  cin >> t >> p;
  int n = t.size();
  for (int i = 0; i < n; i++) {
    if (p == t.substr(i, p.size())) {
      cout << i << endl;
    }
  }

  return 0;
}

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

#include <iostream>
using namespace std;
#include <stdio.h>
#include <string>

#define A 1995455101 // large prime number.
#define B 1995455447 // large prime number, bigger than A.

int main() {
  string T;
  cin >> T;
  int len_T = T.size();
  string P;
  cin >> P;
  int len_P = P.size();
  long long *has_T, *has_P;
  has_T = new long long[len_T];
  has_P = new long long[len_P];

  long long h = 0;
  int i; // hash coding.
  // has_T[i] = (T[0]A^{i-1} + T[1]A^{i-2} + ... + T[i-1]A + T[i]) % B.
  for (i = 0; i < len_T; i++) {
    h = (h * A + T[i]) % B;
    has_T[i] = h;
  }
  h = 0;
  // has_P[i] = (P[0]A^{i-1} + P[1]A^{i-2} + ... + P[i-1]A + P[i]) % B.
  for (i = 0; i < len_P; i++) {
    h = (h * A + P[i]) % B;
    has_P[i] = h;
  }

  // last_has = (P[0]A^{len_P-1} + P[1]A^{len_P-2} + ... + P[len_P-1]) % B.
  long long last_has = has_P[len_P - 1];
  // A^{len_P} Mod B.
  long long power, x, y, z;
  x = A, y = len_P, z = 1;
  for (; y; y /= 2) {
    if ((y & 1) != 0) {
      z = (z * x) % B;
    }
    x = (x * x) % B;
  }
  power = z;

  for (i = len_P - 1; i < len_T; i++) {
    if (has_T[i] ==
        ((i >= len_P ? has_T[i - len_P] : 0) * power + last_has) % B) {
      printf("%d\\n", i - len_P + 1);
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

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

#define A 1995455101 // large prime number.
#define B 1995455447 // large prime number, bigger than A.

int main() {
  string T;
  cin >> T;
  int len_T = T.size();
  string P;
  cin >> P;
  int len_P = P.size();
  long long *has_T, *has_P;
  has_T = new long long[len_T];
  has_P = new long long[len_P];

  long long h = 0;
  int i; // hash coding.
  // has_T[i] = (T[0]A^{i-1} + T[1]A^{i-2} + ... + T[i-1]A + T[i]) % B.
  for (i = 0; i < len_T; i++) {
    h = (h * A + T[i]) % B;
    has_T[i] = h;
  }
  h = 0;
  // has_P[i] = (P[0]A^{i-1} + P[1]A^{i-2} + ... + P[i-1]A + P[i]) % B.
  for (i = 0; i < len_P; i++) {
    h = (h * A + P[i]) % B;
    has_P[i] = h;
  }

  // last_has = (P[0]A^{len_P-1} + P[1]A^{len_P-2} + ... + P[len_P-1]) % B.
  long long last_has = has_P[len_P - 1];
  // A^{len_P} Mod B.
  long long power, x, y, z;
  x = A, y = len_P, z = 1;
  for (; y; y /= 2) {
    if ((y & 1) != 0) {
      z = (z * x) % B;
    }
    x = (x * x) % B;
  }
  power = z;

  for (i = len_P - 1; i < len_T; i++) {
    if (has_T[i] ==
        ((i >= len_P ? has_T[i - len_P] : 0) * power + last_has) % B) {
      cout << i - len_P + 1 << endl;
    }
  }
  cin >> x;
  return 0;
}
Problem p02248 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
#include <vector>

using namespace std;
using int_vec = vector<int>;

int_vec kmp_table(const string &p) {
  auto m = p.size();
  int_vec A(m + 1);

  A[0] = -1;
  int j = -1;
  for (int i = 0; i < m; ++i) {
    while (j >= 0 && p[i] != p[j])
      j = A[j];
    j++;
    A[i + 1] = j;
  }
  return A;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  string text, p;
  cin >> text >> p;

  auto A = kmp_table(p);
  auto m = p.size();
  auto n = text.size();
  int i = 0;
  int j = 0;
  while (i < n) {
    while (j >= 0 && text[i] != p[j]) {
      j = A[j];
    }
    ++i;
    ++j;
    if (j == m) {
      auto k = i - m;
      cout << k << endl;
      j = A[j];
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  string s1, s2;
  cin >> s1 >> s2;

  // kmp table
  vector<int> table(s2.size(), 0);
  for (int i = 1; i < s2.size(); ++i) {
    int j = i;
    int k = 0;
    while (j < s2.size()) {
      if (s2[j] != s2[k]) {
        break;
      }
      table[j++] = ++k;
    }
  }

  // kmp match
  for (auto i = 0; i < s1.size();) {
    auto j = i;
    auto k = 0;
    while (j < s1.size() && k < s2.size()) {
      if (s1[j] != s2[k]) {
        break;
      }
      ++j;
      ++k;
    }
    if (k >= s2.size()) {
      cout << i << endl;
      ++i;
    } else {
      i += table[k] + 1;
    }
  }

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

#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
long long B = 1717171;
int main() {
  string t, p;
  cin >> t >> p;
  long long hash[t.size() + 1];
  long long pow[t.size() + 1];
  hash[0] = 0;
  pow[0] = 1;
  for (int i = 0; i < t.size(); i++) {
    hash[i + 1] = hash[i] * B + t[i];
    pow[i + 1] = pow[i] * B;
  }
  long long base = 0, ans = 0;
  for (int i = 0; i < p.size(); i++)
    base = base * B + p[i];

  for (int i = 0; i + p.size() <= t.size(); i++) {
    int left = i, right = i + p.size() - 1;
    if (hash[right + 1] - hash[left] * pow[right - left + 1] == base)
      printf("%d\\n", i);
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
long long B = 1717171;
int main() {
  string t, p;
  cin >> t >> p;
  long long hash[t.size() + 1];
  long long pow[t.size() + 1];
  hash[0] = 0;
  pow[0] = 1;
  for (int i = 0; i < t.size(); i++) {
    hash[i + 1] = hash[i] * B + t[i];
    pow[i + 1] = pow[i] * B;
  }
  long long base = 0, ans = 0;
  for (int i = 0; i < p.size(); i++)
    base = base * B + p[i];

  for (int i = 0; i + p.size() <= t.size(); i++) {
    int left = i, right = i + p.size() - 1;
    if (hash[right + 1] - hash[left] * pow[right - left + 1] == base)
      cout << i << endl;
  }
  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 = 950527;
int n, m;
char str[1000005];
ull a[1000005];

int main() {
  scanf("%s", str);
  n = strlen(str);
  for (int i = 0; i < n; i++) {
    a[i] = str[i];
    if (i)
      a[i] += a[i - 1] * B;
  }
  scanf("%s", str);
  m = strlen(str);
  ull key = 0, num = 0, C = 1;
  for (int i = 0; i < m; i++) {
    key = key * B + str[i];
    C *= B;
  }
  for (int i = 0; i < n; i++) {
    num = a[i];
    if (i - m >= 0)
      num -= C * a[i - m];
    if (key == num) {
      printf("%d\\n", i - m + 1);
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
ull B = 950527;
int n, m;
char str[1000005];
ull a[1000005];

int main() {
  scanf("%s", str);
  n = strlen(str);
  for (int i = 0; i < n; i++) {
    a[i] = str[i];
    if (i)
      a[i] += a[i - 1] * B;
  }
  scanf("%s", str);
  m = strlen(str);
  ull key = 0, num = 0, C = 1;
  for (int i = 0; i < m; i++) {
    key = key * B + str[i];
    C *= B;
  }
  for (int i = 0; i < n; i++) {
    num = a[i];
    if (i - m >= 0)
      num -= C * a[i - m];
    if (key == num) {
      cout << i - m + 1 << endl;
    }
  }

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

// ALDS1_14_A
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
using namespace std;

int main() {
  string T, P;

  cin >> T;
  cin >> P;

  if (T.length() >= P.length())
    for (int i = 0; i <= T.length() - P.length(); i++)
      if (T.substr(i, P.length()) == P)
        printf("%d\\n", i);

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  string T, P;

  cin >> T;
  cin >> P;

  if (T.length() >= P.length())
    for (int i = 0; i <= T.length() - P.length(); i++)
      if (T.substr(i, P.length()) == P)
        cout << i << endl;

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

#include <bits/stdc++.h>

using namespace std;

#define MAX_S 1000001
#define MAX_T 10001
typedef unsigned long long ull;
const ull B = 1000000007ULL;

vector<ull> p;

vector<ull> rollingHash(char *s, int N) {
  vector<ull> hash(N + 1);
  hash[0] = 0;
  for (int i = 0; i < N; i++) {
    hash[i + 1] = hash[i] * B + s[i];
  }
  return hash;
}

int main() {
  char S[MAX_S], T[MAX_T];
  scanf("%s %s", S, T);
  int N = strlen(S);
  int M = strlen(T);
  p.resize(N + 1);
  p[0] = 1;
  for (int i = 0; i < N; i++) {
    p[i + 1] = p[i] * B;
  }
  if (N < M)
    return 0;
  vector<ull> h = rollingHash(S, N);
  vector<ull> h2 = rollingHash(T, M);
  for (int i = 0; i < N; i++) {
    if (i + M > N)
      continue;
    int L = i, R = i + M - 1;
    if (h[R + 1] - h[L] * p[R - L + 1] == h2[M]) {
      printf("%d\\n", i);
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef unsigned long long ull;
const ull B = 1000000007ULL;

vector<ull> p;

vector<ull> rollingHash(string &s) {
  int N = s.size();
  vector<ull> hash(N + 1);
  hash[0] = 0;
  for (int i = 0; i < N; i++) {
    hash[i + 1] = hash[i] * B + s[i];
  }
  return hash;
}

int main() {
  string S, T;
  cin >> S >> T;
  int N = S.size();
  int M = T.size();
  p.resize(N + 1);
  p[0] = 1;
  for (int i = 0; i < N; i++) {
    p[i + 1] = p[i] * B;
  }
  if (N < M)
    return 0;
  vector<ull> h = rollingHash(S);
  vector<ull> h2 = rollingHash(T);
  for (int i = 0; i < N; i++) {
    if (i + M > N)
      continue;
    int L = i, R = i + M - 1;
    if (h[R + 1] - h[L] * p[R - L + 1] == h2[M]) {
      cout << i << endl;
    }
  }
  return 0;
}
Problem p02248 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef unsigned long long int ull;

const ull B = 1e9 + 7; // base of hash

bool contain(string s, string a) {
  int sl = s.length(), al = a.length();
  if (sl < al)
    return false;

  ull t = 1;
  for (int i = 0; i < al; i++)
    t *= B;

  ull sh = 0, ah = 0;
  for (int i = 0; i < al; i++) {
    sh = sh * B + s[i];
    ah = ah * B + a[i];
  }

  if (sh == ah)
    return true;

  for (int i = 0; i + al < sl; i++) {
    sh = sh * B + s[i + al] - s[i] * t;
    if (sh == ah)
      return true;
  }

  return false;
}

vector<int> match(string s, string a) {
  int sl = s.length(), al = a.length();
  vector<int> ans;
  if (sl < al)
    return ans;
  ull t = 1;
  ull ah = 0, sh = 0;
  for (int i = 0; i < al; i++) {
    sh = sh * B + s[i];
    ah = ah * B + a[i];
    t *= B;
  }

  if (sh == ah)
    ans.push_back(0);

  for (int i = 0; i + al < sl; i++) {
    sh = sh * B + s[i + al] - s[i] * t;
    if (sh == ah)
      ans.push_back(i + 1);
  }
  return ans;
}

int main() {
  string s, a;
  cin >> s >> a;
  vector<int> m = match(s, a);
  for (int i = 0; i < m.size(); i++) {
    cout << m[i] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  string t, p;
  cin >> t >> p;
  int co, sto = t.size();
  int ans[1000000] = {};
  for (int i = 0; i < p.size(); i++) {
    co = 0;
    for (int j = 0; j < sto; j++) {
      if (p[i] == t[ans[j] ? ans[j] : j]) {
        ans[co++] = (ans[j] ? ans[j] : j) + 1;
      }
    }
    sto = co;
  }
  for (int i = 0; i < sto; i++)
    cout << ans[i] - p.size() << endl;
  return 0;
}