Problem p02937 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using std::vector;
template <typename T> void ndarray(vector<T> &vec, int len) { vec.resize(len); }
template <typename T, typename... Args>
void ndarray(vector<T> &vec, int len, Args... args) {
  vec.resize(len);
  for (auto &v : vec)
    ndarray(v, args...);
}

int main() {
  // std::ifstream in("in01.txt");
  // std::cin.rdbuf(in.rdbuf());

  std::string s, t;
  std::cin >> s >> t;

  long long ans = 0;
  int n = s.length();
  bool flag = true;

  vector<std::set<int>> chlist;
  chlist.resize(26);

  for (int i = 1; i <= 2 * n; i++) {
    int ch = s[(2 * n - i) % n] - 'a';
    chlist[ch].insert(2 * n - i + 1);
  }
  vector<vector<long long>> dp;
  ndarray(dp, 26, 2 * n + 1);
  for (int ch = 0; ch < 26; ch++) {
    auto itr = chlist[ch].upper_bound(0);
    if (itr == chlist[ch].end()) {
      dp[ch][0] = -1;
    } else {
      dp[ch][0] = *itr;
    }
  }
  for (int next = 1; next <= 2 * n + 1; next++) {
    for (int ch = 0; ch < 26; ch++) {
      auto itr = chlist[ch].upper_bound(next);
      if (itr == chlist[ch].end()) {
        dp[ch][next] = -1;
      } else {
        dp[ch][next] = *itr - next;
      }
    }
  }
  int next = 0;
  for (auto ch : t) {
    long long add = dp[ch - 'a'][next];
    if (add == -1) {
      flag = false;
      break;
    } else {
      ans += add;
    }
    next = (next + add - 1) % n + 1;
    // std::cout << add << ", " << next << ", " << ans << std::endl;
  }

  if (flag) {
    std::cout << ans << std::endl;
  } else {
    std::cout << -1 << std::endl;
  }

  return 0;
}


Fixed submission:
#include <cmath>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using std::vector;
template <typename T> void ndarray(vector<T> &vec, int len) { vec.resize(len); }
template <typename T, typename... Args>
void ndarray(vector<T> &vec, int len, Args... args) {
  vec.resize(len);
  for (auto &v : vec)
    ndarray(v, args...);
}

int main() {
  // std::ifstream in("in01.txt");
  // std::cin.rdbuf(in.rdbuf());

  std::string s, t;
  std::cin >> s >> t;

  long long ans = 0;
  int n = s.length();
  bool flag = true;

  vector<std::set<int>> chlist;
  chlist.resize(26);

  for (int i = 1; i <= 2 * n; i++) {
    int ch = s[(2 * n - i) % n] - 'a';
    chlist[ch].insert(2 * n - i + 1);
  }
  vector<vector<long long>> dp;
  ndarray(dp, 26, 2 * n + 1);
  for (int ch = 0; ch < 26; ch++) {
    auto itr = chlist[ch].upper_bound(0);
    if (itr == chlist[ch].end()) {
      dp[ch][0] = -1;
    } else {
      dp[ch][0] = *itr;
    }
  }
  for (int next = 1; next <= 2 * n; next++) {
    for (int ch = 0; ch < 26; ch++) {
      auto itr = chlist[ch].upper_bound(next);
      if (itr == chlist[ch].end()) {
        dp[ch][next] = -1;
      } else {
        dp[ch][next] = *itr - next;
      }
    }
  }
  int next = 0;
  for (auto ch : t) {
    long long add = dp[ch - 'a'][next];
    if (add == -1) {
      flag = false;
      break;
    } else {
      ans += add;
    }
    next = (next + add - 1) % n + 1;
    // std::cout << add << ", " << next << ", " << ans << std::endl;
  }

  if (flag) {
    std::cout << ans << std::endl;
  } else {
    std::cout << -1 << std::endl;
  }

  return 0;
}