Problem p02937 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int64_t i = 0; i < (int64_t)(n); i++)
#define invrep(i, n) for (int64_t i = n - 1; i >= 0; i--)
using namespace std;

//////全探索したいときに開けよ////////
vector<vector<int>> fspattern;
vector<int> fspattern_draft;
int fspcnt = 0;

void setfspattern_core(int floor, int number, int degree) {
  if (floor == degree) {
    fspattern.at(fspcnt) = fspattern_draft;
    fspcnt++;
  } else {
    rep(i, number) {
      fspattern_draft.at(floor) = i;
      setfspattern_core(floor + 1, number, degree);
    }
  }
}

void setfspattern(int number, int degree) {
  fspcnt = 0;
  int64_t array_num = 1;
  rep(i, degree) { array_num *= number; }
  fspattern = vector<vector<int>>(array_num, vector<int>(degree));
  fspattern_draft = vector<int>(degree);
  setfspattern_core(0, number, degree);
}

//////nCrのmodを求めたいときに開けよ/////////
// int64_t univMod = 1000000007;
// int64_t factnum = 100000
// vector<int64_t> modfact(factnum);
// vector<int64_t> modinvfact(factnum);

/*void setmodfact(){
  for(int i=0;i<factnum;i++){
    if(i == 0){
      modfact.at(i) = 1;
    }else{
      modfact.at(i) = (modfact.at(i-1)*i)%univMod;
    }
  }
}

int64_t calcmodpower(int64_t a,int64_t n){
  int64_t res = 1;
  while(n != 0){
    if(n & 1){
      res = (res * a) % univMod;
    }
    a = (a*a) % univMod;
    n = n >> 1;
  }
  return res;
}

int64_t calcinverse(int64_t n){
        return calcmodpower(n,univMod-2);
}

void setmodinvfact(){
  for(int i=0;i<factnum;i++){
    if(i==0){
      modinvfact.at(i) = 1;
    }else{
      modinvfact.at(i) = calcmodpower(modfact.at(i),univMod-2);
    }
  }
}*/

/*
int64_t calcGCD(int64_t a,int64_t b){
        int64_t p = a;
        int64_t q = b;
        int64_t c = a%b;
        while(c != 0){
                p = q;
                q = c;
                c = p%q;
        }
        return q;
}
*/
int64_t INF = 1000000000000000000;
int main() {
  string s, t;
  cin >> s >> t;
  string ss = s + s;
  vector<vector<int64_t>> ch(26);
  rep(i, ss.size()) { ch.at(ss.at(i) - 'a').push_back(i); }
  vector<vector<int64_t>> CH(26, vector<int64_t>(s.size(), -1));
  rep(i, 26) {
    rep(j, s.size()) {
      rep(k, ch.at(i).size()) {
        if (ch.at(i).at(k) >= j) {
          CH.at(i).at(j) = ch.at(i).at(k);
          break;
        }
      }
    }
  }
  /*rep(i,26){
    rep(j,s.size()){
      if(CH.at(i).at(j) != -1){
        cout << CH.at(i).at(j);
      }else{
        cout << "D/";
      }
    }
    cout << endl;
  }*/
  int64_t benow = 0;
  int64_t result = 0;
  bool failure = false;
  rep(i, t.size()) {
    // cout << benow << "->";
    if (CH.at(t.at(i) - 'a').at(benow) != -1) {
      result += CH.at(t.at(i) - 'a').at(benow) + 1 - benow;
      benow = (CH.at(t.at(i) - 'a').at(benow) + 1) % s.size();
    } else {
      failure = true;
      break;
    }
    // cout << benow << endl;
  }
  if (!failure) {
    cout << result << endl;
  } else {
    cout << -1 << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int64_t i = 0; i < (int64_t)(n); i++)
#define invrep(i, n) for (int64_t i = n - 1; i >= 0; i--)
using namespace std;

//////全探索したいときに開けよ////////
vector<vector<int>> fspattern;
vector<int> fspattern_draft;
int fspcnt = 0;

void setfspattern_core(int floor, int number, int degree) {
  if (floor == degree) {
    fspattern.at(fspcnt) = fspattern_draft;
    fspcnt++;
  } else {
    rep(i, number) {
      fspattern_draft.at(floor) = i;
      setfspattern_core(floor + 1, number, degree);
    }
  }
}

void setfspattern(int number, int degree) {
  fspcnt = 0;
  int64_t array_num = 1;
  rep(i, degree) { array_num *= number; }
  fspattern = vector<vector<int>>(array_num, vector<int>(degree));
  fspattern_draft = vector<int>(degree);
  setfspattern_core(0, number, degree);
}

//////nCrのmodを求めたいときに開けよ/////////
// int64_t univMod = 1000000007;
// int64_t factnum = 100000
// vector<int64_t> modfact(factnum);
// vector<int64_t> modinvfact(factnum);

/*void setmodfact(){
  for(int i=0;i<factnum;i++){
    if(i == 0){
      modfact.at(i) = 1;
    }else{
      modfact.at(i) = (modfact.at(i-1)*i)%univMod;
    }
  }
}

int64_t calcmodpower(int64_t a,int64_t n){
  int64_t res = 1;
  while(n != 0){
    if(n & 1){
      res = (res * a) % univMod;
    }
    a = (a*a) % univMod;
    n = n >> 1;
  }
  return res;
}

int64_t calcinverse(int64_t n){
        return calcmodpower(n,univMod-2);
}

void setmodinvfact(){
  for(int i=0;i<factnum;i++){
    if(i==0){
      modinvfact.at(i) = 1;
    }else{
      modinvfact.at(i) = calcmodpower(modfact.at(i),univMod-2);
    }
  }
}*/

/*
int64_t calcGCD(int64_t a,int64_t b){
        int64_t p = a;
        int64_t q = b;
        int64_t c = a%b;
        while(c != 0){
                p = q;
                q = c;
                c = p%q;
        }
        return q;
}
*/
int64_t INF = 1000000000000000000;
int main() {
  string s, t;
  cin >> s >> t;
  string ss = s + s;
  vector<vector<int64_t>> ch(26);
  rep(i, ss.size()) { ch.at(ss.at(i) - 'a').push_back(i); }
  vector<vector<int64_t>> CH(26, vector<int64_t>(s.size(), -1));
  rep(i, 26) {
    rep(j, s.size()) {
      auto itr = lower_bound(ch.at(i).begin(), ch.at(i).end(), j);
      if (itr != ch.at(i).end()) {
        CH.at(i).at(j) = *itr;
      }
    }
  }
  /*rep(i,26){
    rep(j,s.size()){
      if(CH.at(i).at(j) != -1){
        cout << CH.at(i).at(j);
      }else{
        cout << "D/";
      }
    }
    cout << endl;
  }*/
  int64_t benow = 0;
  int64_t result = 0;
  bool failure = false;
  rep(i, t.size()) {
    // cout << benow << "->";
    if (CH.at(t.at(i) - 'a').at(benow) != -1) {
      result += CH.at(t.at(i) - 'a').at(benow) + 1 - benow;
      benow = (CH.at(t.at(i) - 'a').at(benow) + 1) % s.size();
    } else {
      failure = true;
      break;
    }
    // cout << benow << endl;
  }
  if (!failure) {
    cout << result << endl;
  } else {
    cout << -1 << endl;
  }
}