Problem p02937 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <math.h>
#include <numeric>
#include <string.h>
#include <string>
#include <vector>

#define LOOP(N) for (int i = 0; i < (N); ++i)
#define REP(i, N) for (int i = 0; i < (N); ++i)
#define FOR(i, start, end) for (int i = (start); i < (end); ++i)
#define ALL(a) (a).begin(), (a).end()

using namespace std;

using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using Graph = vector<vector<int>>;

const uint ALPHABET_NUM = 'z' - 'a' + 1;

int main() {
  string s, t;
  cin >> s >> t;
  vector<vector<int>> alphabet_pos(ALPHABET_NUM);
  REP(i, (int)s.length()) { alphabet_pos[s[i] - 'a'].push_back(i); }

  // impossible
  REP(i, (int)t.length()) {
    if (alphabet_pos[t[i] - 'a'].empty()) {
      cout << -1 << endl;
      return 0;
    }
  }

  ull result = 0;
  ull now = 0, next;
  auto next_pos = alphabet_pos[0].begin();
  REP(i, (int)t.length()) {
    auto index_p = &alphabet_pos[t[i] - 'a'];
    if (i > 0 && t[i - 1] == t[i]) {
      next_pos = lower_bound(next_pos + 1, index_p->end(), now);
    } else {
      next_pos = lower_bound(ALL(*index_p), now);
    }
    if (next_pos == index_p->end()) {
      next = (*index_p)[0];
      result += (ull)s.length() + next - now;
      // cout << "non " << now << " " << next << endl;
    } else {
      next = *next_pos;
      result += next - now;
      // cout << "find " << now << " " << next << endl;
    }
    now = next;
    // cout << result << endl;
  }
  cout << result + 1;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <math.h>
#include <numeric>
#include <string.h>
#include <string>
#include <vector>

#define LOOP(N) for (int i = 0; i < (N); ++i)
#define REP(i, N) for (int i = 0; i < (N); ++i)
#define FOR(i, start, end) for (int i = (start); i < (end); ++i)
#define ALL(a) (a).begin(), (a).end()

using namespace std;

using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using Graph = vector<vector<int>>;

const uint ALPHABET_NUM = 'z' - 'a' + 1;

int main() {
  string s, t;
  cin >> s >> t;
  vector<vector<int>> alphabet_pos(ALPHABET_NUM);
  REP(i, (int)s.length()) { alphabet_pos[s[i] - 'a'].push_back(i); }

  // impossible
  REP(i, (int)t.length()) {
    if (alphabet_pos[t[i] - 'a'].empty()) {
      cout << -1 << endl;
      return 0;
    }
  }

  ull result = 0;
  ull now = 0, next;
  auto next_pos = alphabet_pos[0].begin();
  REP(i, (int)t.length()) {
    auto index_p = &alphabet_pos[t[i] - 'a'];
    if (i > 0 && t[i - 1] == t[i]) {
      next_pos = lower_bound(next_pos + 1, index_p->end(), now);
    } else {
      next_pos = lower_bound(ALL(*index_p), now);
    }
    if (next_pos == index_p->end()) {
      next = (*index_p)[0];
      next_pos = index_p->begin();
      result += (ull)s.length() + next - now;
      // cout << "non " << now << " " << next << endl;
    } else {
      next = *next_pos;
      result += next - now;
      // cout << "find " << now << " " << next << endl;
    }
    now = next;
    // cout << result << endl;
  }
  cout << result + 1;
}