Problem p02937 (C++)

Buggy submission (status: Runtime Error):
/*author : Yashvardhan
Sunday, August 18, 2019
6:46 PM
*/

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

typedef long long ll;

#define int ll
#define pb push_back
#define vi vector<int>
#define pi pair<int, int>
#define vpi vector<pi>
#define ff first
#define ss second
#define mp make_pair
#define endl '\n'
#define all(a) a.begin(), a.end()
#define initialise(a, x) memset(a, x, sizeof(a))
#define rev(Y) reverse(all(Y))
#define printArr(name, from, to)                                               \
  for (int x = from; x < to; x++)                                              \
    cout << name[x] << " ";

#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define _CRT_SECURE_NO_WARNINGS

#ifdef __APPLE__
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << 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 debug(...)
#endif

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < v.size(); ++i)
    os << v[i] << " ";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &v) {
  for (auto it : v)
    os << it << " ";
  return os;
}
template <typename T, typename S>
ostream &operator<<(ostream &os, const pair<T, S> &v) {
  os << v.ff << " " << v.ss;
  return os;
}

const int mod = 1e9 + 7;
const int inf = 2e18;
const int ninf = -2e18;

const int N = 1e5 + 5;

int arr[26][N];
int focc[N];

int takemod(int a) { return ((a % mod) + mod) % mod; }

int pow(int a, int b, int m) {
  int ans = 1;
  while (b) {
    if (b & 1)
      ans = (ans * a) % m;
    b /= 2;
    a = (a * a) % m;
  }
  return ans;
}

int modinv(int a) { return takemod(pow(takemod(a), mod - 2, mod)); }

signed main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
#ifdef __APPLE__
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("error.txt", "w", stderr);
#endif

  time_t t1, t2;
  t1 = clock();

  string s, t;
  cin >> s >> t;

  int n = s.size();

  bool possi = true;

  initialise(focc, -1);
  initialise(arr, 0);

  vi occ[26];

  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < 26; j++)
      arr[j][i] = arr[j][i - 1];
    int curr = s[i - 1] - 'a';
    arr[curr][i]++;
    if (focc[curr] == -1)
      focc[curr] = i;
    occ[curr].pb(i);
  }

  for (auto i : t) {
    int curr = i - 'a';
    if (arr[curr][n] == 0) {
      possi = false;
      break;
    }
  }

  if (!possi) {
    cout << -1 << endl;
    return 0;
  }

  int cpos = 0;
  int ans = 0;
  for (int i = 0; i < t.size(); i++) {
    int curr = t[i] - 'a';
    if (cpos == 0) {
      ans++;
      cpos = focc[curr];
    } else {
      if (arr[curr][n] - arr[curr][cpos - 1] == 0) {
        cpos = focc[curr];
        ans++;
      } else {
        int ind = lower_bound(all(occ[curr]), cpos + 1) - occ[curr].begin();
        cpos = occ[curr][ind];
      }
    }
  }

  ans = ans * n;

  ans -= (n - cpos);

  cout << ans << endl;

  t2 = clock();
  cerr << endl << t2 - t1 << endl;

  return 0;
}


Fixed submission:
/*author : Yashvardhan
Sunday, August 18, 2019
6:46 PM
*/

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

typedef long long ll;

#define int ll
#define pb push_back
#define vi vector<int>
#define pi pair<int, int>
#define vpi vector<pi>
#define ff first
#define ss second
#define mp make_pair
#define endl '\n'
#define all(a) a.begin(), a.end()
#define initialise(a, x) memset(a, x, sizeof(a))
#define rev(Y) reverse(all(Y))
#define printArr(name, from, to)                                               \
  for (int x = from; x < to; x++)                                              \
    cout << name[x] << " ";

#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define _CRT_SECURE_NO_WARNINGS

#ifdef __APPLE__
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << 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 debug(...)
#endif

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < v.size(); ++i)
    os << v[i] << " ";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &v) {
  for (auto it : v)
    os << it << " ";
  return os;
}
template <typename T, typename S>
ostream &operator<<(ostream &os, const pair<T, S> &v) {
  os << v.ff << " " << v.ss;
  return os;
}

const int mod = 1e9 + 7;
const int inf = 2e18;
const int ninf = -2e18;

const int N = 1e5 + 5;

int arr[26][N];
int focc[N];

int takemod(int a) { return ((a % mod) + mod) % mod; }

int pow(int a, int b, int m) {
  int ans = 1;
  while (b) {
    if (b & 1)
      ans = (ans * a) % m;
    b /= 2;
    a = (a * a) % m;
  }
  return ans;
}

int modinv(int a) { return takemod(pow(takemod(a), mod - 2, mod)); }

signed main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
#ifdef __APPLE__
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("error.txt", "w", stderr);
#endif

  time_t t1, t2;
  t1 = clock();

  string s, t;
  cin >> s >> t;

  int n = s.size();

  bool possi = true;

  initialise(focc, -1);
  initialise(arr, 0);

  vi occ[26];

  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < 26; j++)
      arr[j][i] = arr[j][i - 1];
    int curr = s[i - 1] - 'a';
    arr[curr][i]++;
    if (focc[curr] == -1)
      focc[curr] = i;
    occ[curr].pb(i);
  }

  for (auto i : t) {
    int curr = i - 'a';
    if (arr[curr][n] == 0) {
      possi = false;
      break;
    }
  }

  if (!possi) {
    cout << -1 << endl;
    return 0;
  }

  int cpos = 0;
  int ans = 0;
  for (int i = 0; i < t.size(); i++) {
    int curr = t[i] - 'a';
    if (cpos == 0) {
      ans++;
      cpos = focc[curr];
    } else {
      if (arr[curr][n] - arr[curr][cpos] == 0) {
        cpos = focc[curr];
        ans++;
      } else {
        int ind = lower_bound(all(occ[curr]), cpos + 1) - occ[curr].begin();
        cpos = occ[curr][ind];
      }
    }
  }

  ans = ans * n;

  ans -= (n - cpos);

  cout << ans << endl;

  t2 = clock();
  cerr << endl << t2 - t1 << endl;

  return 0;
}