prithivMLmods/Coder-Stat huggingface dataset:
Problem p02937 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
// g++ -std=c++11

using namespace std;
typedef long long ll;

#define rep(i, n) for (long long i = 0; i < (n); ++i)
#define DIV 1000000007 // 10^9+7

ll memo[30];
ll memo2[100005][30];

int main() {
  string s, t;
  cin >> s >> t;

  string ss = s + s;

  rep(i, 30) memo[i] = -1;

  for (ll i = ss.size() - 1; i >= 0; i--) {
    rep(j, 30) {
      if (memo[j] != -1) {
        memo2[i][j] = memo[j] - i;
      } else {
        memo2[i][j] = -1;
      }
    }
    ll ch = ss[i] - 'a';
    memo[ch] = i;
  }

  ll cur = -1;
  rep(i, s.size()) {
    if (t[0] == s[i]) {
      cur = i;
      break;
    }
  }
  if (cur == -1) {
    cout << "-1" << endl;
    return 0;
  }

  for (ll i = 1; i < t.size(); i++) {
    ll ch = t[i] - 'a';
    if (memo2[cur % s.size()][ch] == -1) {
      cout << "-1" << endl;
      return 0;
    }
    cur += memo2[cur % s.size()][ch];
  }
  cout << cur + 1 << endl;
}


Fixed submission:
#include <bits/stdc++.h>
// g++ -std=c++11

using namespace std;
typedef long long ll;

#define rep(i, n) for (long long i = 0; i < (n); ++i)
#define DIV 1000000007 // 10^9+7

ll memo[30];
ll memo2[200005][30];

int main() {
  string s, t;
  cin >> s >> t;

  string ss = s + s;

  rep(i, 30) memo[i] = -1;

  for (ll i = ss.size() - 1; i >= 0; i--) {
    rep(j, 30) {
      if (memo[j] != -1) {
        memo2[i][j] = memo[j] - i;
      } else {
        memo2[i][j] = -1;
      }
    }
    ll ch = ss[i] - 'a';
    memo[ch] = i;
  }

  ll cur = -1;
  rep(i, s.size()) {
    if (t[0] == s[i]) {
      cur = i;
      break;
    }
  }
  if (cur == -1) {
    cout << "-1" << endl;
    return 0;
  }

  for (ll i = 1; i < t.size(); i++) {
    ll ch = t[i] - 'a';
    if (memo2[cur % s.size()][ch] == -1) {
      cout << "-1" << endl;
      return 0;
    }
    cur += memo2[cur % s.size()][ch];
  }
  cout << cur + 1 << endl;
}
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 chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int inf = 1 << 21;
const ll INF = 1LL << 60;
const ll mod = 1e9 + 7;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};

// DAME:
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string s, t;
  cin >> s >> t;
  s += s;
  map<char, vector<int>> sm;
  rep(i, s.size()) { sm[s[i]].push_back(i); }
  rep(i, s.size()) { sort(sm[s[i]].begin(), sm[s[i]].end()); }
  ll ans = 0;
  int index = -1;
  rep(i, t.size()) {
    auto itr = lower_bound(sm[t[i]].begin(), sm[t[i]].end(), index + 1);
    if (itr == sm[t[i]].end()) {
      ans += s.size() - index;
      if (sm[t[i]].size() == 0) {
        cout << -1 << endl;
        return 0;
      }
      index = sm[t[i]][0];
      ans += index;
    } else {
      ans += *itr - index;
      index = *itr;
    }
  }

  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int inf = 1 << 21;
const ll INF = 1LL << 60;
const ll mod = 1e9 + 7;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};

// DAME:
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string s, t;
  cin >> s >> t;
  s += s;
  map<char, vector<int>> sm;
  rep(i, s.size()) sm[s[i]].push_back(i);
  ll ans = 0;
  int index = -1;
  rep(i, t.size()) {
    auto itr = lower_bound(sm[t[i]].begin(), sm[t[i]].end(), index + 1);
    if (itr == sm[t[i]].end()) {
      ans += s.size() - index;
      if (sm[t[i]].size() == 0) {
        cout << -1 << endl;
        return 0;
      }
      index = sm[t[i]][0];
      ans += index;
    } else {
      ans += *itr - index;
      index = *itr;
    }
  }

  cout << ans << endl;
  return 0;
}
Problem p02937 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

string s, t;
int nex[101010][27];

