Problem p02937 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define endl "\n"
#define REP(i, a, n) for (int i = a; i < n; ++i)
#define REPR(i, a, n) for (int i = a; i > n; --i)
#define RUP(a, b) ((a + b - 1) / (b))
#define ALL(v) (v).begin(), (v).end()
#define pb push_back
#define mp make_pair
#define mt make_tuple

typedef long long ll;
typedef pair<int, int> Pii;
typedef tuple<int, int, int> Tiii;
typedef vector<int> Vi;
typedef vector<Vi> VVi;
typedef vector<Pii> VPii;
typedef vector<string> Vs;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> void YesNo(T a) { cout << (a ? "Yes" : "No") << endl; }
template <class T> void YESNO(T a) { cout << (a ? "YES" : "NO") << endl; }
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }
void uniq(Vi &v) {
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
}

signed main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  string s, t;
  int ans = 0;
  cin >> s >> t;

  map<char, Vi> ma;
  map<char, int> mab;
  REP(i, 0, s.size()) {
    ma[s[i]].pb(i + 1);
    mab[s[i]]++;
  }
  int now = 0;
  REP(i, 0, t.size()) {
    if (mab[t[i]] == 0) {
      cout << -1 << endl;
      return 0;
    }
    REP(j, 0, ma[t[i]].size() + 1) {
      if (j == ma[t[i]].size()) {
        ans += s.size();
        now = ma[t[i]][0];
        break;
      }
      if (now < ma[t[i]][j]) {
        now = ma[t[i]][j];
        break;
      }
    }
  }
  cout << ans + now << endl;
  return 0;
}


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

#define int long long
#define endl "\n"
#define REP(i, a, n) for (int i = a; i < n; ++i)
#define REPR(i, a, n) for (int i = a; i > n; --i)
#define RUP(a, b) ((a + b - 1) / (b))
#define ALL(v) (v).begin(), (v).end()
#define pb push_back
#define mp make_pair
#define mt make_tuple

typedef long long ll;
typedef pair<int, int> Pii;
typedef tuple<int, int, int> Tiii;
typedef vector<int> Vi;
typedef vector<Vi> VVi;
typedef vector<Pii> VPii;
typedef vector<string> Vs;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> void YesNo(T a) { cout << (a ? "Yes" : "No") << endl; }
template <class T> void YESNO(T a) { cout << (a ? "YES" : "NO") << endl; }
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }
void uniq(Vi &v) {
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
}

signed main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  string s, t;
  int ans = 0;
  cin >> s >> t;

  map<char, Vi> ma;
  map<char, int> mab;
  REP(i, 0, s.size()) {
    ma[s[i]].pb(i + 1);
    mab[s[i]]++;
  }
  int now = 0;
  REP(i, 0, t.size()) {
    if (mab[t[i]] == 0) {
      cout << -1 << endl;
      return 0;
    }
    auto iter = upper_bound(ALL(ma[t[i]]), now);
    if (iter == ma[t[i]].end()) {
      ans += s.size();
      now = ma[t[i]][0];
    } else {
      now = *iter;
    }
  }
  cout << ans + now << endl;
  return 0;
}