Problem p02247 (C++) — buggy submission, verdict: 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 BM {
public:
  // pattern???text????????????????????????
  std::string text, pattern;
  BM(std::string tex, std::string pat) : text(tex), pattern(pat){};

  int skip[256];

  void find(std::vector<int> &ret) {
    int patSize = pattern.size();

    rep(i, 256) skip[i] = patSize;
    rep(i, patSize) skip[pattern[i]] = (patSize - i - 1);

    int i = patSize - 1;
    while (i < text.size()) {
      int check = 0;
      while (check < patSize && text[i - check] == pattern[patSize - check - 1])
        ++check;
      if (check == patSize) {
        ret.push_back(i - patSize + 1);
        ++i;
      } else
        i = std::max(i + 1, i - check + skip[text[i - check]]);
    }
  }
};

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

  BM bm(a, b);
  std::vector<int> v;
  bm.find(v);

  rep(i, v.size()) { std::cout << v[i] << std::endl; }

  return 0;
}

Error: TLE

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 BM {
public:
  // pattern???text????????????????????????
  std::string text, pattern;
  BM(std::string tex, std::string pat) : text(tex), pattern(pat){};

  int skip[27];

  void find(std::vector<int> &ret) {
    int patSize = pattern.size();

    rep(i, 27) skip[i] = patSize;
    rep(i, patSize) skip[pattern[i] - 'a'] = (patSize - i - 1);

    int i = patSize - 1;
    while (i < text.size()) {
      int check = 0;
      while (check < patSize && text[i - check] == pattern[patSize - check - 1])
        ++check;
      if (check == patSize) {
        ret.push_back(i - patSize + 1);
        ++i;
      } else
        i += skip[text[i - check] - 'a'];
    }
  }
};

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

  BM bm(a, b);
  std::vector<int> v;
  bm.find(v);

  rep(i, v.size()) { std::cout << v[i] << std::endl; }

  return 0;
}