int main() {
  cin >> s >> t;
  int s_sz = s.size();
  int t_sz = t.size();
  s = s + s;
  for (int i = 0; i < 26; i++)
    nex[2 * s_sz][i] = 2 * s_sz;
  for (int i = 2 * s_sz - 1; 0 <= i; i--) {
    for (int j = 0; j < 26; j++) {
      nex[i][j] = nex[i + 1][j];
    }
    nex[i][s[i] - 'a'] = i + 1;
  }

  long long cur = 0;
  for (int i = 0; i < t.size(); i++) {
    char c = t[i] - 'a';
    int x = cur % s_sz;
    if (nex[x][c] == 2 * s_sz) {
      cout << -1 << endl;
      return 0;
    }
    cur += nex[x][c] - x;
  }

  cout << cur << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

string s, t;
int nex[201010][27];

int main() {
  cin >> s >> t;
  int s_sz = s.size();
  int t_sz = t.size();
  s = s + s;
  for (int i = 0; i < 26; i++)
    nex[2 * s_sz][i] = 2 * s_sz;
  for (int i = 2 * s_sz - 1; 0 <= i; i--) {
    for (int j = 0; j < 26; j++) {
      nex[i][j] = nex[i + 1][j];
    }
    nex[i][s[i] - 'a'] = i + 1;
  }

  long long cur = 0;
  for (int i = 0; i < t.size(); i++) {
    char c = t[i] - 'a';
    int x = cur % s_sz;
    if (nex[x][c] == 2 * s_sz) {
      cout << -1 << endl;
      return 0;
    }
    cur += nex[x][c] - x;
  }

  cout << cur << endl;

  return 0;
}
Problem p02937 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
int main() {
  string s, t;
  cin >> s >> t;
  int s_len = s.length();
  s = s + s;

  vector<vector<int>> next(s_len, vector<int>('z' - 'a', -1));

  ll out = 0;
  int j = 0;
  int j0, jstop;
  for (int i = 0; i < t.length(); i++) {
    j0 = j;
    if (next[j0][t[i] - 'a'] >= 0) {
      j = next[j0][t[i] - 'a'];
    } else {
      jstop = j + s_len;
      for (; t[i] != s[j];) {
        if (j == jstop) {
          cout << -1 << endl;
          return 0;
        }
        j++;
      }
      next[j0][t[i] - 'a'] = j;
    }
    j++;
    out += j - j0;
    if (j >= s_len)
      j -= s_len;
  }

  cout << out << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
int main() {
  string s, t;
  cin >> s >> t;
  int s_len = s.length();
  s = s + s;

  vector<vector<int>> next(s_len, vector<int>('z' - 'a' + 1, -1));

  ll out = 0;
  int j = 0;
  int j0, jstop;
  for (int i = 0; i < t.length(); i++) {
    j0 = j;
    if (next[j0][t[i] - 'a'] >= 0) {
      j = next[j0][t[i] - 'a'];
    } else {
      jstop = j + s_len;
      for (; t[i] != s[j];) {
        if (j == jstop) {
          cout << -1 << endl;
          return 0;
        }
        j++;
      }
      next[j0][t[i] - 'a'] = j;
    }
    j++;
    out += j - j0;
    if (j >= s_len)
      j -= s_len;
  }

  cout << out << endl;
  return 0;
}
Problem p02937 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

vector<vector<int>> point(26, vector<int>());

int main() {
  string s, t;
  cin >> s >> t;
  for (int i = 0; i < s.length(); i++) {
    point[s[i] - 'a'].push_back(i);
  }
  for (int i = 0; i < t.length(); i++) {
    if (!point[t[i] - 'a'].size()) {
      cout << -1 << endl;
      return 0;
    }
  }
  int now = -1;
  long long ans = 0;
  for (int i = 0; i < t.length(); i++) {
    int flag = 0;
    for (int j = 0; j < point[t[i] - 'a'].size(); j++) {
      if (point[t[i] - 'a'][j] > now) {
        ans += point[t[i] - 'a'][j] - now;
        now = point[t[i] - 'a'][j];
        flag = 1;
        break;
      }
    }
    if (!flag) {
      ans += s.length() - 1 - now;
      now = -1;
      ans += point[t[i] - 'a'][0] - now;
      now = point[t[i] - 'a'][0];
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

vector<vector<int>> point(26, vector<int>());

int main() {
  string s, t;
  cin >> s >> t;
  for (int i = 0; i < s.length(); i++) {
    point[s[i] - 'a'].push_back(i);
  }
  for (int i = 0; i < t.length(); i++) {
    if (!point[t[i] - 'a'].size()) {
      cout << -1 << endl;
      return 0;
    }
  }
  int now = -1;
  long long ans = 0;
  for (int i = 0; i < t.length(); i++) {
    auto it = lower_bound(point[t[i] - 'a'].begin(), point[t[i] - 'a'].end(),
                          now + 1);
    if (it != point[t[i] - 'a'].end()) {
      ans += *it - now;
      now = *it;
    } else {
      ans += s.length() - 1 - now + point[t[i] - 'a'][0] + 1;
      now = point[t[i] - 'a'][0];
    }
  }
  cout << ans << endl;
}
Problem p02937 (C++)

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

int main() {
  string s, t;
  cin >> s >> t;
  vector<vector<int>> positions(26);
  for (int i = 0; i < s.size(); ++i)
    positions[s[i] - 'a'].push_back(i);
  ll ans = 0;
  int tpos = 0;
  int pointer = -1;
  while (tpos < t.size()) {
    if (positions[t[tpos] - 'a'].size() == 0) {
      cout << -1 << endl;
      return 0;
    }
    int flag = false;
    for (int spos : positions[t[tpos] - 'a']) {
      if (pointer < spos) {
        // OK
        ans += spos - pointer;
        pointer = spos;
        flag = true;
        break;
      }
    }
    if (!flag) {
      // next loop
      ans += (positions[t[tpos] - 'a'][0] + s.size() - pointer);
      pointer = positions[t[tpos] - 'a'][0];
    }
    tpos++;
  }
  cout << ans << endl;
  return 0;
}

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

int main() {
  string s, t;
  cin >> s >> t;
  vector<vector<int>> positions(26);
  for (int i = 0; i < s.size(); ++i)
    positions[s[i] - 'a'].push_back(i);
  ll ans = 0;
  int tpos = 0;
  int pointer = -1;
  while (tpos < t.size()) {
    if (positions[t[tpos] - 'a'].size() == 0) {
      cout << -1 << endl;
      return 0;
    }
    auto itr = upper_bound(positions[t[tpos] - 'a'].begin(),
                           positions[t[tpos] - 'a'].end(), pointer);
    if (itr != positions[t[tpos] - 'a'].end()) {
      // OK
      ans += *itr - pointer;
      pointer = *itr;
    } else {
      // next loop
      ans += (positions[t[tpos] - 'a'][0] + s.size() - pointer);
      pointer = positions[t[tpos] - 'a'][0];
    }
    tpos++;
  }
  cout << ans << endl;
  return 0;
}
Problem p02937 (C++)

Buggy submission (status: Runtime Error):
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

// template
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (a); i > (b); i--)
#define ALL(v) (v).begin(), (v).end()
typedef long long int ll;
typedef pair<ll, ll> P;
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 <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  fill((T *)array, (T *)(array + N), val);
}
const int inf = INT_MAX / 2;
const ll INF = LLONG_MAX / 2;
// template end

int main() {
  string s, t;
  cin >> s >> t;
  int n = s.size(), m = t.size();
  ll nxt[1010][26];
  Fill(nxt, INF);
  rep(i, 0, n) nxt[i][s[i] - 'a'] = 0LL;
  rrep(i, 2 * n, 0) rep(j, 0, 26) {
    chmin(nxt[(i - 1 + n) % n][j], nxt[i % n][j] + 1LL);
  }
  ll ans = 0;
  rep(i, 0, m) {
    ll val = nxt[ans % n][t[i] - 'a'];
    cerr << val << endl;
    if (val == INF) {
      printf("-1");
      return 0;
    }
    ans += val + 1;
  }
  printf("%lld\n", ans);
  return 0;
}


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

// template
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (a); i > (b); i--)
#define ALL(v) (v).begin(), (v).end()
typedef long long int ll;
typedef pair<ll, ll> P;
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 <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  fill((T *)array, (T *)(array + N), val);
}
const int inf = INT_MAX / 2;
const ll INF = LLONG_MAX / 2;
// template end

int main() {
  string s, t;
  cin >> s >> t;
  int n = s.size(), m = t.size();
  ll nxt[100010][26];
  Fill(nxt, INF);
  rep(i, 0, n) nxt[i][s[i] - 'a'] = 0LL;
  rrep(i, 2 * n, 0) rep(j, 0, 26) {
    chmin(nxt[(i - 1 + n) % n][j], nxt[i % n][j] + 1LL);
  }
  ll ans = 0;
  rep(i, 0, m) {
    ll val = nxt[ans % n][t[i] - 'a'];
    cerr << val << endl;
    if (val == INF) {
      printf("-1");
      return 0;
    }
    ans += val + 1;
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p02937 (Python)

Buggy submission (status: Runtime Error):
# Library
import bisect

alp = "abcdefghijklmnopqrstuvwxyz"
#

s = input()
t = input()
ss = s * 2
index = -1
cir = 0
letter = []
res = 0

for i in range(len(ss)):
    letter.append([ss[i], i])

# Sort出来る.
letter.sort()

for i in range(len(t)):
    search = [t[i], index]
    tmp_ind = bisect.bisect(letter, search)
    # print("now searching " + str(search))
    # print("find: " + str(tmp_ind))
    # print("found: " + str(letter[tmp_ind]))
    if t[i] != letter[tmp_ind][0]:
        # Not exist
        res = -1
        break
    index += letter[tmp_ind][1] - index
    if index >= len(s):
        cir += 1
        index -= len(s)
    # print("cir,index : " + str(cir) + " " + str(index))
    res = cir * len(s) + index + 1

print(res)


Fixed submission:
# Library
import bisect

alp = "abcdefghijklmnopqrstuvwxyz"
#

s = input()
t = input()
ss = s * 2
index = -1
cir = 0
letter = []
res = 0

for i in range(len(ss)):
    letter.append([ss[i], i])

# Sort出来る.
letter.sort()

for i in range(len(t)):
    search = [t[i], index]
    tmp_ind = bisect.bisect(letter, search)
    # print("now searching " + str(search))
    # print("find: " + str(tmp_ind))
    # print("found: " + str(letter[tmp_ind]))
    if tmp_ind == len(ss):
        res = -1
        break
    if t[i] != letter[tmp_ind][0]:
        # Not exist
        res = -1
        break
    index += letter[tmp_ind][1] - index
    if index >= len(s):
        cir += 1
        index -= len(s)
    # print("cir,index : " + str(cir) + " " + str(index))
    res = cir * len(s) + index + 1

print(res)
Problem p02937 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>

using namespace std;

using ll = long long;

int main() {
  string s, t;
  cin >> s >> t;
  ll s_size = s.length();
  ll t_size = t.length();

  ll s_table[26][s_size];
  ll s_cur[26];
  bool used[26];

  for (int i = 0; i < 26; i++) {
    for (int j = 0; j < s_size; j++) {
      s_table[i][j] = -1;
    }
    s_cur[i] = -1;
    used[i] = false;
  }

  for (int i = s_size - 1; i >= 0; i--) {
    ll c = s.at(i) - 'a';
    s_cur[c] = i;
    used[c] = true;
    for (int j = 0; j < 26; j++) {
      s_table[j][i] = s_cur[j];
    }
  }

  ll ans = 0;
  ll s_pos = 0;
  for (int i = 0; i < t_size; i++) {
    int c = t.at(i) - 'a';
    if (used[c] == false) {
      cout << -1 << endl;
      return 0;
    }
    int cur = s_table[c][s_pos];
    // cout << "cur1:" << cur << endl;
    if (cur == -1) {
      ans += (s_size - s_pos);
      s_pos = 0;
      cur = s_table[c][s_pos];
    }
    // cout << "cur2:" << cur << endl;
    // cout << "s_pos:" << cur << endl;
    ans += cur - s_pos + 1;
    s_pos = cur + 1;
    // cout << "ans:" << ans << endl;
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <iostream>
#include <string>

using namespace std;

using ll = long long;

int main() {
  string s, t;
  cin >> s >> t;
  ll s_size = s.length();
  ll t_size = t.length();

  ll s_table[26][s_size];
  ll s_cur[26];
  bool used[26];

  for (int i = 0; i < 26; i++) {
    for (int j = 0; j < s_size; j++) {
      s_table[i][j] = -1;
    }
    s_cur[i] = -1;
    used[i] = false;
  }

  for (int i = s_size - 1; i >= 0; i--) {
    ll c = s.at(i) - 'a';
    s_cur[c] = i;
    used[c] = true;
    for (int j = 0; j < 26; j++) {
      s_table[j][i] = s_cur[j];
    }
  }

  ll ans = 0;
  ll s_pos = 0;
  for (int i = 0; i < t_size; i++) {
    int c = t.at(i) - 'a';
    if (used[c] == false) {
      cout << -1 << endl;
      return 0;
    }
    int cur = s_table[c][s_pos];
    // cout << "cur1:" << cur << endl;
    if (cur == -1) {
      ans += (s_size - s_pos);
      s_pos = 0;
      cur = s_table[c][s_pos];
    }
    // cout << "cur2:" << cur << endl;
    // cout << "s_pos:" << cur << endl;
    ans += cur - s_pos + 1;
    s_pos = (cur + 1) % s_size;
    // cout << "ans:" << ans << endl;
  }

  cout << ans << endl;

  return 0;
}
Problem p02937 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;

string s, t;
vector<int> dict[30];
long long ans;

long long search(char t) {
  long long x;
  long long y = 0;
  x = ans % s.length();
  if (dict[(int)t - 97][0] > x) {
    return dict[(int)t - 97][0] - x + y;
  } else if (dict[(int)t - 97][dict[(int)t - 97].size() - 1] <= x) {
    y = s.length() - x;
    x = 0;
    if (dict[(int)t - 97][0] > x) {
      return dict[(int)t - 97][0] + y;
    }
  }

  long long ub = dict[(int)t - 97].size() - 1;
  long long lb = 0;
  long long mid = (ub + lb) / 2;

  while (ub - lb > 1) {
    if (dict[(int)t - 97][mid] > x) {
      ub = mid;
    } else {
      lb = mid;
    }
  }
  return dict[(int)t - 97][ub] - x + y;
}

int main() {

  cin >> s >> t;

  ans = 0;

  for (int i = 0; i < s.length(); i++) {
    dict[(int)s[i] - 97].push_back(i + 1);
  }

  for (int i = 0; i < t.length(); i++) {
    if (dict[(int)t[i] - 97].empty()) {
      ans = -1;
      break;
    } else if (dict[(int)t[i] - 97].size() == 1) {
      long long x;
      x = ans % s.length();
      if (dict[(int)t[i] - 97][0] > x) {
        x = dict[(int)t[i] - 97][0] - x;
        ans += x;
      } else {
        ans += (s.length() - x) + dict[(int)t[i] - 97][0];
      }
    } else {
      ans += search(t[i]);
    }
    // cout << ans << endl;
  }

  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;

string s, t;
vector<int> dict[30];
long long ans;

long long search(char t) {
  long long x;
  long long y = 0;
  x = ans % s.length();
  if (dict[(int)t - 97][0] > x) {
    return dict[(int)t - 97][0] - x + y;
  } else if (dict[(int)t - 97][dict[(int)t - 97].size() - 1] <= x) {
    y = s.length() - x;
    x = 0;
    if (dict[(int)t - 97][0] > x) {
      return dict[(int)t - 97][0] + y;
    }
  }

  long long ub = dict[(int)t - 97].size() - 1;
  long long lb = 0;
  long long mid = (ub + lb) / 2;

  while (ub - lb > 1) {
    if (dict[(int)t - 97][mid] > x) {
      ub = mid;
    } else {
      lb = mid;
    }
    mid = (ub + lb) / 2;
  }
  return dict[(int)t - 97][ub] - x + y;
}

int main() {

  cin >> s >> t;

  ans = 0;

  for (int i = 0; i < s.length(); i++) {
    dict[(int)s[i] - 97].push_back(i + 1);
  }

  for (int i = 0; i < t.length(); i++) {
    if (dict[(int)t[i] - 97].empty()) {
      ans = -1;
      break;
    } else if (dict[(int)t[i] - 97].size() == 1) {
      long long x;
      x = ans % s.length();
      if (dict[(int)t[i] - 97][0] > x) {
        x = dict[(int)t[i] - 97][0] - x;
        ans += x;
      } else {
        ans += (s.length() - x) + dict[(int)t[i] - 97][0];
      }
    } else {
      ans += search(t[i]);
    }
    // cout << ans << endl;
  }

  cout << ans << endl;
}
Problem p02937 (C++)

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

ll findNext(ll curr_idx, ll len, vector<ll> pos) {
  ll multiplier = curr_idx / len;
  curr_idx = curr_idx % len;
  ll next_pos = lower_bound(pos.begin(), pos.end(), curr_idx) - pos.begin();

  if (next_pos == pos.size())
    return len * (multiplier + 1) + pos[0];
  return len * multiplier + pos[next_pos];
}

bool ifPossible(string s, string t) {
  for (int i = 0; i < 26; ++i) {
    char ch = (char)(i + 'a');
    bool find_s = (s.find(ch) != string ::npos);
    bool find_t = (t.find(ch) != string ::npos);

    if (find_s == false && find_t == true)
      return false;
  }

  return true;
}

int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

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

  if (!ifPossible(s, t)) {
    cout << -1;
    return 0;
  }

  vector<ll> pos[26];
  ll s_len = s.length(), t_len = t.length();

  for (int i = 0; i < s_len; ++i)
    pos[s[i] - 'a'].push_back(i);

  ll start = 0, t_pos = 0;
  while (t_pos < t_len) {
    start = findNext(start, s_len, pos[t[t_pos] - 'a']) + 1;
    ++t_pos;
  }

  cout << start;
}

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

ll findNext(ll curr_idx, ll len, vector<ll> pos) {
  ll multiplier = curr_idx / len;
  curr_idx = curr_idx % len;
  ll next_pos = lower_bound(pos.begin(), pos.end(), curr_idx) - pos.begin();

  if (next_pos == pos.size())
    return len * (multiplier + 1) + pos[0];
  return len * multiplier + pos[next_pos];
}

bool ifPossible(string s, string t) {
  for (int i = 0; i < 26; ++i) {
    char ch = (char)(i + 'a');
    bool find_s = (s.find(ch) != string ::npos);
    bool find_t = (t.find(ch) != string ::npos);

    if (find_s == false && find_t == true)
      return false;
  }

  return true;
}

int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

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

  if (!ifPossible(s, t)) {
    cout << -1;
    return 0;
  }

  vector<ll> pos[26];
  ll s_len = s.length(), t_len = t.length();

  for (int i = 0; i < s_len; ++i)
    pos[s[i] - 'a'].push_back(i);

  ll start = 0, t_pos = 0;
  while (t_pos < t_len) {
    ll len = s_len, curr_idx = start, multiplier = curr_idx / len;
    curr_idx = curr_idx % len;
    ll next_pos = lower_bound(pos[t[t_pos] - 'a'].begin(),
                              pos[t[t_pos] - 'a'].end(), curr_idx) -
                  pos[t[t_pos] - 'a'].begin();

    if (next_pos == pos[t[t_pos] - 'a'].size())
      start = len * (multiplier + 1) + pos[t[t_pos] - 'a'][0] + 1;
    else
      start = len * multiplier + pos[t[t_pos] - 'a'][next_pos] + 1;
    ++t_pos;
  }

  cout << start;
}
Problem p02937 (C++)

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

int main() {
  string S, T;
  cin >> S >> T;

  int64_t s, t;
  s = S.size();
  t = T.size();

  S = S + S;

  vector<vector<int64_t>> a(1e+5 + 10, vector<int64_t>(26, 2 * s));
  for (int64_t i = 2 * s - 1; i >= 0; --i) {
    for (int64_t j = 0; j < 26; ++j) {
      a[i][j] = a[i + 1][j];
    }
    a[i][S[i] - 'a'] = i + 1;
  }

  int64_t cur = 0;
  bool flg = false;
  for (auto b : T) {
    int64_t c = b - 'a';
    int64_t pos = cur % s;
    if (a[pos][c] == 2 * s) {
      flg = true;
      break;
    }
    cur = cur + a[pos][c] - pos;
  }

  if (flg) {
    cout << -1 << endl;
  } else {
    cout << cur << endl;
  }

  return 0;
}

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

int main() {
  string S, T;
  cin >> S >> T;

  int64_t s, t;
  s = S.size();
  t = T.size();

  S = S + S;

  vector<vector<int64_t>> a(2 * s + 10, vector<int64_t>(26, 2 * s));
  for (int64_t i = 2 * s - 1; i >= 0; --i) {
    for (int64_t j = 0; j < 26; ++j) {
      a[i][j] = a[i + 1][j];
    }
    a[i][S[i] - 'a'] = i + 1;
  }

  int64_t cur = 0;
  bool flg = false;
  for (auto b : T) {
    int64_t c = b - 'a';
    int64_t pos = cur % s;
    if (a[pos][c] == 2 * s) {
      flg = true;
      break;
    }
    cur = cur + a[pos][c] - pos;
  }

  if (flg) {
    cout << -1 << endl;
  } else {
    cout << cur << endl;
  }

  return 0;
}
Problem p02937 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
char a[maxn];
char b[maxn];
vector<int> c[30];
int main() {
  scanf("%s %s", a, b);
  int n = strlen(a);
  int m = strlen(b);
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    c[a[i] - 'a'].push_back(i + 1);
  }
  int j;
  int x = b[0] - 'a';
  if (c[x].empty()) {
    printf("-1\n");
    return 0;
  }
  int k = c[x][0];
  sum += k;
  for (int i = 1; i < m; i++) {
    x = b[i] - 'a';
    j = upper_bound(c[x].begin(), c[x].end(), k) - c[x].begin();
    if (j == c[x].end() - c[x].begin())
      sum += c[x][0] + n - k, k = c[x][0];
    else {
      sum += c[x][j] - k;
      k = c[x][j];
    }
  }
  printf("%lld\n", sum);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
char a[maxn];
char b[maxn];
vector<int> c[30];
int main() {
  scanf("%s %s", a, b);
  int n = strlen(a);
  int m = strlen(b);
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    c[a[i] - 'a'].push_back(i + 1);
  }
  int j;
  int x = b[0] - 'a';
  if (c[x].empty()) {
    printf("-1\n");
    return 0;
  }
  int k = c[x][0];
  sum += k;
  for (int i = 1; i < m; i++) {
    x = b[i] - 'a';
    if (c[x].empty()) {
      printf("-1\n");
      return 0;
    }
    j = upper_bound(c[x].begin(), c[x].end(), k) - c[x].begin();
    if (j == c[x].end() - c[x].begin())
      sum += c[x][0] + n - k, k = c[x][0];
    else {
      sum += c[x][j] - k;
      k = c[x][j];
    }
  }
  printf("%lld\n", sum);
}
Problem p02937 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define pp pair<int, int>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define ll long long
ll MOD = 1000000007;
ll mod = 998244353;
int inf = 1000000000;
ll INF = 10000000000000000;
int main() {
  string s, t;
  cin >> s >> t;
  vector<vector<ll>> o(26, vector<ll>(0));
  rep(i, s.size()) {
    int x = s.at(i) - 'a';
    o.at(x).push_back(i);
  }
  ll ans = 0, e = -1;
  rep(i, t.size()) {
    int x = t.at(i) - 'a';
    if (o.at(x).size() == 0) {
      cout << -1 << endl;
      return 0;
    } else {
      vector<ll> y = o.at(x);
      auto itt = upper_bound(y.begin(), y.end(), e);
      if (itt == y.end()) {
        ans += y.at(0) + s.size() - e;
        e = y.at(0);
      } else {
        ans += *itt - e;
        e = *itt;
      }
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pp pair<int, int>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define ll long long
ll MOD = 1000000007;
ll mod = 998244353;
int inf = 1000000000;
ll INF = 10000000000000000;
int main() {
  string s, t;
  cin >> s >> t;
  vector<vector<ll>> o(26, vector<ll>(0));
  rep(i, s.size()) {
    int x = s.at(i) - 'a';
    o.at(x).push_back(i);
  }
  ll ans = 0, e = -1;
  rep(i, t.size()) {
    int x = t.at(i) - 'a';
    if (o.at(x).size() == 0) {
      cout << -1 << endl;
      return 0;
    } else {
      auto itt = upper_bound(o.at(x).begin(), o.at(x).end(), e);
      if (itt == o.at(x).end()) {
        ans += o.at(x).at(0) + s.size() - e;
        e = o.at(x).at(0);
      } else {
        ans += *itt - e;
        e = *itt;
      }
    }
  }
  cout << ans << endl;
}
Problem p02937 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;

int main() {
  string s, t;
  cin >> s >> t;
  string ss = s + s;
  vector<vector<int>> nex(s.length(), vector<int>(26, -1));
  vector<int> idx(26, -1);
  for (size_t i = ss.length() - 1; i >= s.length(); --i) {
    idx[ss[i] - 'a'] = i;
  }
  for (size_t i = s.length() - 1; i--;) {
    nex[i] = idx;
    idx[s[i] - 'a'] = i;
  }
  long long ans = 0;
  size_t i = idx[t[0] - 'a'];
  if (idx[t[0] - 'a'] < 0) {
    cout << -1 << endl;
    return 0;
  }
  ans += i + 1;
  for (size_t j = 1; j < t.length(); ++j) {
    int c = t[j] - 'a';
    if (nex[i][c] < 0) {
      cout << -1 << endl;
      return 0;
    }
    ans += (nex[i][c] - i);
    i = nex[i][c] % s.length();
  }
  cout << ans << endl;
}


Fixed submission:
#include <iostream>
#include <vector>
using namespace std;

int main() {
  string s, t;
  cin >> s >> t;
  string ss = s + s;
  vector<vector<int>> nex(s.length(), vector<int>(26, -1));
  vector<int> idx(26, -1);
  for (size_t i = ss.length() - 1; i >= s.length(); --i) {
    idx[ss[i] - 'a'] = i;
  }
  for (size_t i = s.length() - 1; i < s.length(); --i) {
    nex[i] = idx;
    idx[s[i] - 'a'] = i;
  }
  long long ans = 0;
  size_t i = idx[t[0] - 'a'];
  if (idx[t[0] - 'a'] < 0) {
    cout << -1 << endl;
    return 0;
  }
  ans += i + 1;
  for (size_t j = 1; j < t.length(); ++j) {
    int c = t[j] - 'a';
    if (nex[i][c] < 0) {
      cout << -1 << endl;
      return 0;
    }
    ans += (nex[i][c] - i);
    i = nex[i][c] % s.length();
  }
  cout << ans << endl;
}
Problem p02937 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int main() {
  string s, t;
  cin >> t >> s;
  vector<vector<long>> tc(26);

  long n = t.size();
  for (long i = 0; i < n; i++) {
    // cout << t[i] << " " << t[i] - 'a' << endl;
    tc[t[i] - 'a'].push_back(i);
  }

  n = s.size();
  long cnt = 0;
  long m = -1;
  vector<vector<long>> ntc = tc;
  for (long i = 0; i < n; i++) {

    if (tc[s[i] - 'a'].size() <= 0) {
      cout << -1 << endl;
      return 0;
    }
    bool flag = false;
    for (auto j : tc[s[i] - 'a']) {
      // cout << j << " " << m << endl;
      if (j <= m)
        continue;
      else {
        flag = true;
        m = j;
        break;
      }
    }

    if (!flag) {
      cnt++;
      m = tc[s[i] - 'a'][0];
    }
  }

  // cout << cnt << " " << m << endl;
  cout << (long long)t.size() * cnt + m + 1 << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int main() {
  string s, t;
  cin >> t >> s;
  vector<vector<long>> tc(26);

  long n = t.size();
  for (long i = 0; i < n; i++) {
    // cout << t[i] << " " << t[i] - 'a' << endl;
    tc[t[i] - 'a'].push_back(i);
  }

  n = s.size();
  long cnt = 0;
  long m = -1;
  vector<vector<long>> ntc = tc;
  for (long i = 0; i < n; i++) {

    if (tc[s[i] - 'a'].size() <= 0) {
      cout << -1 << endl;
      return 0;
    }
    bool flag = false;
    int ind = s[i] - 'a';
    auto it = upper_bound(tc[ind].begin(), tc[ind].end(), m);
    if (it != tc[ind].end()) {
      flag = true;
      m = *it;
    }

    if (!flag) {
      cnt++;
      m = tc[s[i] - 'a'][0];
    }
  }

  // cout << cnt << " " << m << endl;
  cout << (long long)t.size() * cnt + m + 1 << endl;
}
Problem p02937 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define EM 1000000
using namespace std;
using LL = long long;
using P = pair<LL, LL>;
LL LINF = 1e18;
int INF = 1e9;
LL mod = 1e9 + 7;
using vint = vector<int>;
using vLL = vector<LL>;
using vvint = vector<vector<int>>;
using vvLL = vector<vector<LL>>;
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;
}

int main() {
  string s, t;
  cin >> s >> t;
  vector<vector<int>> dp(26, vector<int>(2 * s.size(), INF));
  string ss = s + s;
  for (int i = 0; i < 26; i++) {
    int c = 'a' + i;
    int l = 0;
    for (int j = 0; j < ss.size(); j++) {
      if (ss[j] == c) {
        while (l < j) {
          dp[i][l] = j - l;
          l++;
        }
      }
      if (l >= s.size())
        break;
    }
  }

  LL ans = 0;
  bool ju = false;
  for (int j = 0; j < s.size(); j++) {
    if (t[0] == s[j]) {
      ju = true;
      ans += j + 1;
      int r = j;
      for (int k = 1; k < t.size(); k++) {
        int d = dp[t[k] - 'a'][r];
        if (d == INF) {
          ju = false;
          break;
        }
        ans += d;
        r += d;
        r %= s.size();
      }
      if (ju)
        break;
    }
  }
  if (ju)
    cout << ans << endl;
  else
    cout << -1 << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define EM 1000000
using namespace std;
using LL = long long;
using P = pair<LL, LL>;
LL LINF = 1e18;
int INF = 1e9;
LL mod = 1e9 + 7;
using vint = vector<int>;
using vLL = vector<LL>;
using vvint = vector<vector<int>>;
using vvLL = vector<vector<LL>>;
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;
}

int main() {
  string s, t;
  cin >> s >> t;
  vector<vector<int>> dp(26, vector<int>(2 * s.size(), INF));
  string ss = s + s;
  for (int i = 0; i < 26; i++) {
    int c = 'a' + i;
    int l = 0;
    for (int j = 0; j < ss.size(); j++) {
      if (ss[j] == c) {
        while (l < j) {
          dp[i][l] = j - l;
          l++;
        }
      }
      if (l >= s.size())
        break;
    }
  }

  LL ans = 0;
  bool ju = false;
  for (int j = 0; j < s.size(); j++) {
    if (t[0] == s[j]) {
      ju = true;
      ans += j + 1;
      int r = j;
      for (int k = 1; k < t.size(); k++) {
        int d = dp[t[k] - 'a'][r];
        if (d == INF) {
          ju = false;
          break;
        }
        ans += d;
        r += d;
        r %= s.size();
      }
      break;
    }
  }
  if (ju)
    cout << ans << endl;
  else
    cout << -1 << endl;
}
Problem p02937 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)

int main() {
  string s, t;
  cin >> s >> t;
  vector<vector<int>> memo(26);
  int cur = 0; // 今何週目を見ているか
  int dex = 0; // 次に何文字目をみるか
  ll ans = 0;
  for (int i = 0; i < s.size(); i++) {
    memo[s[i] - 'a'].push_back(i);
  }
  int judge = 1;
  for (int i = 0; i < t.size(); i++) {
    if (memo[t[i] - 'a'].size() == 0) {
      judge = 0;
      cout << -1 << endl;
      break;
    }
    int curjudge = 1;
    for (int j = 0; j < memo[t[i] - 'a'].size(); j++) {
      if (dex <= memo[t[i] - 'a'][j]) {
        curjudge = 0;
        dex = memo[t[i] - 'a'][j] + 1;
        break;
      }
    }
    if (curjudge) {
      cur++;
      dex = memo[t[i] - 'a'][0] + 1;
    }
  }
  if (judge)
    cout << cur * s.size() + dex << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)

int main() {
  string s, t;
  cin >> s >> t;
  vector<vector<int>> memo(26);
  int cur = 0; // 今何週目を見ているか
  int dex = 0; // 次に何文字目をみるか
  ll ans = 0;
  for (int i = 0; i < s.size(); i++) {
    memo[s[i] - 'a'].push_back(i);
  }
  int judge = 1;
  for (int i = 0; i < t.size(); i++) {
    if (memo[t[i] - 'a'].size() == 0) {
      judge = 0;
      cout << -1 << endl;
      break;
    }
    int curjudge = 1;
    /*for(int j = 0; j < memo[t[i]-'a'].size(); j++){
        if(dex <= memo[t[i]-'a'][j]){
            curjudge = 0;
            dex = memo[t[i]-'a'][j] + 1;
            break;
        }
    }*/
    int index =
        int(lower_bound(memo[t[i] - 'a'].begin(), memo[t[i] - 'a'].end(), dex) -
            memo[t[i] - 'a'].begin());
    if (index < memo[t[i] - 'a'].size() && dex <= memo[t[i] - 'a'][index]) {
      dex = memo[t[i] - 'a'][index] + 1;
      curjudge = 0;
    }
    if (curjudge) {
      cur++;
      dex = memo[t[i] - 'a'][0] + 1;
    }
  }
  if (judge)
    cout << cur * s.size() + dex << endl;
}
Problem p02937 (C++)

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

struct Fast {
  Fast() {
    std::cin.tie(0);
    ios::sync_with_stdio(false);
  }
} fast;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vll = vector<long long>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
#define MOD 1000000007

int main() {
  string s, t, u;
  cin >> s >> t;
  u = s + s;
  ll x = s.size(), y = t.size();
  vll hu[26];
  rep(i, 2 * x) {
    int j = s[i] - 'a';
    hu[j].pb(i);
  }

  vector<vll> next(x, vll(26, -1));
  // next[i][j]はsのi番目(0-origin)以降(i含む)でjが初めて出てくる場所
  rep(i, x) {
    rep(j, 26) {
      if (hu[j].empty())
        continue;
      ll c = *lower_bound(all(hu[j]), i);
      next[i][j] = c % x;
    }
  }

  ll ans = 0;
  vll So(100005, -1); // greedyにtのi番目に対応してsの文字の出現場所を列挙
  bool f = true;

  rep(i, y) {
    int j = t[i] - 'a';
    So[i + 1] = next[(So[i] + 1) % x][j];
    if (So[i + 1] == -1) {
      f = false;
      break;
    }
    ll tmp = (So[i + 1] > So[i] ? So[i + 1] - So[i] : So[i + 1] - So[i] + x);
    ans = ans + tmp;
  }

  cout << (f ? ans : -1) << endl;
}


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

struct Fast {
  Fast() {
    std::cin.tie(0);
    ios::sync_with_stdio(false);
  }
} fast;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vll = vector<long long>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
#define MOD 1000000007

int main() {
  string s, t, u;
  cin >> s >> t;
  u = s + s;
  ll x = s.size(), y = t.size();
  vll hu[26];
  rep(i, 2 * x) {
    int j = u[i] - 'a';
    hu[j].pb(i);
  }

  vector<vll> next(x, vll(26, -1));
  // next[i][j]はsのi番目(0-origin)以降(i含む)でjが初めて出てくる場所
  rep(i, x) {
    rep(j, 26) {
      if (hu[j].empty())
        continue;
      ll c = *lower_bound(all(hu[j]), i);
      next[i][j] = c % x;
    }
  }

  ll ans = 0;
  vll So(100005, -1); // greedyにtのi番目に対応してsの文字の出現場所を列挙
  bool f = true;

  rep(i, y) {
    int j = t[i] - 'a';
    So[i + 1] = next[(So[i] + 1) % x][j];
    if (So[i + 1] == -1) {
      f = false;
      break;
    }
    ll tmp = (So[i + 1] > So[i] ? So[i + 1] - So[i] : So[i + 1] - So[i] + x);
    ans = ans + tmp;
  }

  cout << (f ? ans : -1) << endl;
}
Problem p02937 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
#define all(x) (x).begin(), (x).end()
#define fsp(x) fixed << setprecision(x)
const ll inf = LLONG_MAX;
const long double pi = acos(-1);
void Yes() { cout << "Yes" << endl; }
void No() { cout << "No" << endl; }
void YES() { cout << "YES" << endl; }
void NO() { cout << "NO" << endl; }

int main() {
  // ios::sync_with_stdio(false);
  // cin.tie(nullptr);
  // const ll p = 1e9 + 7;
  // const ll p = 998244353;

  string s, t;
  cin >> s >> t;
  ll n = s.size(), m = t.size();

  vll cnt(26, 0);
  for (ll i = 0; i < n; i++)
    cnt[s[i] - 'a']++;

  for (ll i = 0; i < m; i++) {
    if (cnt[t[i] - 'a'] == 0) {
      cout << -1 << endl;
      return 0;
    }
  }

  vector<vll> v(26, vll(0));
  for (ll i = 0; i < n; i++)
    v[s[i] - 'a'].push_back(i);

  ll ans = 1, prev_idx = -1;
  for (ll i = 0; i < m; i++) {
    for (ll j = 0; j < v[t[i] - 'a'].size(); j++) {
      if (v[t[i] - 'a'][j] > prev_idx) {
        prev_idx = v[t[i] - 'a'][j];
        break;
      }
      if (j == v[t[i] - 'a'].size() - 1) {
        prev_idx = v[t[i] - 'a'][0];
        ans++;
      }
    }
  }
  cout << n * (ans - 1) + prev_idx + 1 << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
#define all(x) (x).begin(), (x).end()
#define fsp(x) fixed << setprecision(x)
const ll inf = LLONG_MAX;
const long double pi = acos(-1);
void Yes() { cout << "Yes" << endl; }
void No() { cout << "No" << endl; }
void YES() { cout << "YES" << endl; }
void NO() { cout << "NO" << endl; }

int main() {
  // ios::sync_with_stdio(false);
  // cin.tie(nullptr);
  // const ll p = 1e9 + 7;
  // const ll p = 998244353;

  string s, t;
  cin >> s >> t;
  ll n = s.size(), m = t.size();

  vll cnt(26, 0);
  for (ll i = 0; i < n; i++)
    cnt[s[i] - 'a']++;

  for (ll i = 0; i < m; i++) {
    if (cnt[t[i] - 'a'] == 0) {
      cout << -1 << endl;
      return 0;
    }
  }

  vector<vll> v(26, vll(0));
  for (ll i = 0; i < n; i++)
    v[s[i] - 'a'].push_back(i);

  ll ans = 1, prev_idx = -1;
  for (ll i = 0; i < m; i++) {
    auto itr = upper_bound(all(v[t[i] - 'a']), prev_idx);
    if (itr == v[t[i] - 'a'].end()) {
      prev_idx = v[t[i] - 'a'][0];
      ans++;
    } else
      prev_idx = *itr;
  }
  cout << n * (ans - 1) + prev_idx + 1 << endl;
}
Problem p02937 (C++)

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

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;

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

  string ss = s + s;
  map<char, vi> ss_m;
  for (int i = 0; i < ss.length(); i++) {
    ss_m[ss[i]].push_back(i + 1);
  }

  vi t_i(t.length() + 1, 0);
  for (int i = 0; i < t.length(); i++) {
    auto &v = ss_m[t[i]];
    if (v.size() == 0) {
      cout << -1 << endl;
      return 0;
    }
    for (int j = 0; j < v.size(); j++) {
      if (t_i[i] < v[j]) {
        t_i[i + 1] = (v[j] > s.length()) ? v[j] - s.length() : v[j];
        break;
      }
    }
  }
  // for (int i = 0; i < t_i.size(); i++) {
  //   printf("%d ", t_i[i]);
  // }
  // printf("\n");

  ll ans = 0;
  for (int i = 1; i < t_i.size(); i++) {
    if (i == t_i.size() - 1) {
      // printf("last\n");
      ans += t_i[i];
    } else {
      if (t_i[i] >= t_i[i + 1]) {
        ans += s.length();
        // printf("ans=%d\n", ans);
      }
    }
  }
  cout << ans << endl;
  return 0;
}


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

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;

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

  string ss = s + s;
  map<char, vi> ss_m;
  for (int i = 0; i < ss.length(); i++) {
    ss_m[ss[i]].push_back(i + 1);
  }

  vi t_i(t.length() + 1, 0);
  for (int i = 0; i < t.length(); i++) {
    auto &v = ss_m[t[i]];
    if (v.size() == 0) {
      cout << -1 << endl;
      return 0;
    }
    // for (int j = 0; j < v.size(); j++) {
    //   if (t_i[i] < v[j]) {
    //     t_i[i + 1] = (v[j] > s.length()) ? v[j] - s.length() : v[j];
    //     break;
    //   }
    // }
    auto it = lower_bound(v.begin(), v.end(), t_i[i] + 1);
    t_i[i + 1] = (*it > s.length()) ? *it - s.length() : *it;
  }
  // for (int i = 0; i < t_i.size(); i++) {
  //   printf("%d ", t_i[i]);
  // }
  // printf("\n");

  ll ans = 0;
  for (int i = 1; i < t_i.size(); i++) {
    if (i == t_i.size() - 1) {
      // printf("last\n");
      ans += t_i[i];
    } else {
      if (t_i[i] >= t_i[i + 1]) {
        ans += s.length();
        // printf("ans=%d\n", ans);
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02937 (C++)

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

int main() {
  string s, t;
  cin >> s >> t;
  string ds = s + s;
  vector<vector<int>> abc(26);
  for (int i = 0; i < ds.length(); i++) {
    abc[ds[i] - 'a'].push_back(i);
  }
  for (int i = 0; i < 26; i++) {
    sort(abc[i].begin(), abc[i].end());
  }
  // now:今のsでの添字
  int now = -1;
  long long int ans = 0;
  for (int i = 0; i < t.length(); i++) {
    vector<int> temp = abc[t[i] - 'a'];
    auto itr = lower_bound(temp.begin(), temp.end(), now + 1);
    if (itr != temp.end()) {
      ans += *itr - now;
      now = *itr % s.length();
    } else {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << ans << endl;
}

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

int main() {
  string s, t;
  cin >> s >> t;
  string ds = s + s;
  vector<vector<int>> abc(26);
  for (int i = 0; i < ds.length(); i++) {
    abc[ds[i] - 'a'].push_back(i);
  }
  for (int i = 0; i < 26; i++) {
    sort(abc[i].begin(), abc[i].end());
  }
  // now:今のsでの添字
  int now = -1;
  long long int ans = 0;
  for (int i = 0; i < t.length(); i++) {
    // vector<int> temp=abc[t[i]-'a'];
    auto itr =
        lower_bound(abc[t[i] - 'a'].begin(), abc[t[i] - 'a'].end(), now + 1);
    if (itr != abc[t[i] - 'a'].end()) {
      ans += *itr - now;
      now = *itr % s.length();
    } else {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << ans << endl;
}
Problem p02937 (C++)

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

using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i <= n; i++)
typedef long long ll;

int main() {
  string s, t;
  cin >> s >> t;
  vector<set<int>> v(26);
  int n = int(s.size());
  rep(i, n) { v[s[i] - 'a'].insert(i + 1); }
  ll ans = 0;
  int id = 0;
  int m = int(t.size());
  rep(i, m) {
    int c = t[i] - 'a';
    if (v[c].empty()) {
      cout << -1 << endl;
      return 0;
    }
    auto itr = lower_bound(v[c].begin(), v[c].end(), id + 1);
    if (itr == v[c].end()) {
      itr = v[c].begin();
      ans += ll(n - id + *itr);
    } else {
      ans += ll(*itr - id);
    }
    id = *itr;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i <= n; i++)
typedef long long ll;

int main() {
  string s, t;
  cin >> s >> t;
  vector<set<int>> v(26);
  int n = int(s.size());
  rep(i, n) { v[s[i] - 'a'].insert(i + 1); }
  ll ans = 0;
  int id = 0;
  int m = int(t.size());
  rep(i, m) {
    int c = t[i] - 'a';
    if (v[c].empty()) {
      cout << -1 << endl;
      return 0;
    }
    auto itr = v[c].lower_bound(id + 1);
    if (itr == v[c].end()) {
      itr = v[c].begin();
      ans += ll(n - id + *itr);
    } else {
      ans += ll(*itr - id);
    }
    id = *itr;
  }
  cout << ans << endl;
  return 0;
}
Problem p02937 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ALL(x) x.begin(), x.end()
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1000000000
#define mod 1000000007
typedef long long ll;
const ll LINF = 1001002003004005006ll;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  string s, t;
  cin >> s >> t;
  vector<vector<ll>> v(26);
  rep(i, s.size()) { v[s[i] - 'a'].push_back(i); }
  ll ind = 0, numOf_s = 0;
  for (auto c : t) {
    auto ite = lower_bound(ALL(v[c - 'a']), ind);
    if (ite == v[c - 'a'].end()) {
      numOf_s++;
      ind = v[c - 'a'][0] + 1;
    } else {
      ind = *ite + 1;
    }
  }
  cout << numOf_s * s.size() + ind << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ALL(x) x.begin(), x.end()
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1000000000
#define mod 1000000007
typedef long long ll;
const ll LINF = 1001002003004005006ll;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  string s, t;
  cin >> s >> t;
  vector<vector<ll>> v(26);
  rep(i, s.size()) { v[s[i] - 'a'].push_back(i); }
  ll ind = 0, numOf_s = 0;
  for (auto c : t) {
    if (v[c - 'a'].size() == 0) {
      cout << -1 << endl;
      return 0;
    }
    auto ite = lower_bound(ALL(v[c - 'a']), ind);
    if (ite == v[c - 'a'].end()) {
      numOf_s++;
      ind = v[c - 'a'][0] + 1;
    } else {
      ind = *ite + 1;
    }
  }
  cout << numOf_s * s.size() + ind << endl;
  return 0;
}