Problem p02937 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define bit(n) (1 << (n))
#define ii(x)                                                                  \
  int x;                                                                       \
  cin >> (x)
#define ii2(x, y)                                                              \
  int x, y;                                                                    \
  cin >> (x) >> (y)
#define ii3(x, y, z)                                                           \
  int x, y, z;                                                                 \
  cin >> (x) >> (y) >> (z)
#define yn(x, ok, ng) cout << ((x) ? (ok) : (ng)) << endl
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
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;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
int const INF = 1001001001;

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

  string s, t;
  cin >> s >> t;
  s += s;
  vvi mp(26);
  rep(i, SZ(s)) { mp[s[i] - 'a'].push_back(i); }

  // tにあってsにない文字があるとtを作ることはできない
  rep(i, SZ(t)) {
    if (!SZ(mp[t[i] - 'a'])) {
      cout << -1 << endl;
      return 0;
    }
  }

  ll cur = 0;
  ll ans = 0;
  rep(i, SZ(t)) {
    vi v = mp[t[i] - 'a'];
    ll iter = lower_bound(v.begin(), v.end(), cur) - v.begin();
    ll idx = v[iter];
    ans += idx - cur + 1;
    if (idx >= SZ(s) / 2)
      idx -= SZ(s) / 2;
    cur = idx + 1;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define bit(n) (1 << (n))
#define ii(x)                                                                  \
  int x;                                                                       \
  cin >> (x)
#define ii2(x, y)                                                              \
  int x, y;                                                                    \
  cin >> (x) >> (y)
#define ii3(x, y, z)                                                           \
  int x, y, z;                                                                 \
  cin >> (x) >> (y) >> (z)
#define yn(x, ok, ng) cout << ((x) ? (ok) : (ng)) << endl
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
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;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
int const INF = 1001001001;

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

  string s, t;
  cin >> s >> t;
  s += s;
  vvi mp(26);
  rep(i, SZ(s)) { mp[s[i] - 'a'].push_back(i); }

  // tにあってsにない文字があるとtを作ることはできない
  rep(i, SZ(t)) {
    if (!SZ(mp[t[i] - 'a'])) {
      cout << -1 << endl;
      return 0;
    }
  }

  ll cur = 0;
  ll ans = 0;
  rep(i, SZ(t)) {
    ll iter = lower_bound(mp[t[i] - 'a'].begin(), mp[t[i] - 'a'].end(), cur) -
              mp[t[i] - 'a'].begin();
    ll idx = mp[t[i] - 'a'][iter];
    ans += idx - cur + 1;
    if (idx >= SZ(s) / 2)
      idx -= SZ(s) / 2;
    cur = idx + 1;
  }
  cout << ans << endl;
  return 0;
}