Problem p02937 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
using ll = long long;
using ld = long double;
const char nl = '\n';
/* -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*-
 * -*- -*- -*- -*- */

#include <bits/extc++.h>
namespace pbds = __gnu_pbds;
template <typename key, typename val = pbds::null_type,
          typename comp = less<key>>
using ostree = pbds::tree<key, val, comp, pbds::rb_tree_tag,
                          pbds::tree_order_statistics_node_update>;
using vi = vector<int>;
using vll = vector<ll>;
#define all(x) std::begin(x), std::end(x)
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename... Args, template <typename...> typename T>
string to_string(T<Args...> const &);
string to_string(string const &s) { return '"' + s + '"'; }
string to_string(char const &c) { return to_string(string(1, c)); }
string to_string(char const *c) { return to_string(string(c)); }
string to_string(bool const &b) { return (b ? "T" : "F"); }
template <typename... Args> string to_string(pair<Args...> const &p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename... Args, template <typename...> typename T>
string to_string(T<Args...> const &S) {
  string s = "{";
  for (auto const &e : S)
    s += " " + to_string(e);
  s += " }";
  return s;
}
template <typename Tail> void debug_out(Tail t) {
  cerr << " " << to_string(t) << " ]" << endl;
}
template <typename Head, typename... Tail> void debug_out(Head h, Tail... t) {
  cerr << " " << to_string(h) << ",";
  debug_out(t...);
}
#define pr(...) cerr << "[" << (#__VA_ARGS__) << "] : [", debug_out(__VA_ARGS__)
template <typename T> void dbr(T lb, T ub) {
  cerr << '{';
  for (auto it = lb; it != ub; it++)
    cerr << ' ' << to_string(*it);
  cerr << " }" << endl;
}
template <typename T, typename Comp = less<T>>
bool smin(T &mem, T const &v, Comp const &cmp = Comp()) {
  return cmp(v, mem) ? mem = v, true : false;
}
template <typename T, typename Comp = less<T>>
bool smax(T &mem, T const &v, Comp const &cmp = Comp()) {
  return cmp(mem, v) ? mem = v, true : false;
}
/* -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*-
 * -*- -*- -*- -*- */

string s, t;
int a[26][100100], n, m;

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

  cin >> s >> t;
  {
    set<char> S;
    for (char c : s)
      S.emplace(c);
    for (char c : t)
      if (S.find(c) == S.end()) {
        cout << -1 << endl;
        return 0;
      }
  }

  n = s.length(), m = t.length();

  for (int z = 0; z < 2; z++) {
    for (int i = n - 1; i >= 0; i--) {
      for (char c = 'a'; c <= 'z'; c++) {
        a[c - 'a'][i] = a[c - 'a'][(i + 1) % n];
      }
      a[s[i] - 'a'][i] = i;
    }
  }

  int pos = 0, cur = 0;
  for (;;) {
    for (; cur < m and t[cur] == s[pos % n]; cur++, pos++)
      ;
    if (cur == m)
      break;
    char c = t[cur];
    int npos = a[c - 'a'][pos % n];
    if (npos > (pos % n))
      pos += npos - (pos % n);
    else
      pos += (n - (pos % n) + npos);
  }
  cout << pos << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
using ll = long long;
using ld = long double;
const char nl = '\n';
/* -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*-
 * -*- -*- -*- -*- */

#include <bits/extc++.h>
namespace pbds = __gnu_pbds;
template <typename key, typename val = pbds::null_type,
          typename comp = less<key>>
using ostree = pbds::tree<key, val, comp, pbds::rb_tree_tag,
                          pbds::tree_order_statistics_node_update>;
using vi = vector<int>;
using vll = vector<ll>;
#define all(x) std::begin(x), std::end(x)
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename... Args, template <typename...> typename T>
string to_string(T<Args...> const &);
string to_string(string const &s) { return '"' + s + '"'; }
string to_string(char const &c) { return to_string(string(1, c)); }
string to_string(char const *c) { return to_string(string(c)); }
string to_string(bool const &b) { return (b ? "T" : "F"); }
template <typename... Args> string to_string(pair<Args...> const &p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename... Args, template <typename...> typename T>
string to_string(T<Args...> const &S) {
  string s = "{";
  for (auto const &e : S)
    s += " " + to_string(e);
  s += " }";
  return s;
}
template <typename Tail> void debug_out(Tail t) {
  cerr << " " << to_string(t) << " ]" << endl;
}
template <typename Head, typename... Tail> void debug_out(Head h, Tail... t) {
  cerr << " " << to_string(h) << ",";
  debug_out(t...);
}
#define pr(...) cerr << "[" << (#__VA_ARGS__) << "] : [", debug_out(__VA_ARGS__)
template <typename T> void dbr(T lb, T ub) {
  cerr << '{';
  for (auto it = lb; it != ub; it++)
    cerr << ' ' << to_string(*it);
  cerr << " }" << endl;
}
template <typename T, typename Comp = less<T>>
bool smin(T &mem, T const &v, Comp const &cmp = Comp()) {
  return cmp(v, mem) ? mem = v, true : false;
}
template <typename T, typename Comp = less<T>>
bool smax(T &mem, T const &v, Comp const &cmp = Comp()) {
  return cmp(mem, v) ? mem = v, true : false;
}
/* -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*-
 * -*- -*- -*- -*- */

string s, t;
int a[26][100100], n, m;

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

  cin >> s >> t;
  {
    set<char> S;
    for (char c : s)
      S.emplace(c);
    for (char c : t)
      if (S.find(c) == S.end()) {
        cout << -1 << endl;
        return 0;
      }
  }

  n = s.length(), m = t.length();

  for (int z = 0; z < 2; z++) {
    for (int i = n - 1; i >= 0; i--) {
      for (char c = 'a'; c <= 'z'; c++) {
        a[c - 'a'][i] = a[c - 'a'][(i + 1) % n];
      }
      a[s[i] - 'a'][i] = i;
    }
  }

  ll pos = 0, cur = 0;
  for (;;) {
    for (; cur < m and t[cur] == s[pos % n]; cur++, pos++)
      ;
    if (cur == m)
      break;
    char c = t[cur];
    int npos = a[c - 'a'][pos % n];
    if (npos > (pos % n))
      pos += npos - (pos % n);
    else
      pos += (n - (pos % n) + npos);
  }
  cout << pos << endl;
}