Problem p02937 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define ll long long
#define db long double
#define ii pair<int, int>
#define vi vector<int>
#define fi first
#define se second
#define sz(a) (int)(a).size()
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define mp make_pair
#define FN(i, n) for (int i = 0; i < (int)(n); ++i)
#define FEN(i, n) for (int i = 1; i <= (int)(n); ++i)
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repv(i, a, b) for (int i = b - 1; i >= a; i--)
#define SET(A, val) memset(A, val, sizeof(A))
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
// order_of_key (val): returns the no. of values less than val
// find_by_order (k): returns the kth largest element.(0-based)
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif
const int N = 100005;
int nxt[N][26];
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  string s, t;
  cin >> s >> t;
  string s2 = s;
  s2 += s2;
  rep(j, 0, 26) nxt[sz(s2)][j] = -1;
  repv(i, 0, sz(s2)) {
    int curr = s2[i] - 'a';
    rep(j, 0, 26) {
      if (curr == j)
        nxt[i][j] = i;
      else
        nxt[i][j] = nxt[i + 1][j];
    }
  }
  repv(i, 0, sz(s)) rep(j, 0, 26) {
    if (nxt[i][j] != -1)
      nxt[i][j] %= sz(s);
  }
  ll ans = 0;
  int curr = 0;
  rep(i, 0, sz(t)) {
    int id = t[i] - 'a';
    int p = nxt[curr][id];
    if (p == -1) {
      cout << -1 << endl;
      exit(0);
    }
    if (p < curr)
      ans += sz(s) - curr + p + 1;
    else
      ans += p - curr + 1;
    curr = p + 1;
    curr %= sz(s);
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define ll long long
#define db long double
#define ii pair<int, int>
#define vi vector<int>
#define fi first
#define se second
#define sz(a) (int)(a).size()
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define mp make_pair
#define FN(i, n) for (int i = 0; i < (int)(n); ++i)
#define FEN(i, n) for (int i = 1; i <= (int)(n); ++i)
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repv(i, a, b) for (int i = b - 1; i >= a; i--)
#define SET(A, val) memset(A, val, sizeof(A))
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
// order_of_key (val): returns the no. of values less than val
// find_by_order (k): returns the kth largest element.(0-based)
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif
const int N = 200005;
int nxt[N][26];
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  string s, t;
  cin >> s >> t;
  string s2 = s;
  s2 += s2;
  rep(j, 0, 26) nxt[sz(s2)][j] = -1;
  repv(i, 0, sz(s2)) {
    int curr = s2[i] - 'a';
    rep(j, 0, 26) {
      if (curr == j)
        nxt[i][j] = i;
      else
        nxt[i][j] = nxt[i + 1][j];
    }
  }
  repv(i, 0, sz(s)) rep(j, 0, 26) {
    if (nxt[i][j] != -1)
      nxt[i][j] %= sz(s);
  }
  ll ans = 0;
  int curr = 0;
  rep(i, 0, sz(t)) {
    int id = t[i] - 'a';
    int p = nxt[curr][id];
    if (p == -1) {
      cout << -1 << endl;
      exit(0);
    }
    if (p < curr)
      ans += sz(s) - curr + p + 1;
    else
      ans += p - curr + 1;
    curr = p + 1;
    curr %= sz(s);
  }
  cout << ans << endl;
  return 0;
}