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

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
int main() {
  int n, q;
  cin >> n >> q;

  vector<int> parent(n); // [0]->error
  for (int i = 1; i < n; i++) {
    int a, b;
    cin >> a >> b;
    parent[b - 1] = a - 1;
  }

  vector<ll> cost(n, 0);
  for (int i = 0; i < q; i++) {
    int p, x;
    cin >> p >> x;
    cost[p - 1] += x;
  }

  for (int i = 0; i < n; i++) {
    ll sum = cost[0];
    for (int node = i; node != 0; node = parent[node]) {
      sum += cost[node];
    }
    cout << sum << ' ';
  }

  cout << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
int main() {
  int n, q;
  cin >> n >> q;

  vector<int> parent(n); // [0]->error
  for (int i = 1; i < n; i++) {
    int a, b;
    cin >> a >> b;
    parent[b - 1] = a - 1;
  }

  vector<ll> cost(n, 0);
  for (int i = 0; i < q; i++) {
    int p, x;
    cin >> p >> x;
    cost[p - 1] += x;
  }

  cout << cost[0] << ' ';
  for (int i = 1; i < n; i++) {
    cost[i] += cost[parent[i]];
    cout << cost[i] << ' ';
  }

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

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

// int point[5001][5001] = {0};

vector<bool> used(100, false);

void bfs(vector<vector<int>> &data, vector<long long> &ans, int t) {
  used[t] = true;
  // ans[t].first = time;
  for (int i = 0; i < data[t].size(); i++) {
    if (!used[data[t][i]]) {
      ans[data[t][i]] += ans[t];
      bfs(data, ans, data[t][i]);
    }
  }

  // ans[t].second = time;
}

int main() {
  int N, Q;
  cin >> N >> Q;
  string t;
  getline(cin, t);

  vector<vector<int>> data(N);
  vector<long long> ans(N, 0);
  for (int i = 0; i < N - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    data[a].push_back(b);
    data[b].push_back(a);
  }
  for (int i = 0; i < Q; i++) {
    int a;
    long long b;
    cin >> a >> b;
    a--;
    ans[a] += b;
  }
  // used[0] = true;

  bfs(data, ans, 0);
  for (int i = 0; i < N; i++) {
    cout << ans[i] << endl;
  }
  // cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using Point = pair<int, int>;

// int point[5001][5001] = {0};

vector<bool> used(200000, false);

void bfs(vector<vector<int>> &data, vector<long long> &ans, int t) {
  used[t] = true;
  // ans[t].first = time;
  for (int i = 0; i < data[t].size(); i++) {
    if (!used[data[t][i]]) {
      ans[data[t][i]] += ans[t];
      bfs(data, ans, data[t][i]);
    }
  }

  // ans[t].second = time;
}

int main() {
  int N, Q;
  cin >> N >> Q;
  string t;
  getline(cin, t);

  vector<vector<int>> data(N);
  vector<long long> ans(N, 0);
  for (int i = 0; i < N - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    data[a].push_back(b);
    data[b].push_back(a);
  }
  for (int i = 0; i < Q; i++) {
    int a;
    long long b;
    cin >> a >> b;
    a--;
    ans[a] += b;
  }
  // used[0] = true;

  bfs(data, ans, 0);
  for (int i = 0; i < N; i++) {
    cout << ans[i] << endl;
  }
  // cout << ans << endl;

  return 0;
}
Problem p02936 (C++)

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

int w, h;

void dfs(vector<vector<int>> g, int v, vector<bool> &seen,
         vector<long long> &add_x, vector<long long> &count, long long add) {
  seen[v] = true;
  add += add_x[v];
  count[v] += add;
  for (auto i : g[v]) {
    if (seen[i]) {
      continue;
    }
    dfs(g, i, seen, add_x, count, add);
  }
  add -= add_x[v];
}

int main() {
  int N, Q;
  cin >> N >> Q;
  vector<vector<int>> g(N + 1);
  vector<bool> seen(N + 1, false);
  for (int i = 0; i < N - 1; i++) {
    int a, b;
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  vector<long long> add_x(N + 1, 0);

  for (int i = 0; i < Q; i++) {
    int p, x;
    cin >> p >> x;
    add_x[p] += x;
  }
  vector<long long> count(N + 1, 0);
  long long add = 0;
  dfs(g, 1, seen, add_x, count, add);
  for (int i = 1; i <= N; i++) {
    cout << count[i] << " ";
  }
}


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

int w, h;

void dfs(vector<vector<int>> &g, int v, vector<bool> &seen,
         vector<long long> &add_x, vector<long long> &count, long long add) {
  seen[v] = true;
  add += add_x[v];
  count[v] += add;
  for (auto i : g[v]) {
    if (seen[i]) {
      continue;
    }
    dfs(g, i, seen, add_x, count, add);
  }
  add -= add_x[v];
}

int main() {
  int N, Q;
  cin >> N >> Q;
  vector<vector<int>> g(N + 1);
  vector<bool> seen(N + 1, false);
  for (int i = 0; i < N - 1; i++) {
    int a, b;
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  vector<long long> add_x(N + 1, 0);

  for (int i = 0; i < Q; i++) {
    int p, x;
    cin >> p >> x;
    add_x[p] += x;
  }
  vector<long long> count(N + 1, 0);
  long long add = 0;
  dfs(g, 1, seen, add_x, count, add);
  for (int i = 1; i <= N; i++) {
    cout << count[i] << " ";
  }
}
Problem p02937 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <locale>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
#define FORN(i, m, n) for (int i = m; i < (n); i++)
#define PRINTVEC(v)                                                            \
  FORN(i, 0, v.size()) cout << v[i] << " ";                                    \
  cout << endl
#define PRINTMAT(m)                                                            \
  FORN(j, 0, m.size()) { PRINTVEC(m[j]); }
#define p_b(x) push_back(x)
#define m_p(a, b) make_pair(a, b)
typedef long long ll;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  string s, t;
  cin >> s >> t;
  vector<vector<int>> v(27);
  FORN(i, 0, s.size()) v[s[i] - 'a'].p_b(i);
  ll ans = 0, cur = -1;
  FORN(i, 0, t.size()) {
    int x = t[i] - 'a', pos;
    if (v[x].empty()) {
      cout << -1 << endl;
      return 0;
    }
    pos = upper_bound(v[x].begin(), v[x].end(), cur) - v[x].begin();
    if (pos == v[x].size()) {
      pos = v[x][0];
      ans++;
    }
    cur = v[x][pos];
  }
  ans = ans * s.size() + cur + 1;
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <locale>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
#define FORN(i, m, n) for (int i = m; i < (n); i++)
#define PRINTVEC(v)                                                            \
  FORN(i, 0, v.size()) cout << v[i] << " ";                                    \
  cout << endl
#define PRINTMAT(m)                                                            \
  FORN(j, 0, m.size()) { PRINTVEC(m[j]); }
#define p_b(x) push_back(x)
#define m_p(a, b) make_pair(a, b)
typedef long long ll;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  string s, t;
  cin >> s >> t;
  vector<vector<int>> v(27);
  FORN(i, 0, s.size()) v[s[i] - 'a'].p_b(i);
  ll ans = 0, cur = -1;
  FORN(i, 0, t.size()) {
    int x = t[i] - 'a', pos;
    if (v[x].empty()) {
      cout << -1 << endl;
      return 0;
    }
    pos = upper_bound(v[x].begin(), v[x].end(), cur) - v[x].begin();
    if (pos == v[x].size()) {
      pos = 0;
      ans++;
    }
    cur = v[x][pos];
  }
  ans = ans * s.size() + cur + 1;
  cout << ans << endl;
}
Problem p02937 (C++)

Buggy submission (status: Runtime Error):
/*
ID:dyx_6661
TASK:
LANG:
*/
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long int ll;
typedef double db;
typedef string str;
typedef pair<int, int> pa;
typedef vector<int> vec;

const int n_size = 1e5 + 5;

ll f[n_size][30];
char a[2 * n_size], b[n_size];

ll ans = 0, cnt = 1;

int main() {
  //  freopen(".in","r",stdin);
  //  freopen(".out","w",stdout);
  scanf("%s", a + 1);
  scanf("%s", b + 1);
  ll n = strlen(a + 1), m = strlen(b + 1);
  for (ll i = 1; i <= n; i++)
    a[i + n] = a[i];
  for (ll i = 2 * n; i >= 1; i--) {
    for (int j = 1; j <= 26; j++) {
      if (a[i] == j + 'a' - 1) {
        f[i][j] = i;
      } else {
        f[i][j] = f[i + 1][j];
      }
    }
  }

  for (ll i = 1; i <= m; i++) {
    if (!f[1][b[i] - 'a' + 1]) {
      printf("-1");
      return 0;
    }
    ans += f[cnt][b[i] - 'a' + 1] - cnt + 1;
    if (f[cnt][b[i] - 'a' + 1] > n)
      cnt = f[cnt][b[i] - 'a' + 1] - n;
    cnt = f[cnt][b[i] - 'a' + 1] + 1;
  }
  printf("%lld", ans);
  return 0;
}


Fixed submission:
/*
ID:dyx_6661
TASK:
LANG:
*/
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long int ll;
typedef double db;
typedef string str;
typedef pair<int, int> pa;
typedef vector<int> vec;

const int n_size = 1e5 + 5;

ll f[2 * n_size][30];
char a[2 * n_size], b[n_size];

ll ans = 0, cnt = 1;

int main() {
  //  freopen(".in","r",stdin);
  //  freopen(".out","w",stdout);
  scanf("%s", a + 1);
  scanf("%s", b + 1);
  ll n = strlen(a + 1), m = strlen(b + 1);
  for (ll i = 1; i <= n; i++)
    a[i + n] = a[i];
  for (ll i = 2 * n; i >= 1; i--) {
    for (int j = 1; j <= 26; j++) {
      if (a[i] == j + 'a' - 1) {
        f[i][j] = i;
      } else {
        f[i][j] = f[i + 1][j];
      }
    }
  }

  for (ll i = 1; i <= m; i++) {
    if (!f[1][b[i] - 'a' + 1]) {
      printf("-1");
      return 0;
    }
    ans += f[cnt][b[i] - 'a' + 1] - cnt + 1;
    if (f[cnt][b[i] - 'a' + 1] > n)
      cnt = f[cnt][b[i] - 'a' + 1] - n;
    cnt = f[cnt][b[i] - 'a' + 1] + 1;
  }
  printf("%lld", ans);
  return 0;
}
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 ll long long
#define P pair<int, int>
#define fast_io                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
const int MOD = 1000000007;
const int INF = 2002002002;
const ll LLINF = 9009009009009009009;
using namespace std;

int main() {
  fast_io const int N_ALPHA = 26;
  vector<vector<int>> alp(N_ALPHA);
  string s, t;
  cin >> s >> t;

  rep(i, s.size()) alp[s[i] - 'a'].push_back(i + 1);

  ll n = s.size(), ans = 0;
  ;
  int f_ind, c_ind = 0;
  for (char c : t) {
    f_ind = c_ind;
    if (alp[c - 'a'].empty()) {
      cout << -1 << endl;
      return 0;
    }
    c_ind = alp[c - 'a'][0];
    for (int a : alp[c - 'a'])
      if (a > f_ind) {
        c_ind = a;
        break;
      }
    if (f_ind < c_ind)
      ans += (c_ind - f_ind);
    else
      ans += (n - f_ind) + c_ind;
  }

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

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define ll long long
#define P pair<int, int>
#define fast_io                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
const int MOD = 1000000007;
const int INF = 2002002002;
const ll LLINF = 9009009009009009009;
using namespace std;

int main() {
  fast_io const int N_ALPHA = 26;
  vector<vector<int>> alp(N_ALPHA);
  string s, t;
  cin >> s >> t;

  rep(i, s.size()) alp[s[i] - 'a'].push_back(i + 1);

  ll n = s.size(), ans = 0;
  ;
  int f_ind, c_ind = 0;
  for (char c : t) {
    f_ind = c_ind;
    if (alp[c - 'a'].empty()) {
      cout << -1 << endl;
      return 0;
    }
    if (alp[c - 'a'].end() -
            upper_bound(alp[c - 'a'].begin(), alp[c - 'a'].end(), f_ind) ==
        0)
      c_ind = alp[c - 'a'][0];
    else
      c_ind = *upper_bound(alp[c - 'a'].begin(), alp[c - 'a'].end(), f_ind);
    if (f_ind < c_ind)
      ans += (c_ind - f_ind);
    else
      ans += (n - f_ind) + c_ind;
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
const int N = 1e5 + 7;
char s[N], t[N];
int lens, lent;

using ll = long long;
ll ans = 0, last = 0;
std::vector<int> ref[30];

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

  std::cin >> (s + 1) >> (t + 1);
  lens = strlen(s + 1), lent = strlen(t + 1);

  for (int i = 1; i <= lens; i++)
    ref[s[i]].push_back(i);

  ans = lens;

  for (int i = 1; i <= lent; i++) {
    if (ref[t[i]].empty()) {
      std::cout << -1;
      exit(0);
    }

    auto pos = std::upper_bound(ref[t[i]].begin(), ref[t[i]].end(), last);
    if (pos == ref[t[i]].end()) {
      ans += lens;
      i--;
      last = 0;
    } else
      last = *pos;
  }
  std::cout << ans - (lens - last);
  exit(0);
}


Fixed submission:
#include <bits/stdc++.h>
const int N = 1e5 + 7;
char s[N], t[N];
int lens, lent;

using ll = long long;
ll ans = 0, last = 0;
std::vector<int> ref[200];

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

  std::cin >> (s + 1) >> (t + 1);
  lens = strlen(s + 1), lent = strlen(t + 1);

  for (int i = 1; i <= lens; i++)
    ref[s[i]].push_back(i);

  ans = lens;

  for (int i = 1; i <= lent; i++) {
    if (ref[t[i]].empty()) {
      std::cout << -1;
      exit(0);
    }

    auto pos = std::upper_bound(ref[t[i]].begin(), ref[t[i]].end(), last);
    if (pos == ref[t[i]].end()) {
      ans += lens;
      i--;
      last = 0;
    } else
      last = *pos;
  }
  std::cout << ans - (lens - last);
  exit(0);
}
Problem p02937 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 6;
vector<int> e[30];

int main() {
  string s, t;
  cin >> s >> t;
  int len = s.length();
  int len2 = t.length();
  for (int i = 0; i < len; i++) {
    e[s[i] - 'a'].push_back(i);
  }
  ll now = 0;
  ll cnt = 0;
  int flag = 1;
  for (int i = 0; i < len2; i++) {
    int u = t[i] - 'a';
    if (e[u].size() == 0) {
      flag = 0;
      break;
    }
    if (flag == 0)
      break;
    int pos = lower_bound(e[u].begin(), e[u].end(), now) - e[u].begin();
    if (pos >= e[u].size()) {
      now = 0;
      cnt++;
      pos = lower_bound(e[u].begin(), e[u].end(), now) - e[u].begin();
    }
    now = e[u][pos] + 1;
  }
  ll ans = 1ll * cnt * len + now;
  if (ans <= 0)
    while (1)
      ;
  if (flag)
    cout << ans << endl;
  else
    cout << -1 << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 6;
vector<int> e[30];

int main() {
  string s, t;
  cin >> s >> t;
  int len = s.length();
  int len2 = t.length();
  for (int i = 0; i < len; i++) {
    e[s[i] - 'a'].push_back(i);
  }
  ll now = 0;
  ll cnt = 0;
  int flag = 1;
  for (int i = 0; i < len2; i++) {
    int u = t[i] - 'a';
    if (e[u].size() == 0) {
      flag = 0;
      break;
    }
    if (flag == 0)
      break;
    int pos = lower_bound(e[u].begin(), e[u].end(), now) - e[u].begin();
    if (pos >= e[u].size()) {
      now = 0;
      cnt++;
      pos = lower_bound(e[u].begin(), e[u].end(), now) - e[u].begin();
    }
    now = e[u][pos] + 1;
  }
  ll ans = 1ll * cnt * len + now;
  if (ans < 0)
    while (1)
      ;
  if (flag)
    cout << ans << endl;
  else
    cout << -1 << endl;
}
Problem p02937 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define INF 100000000000
#define MOD 1000000007
#define ll long long int
// #define int long long int
using namespace std;

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

  // able or unable
  vector<int> freq1(40, 0); // s material
  vector<int> freq2(40, 0); // t object

  // cout<<99<<endl;

  for (int i = 0; i < s.size(); i++) {
    freq1[s[i] - 'a']++;
  }

  for (int i = 0; i < t.size(); i++) {
    freq2[t[i] - 'a']++;
  }

  // cout<<888<<endl;

  for (int i = 0; i < 40; i++) {
    if (freq1[i] == 0 and freq2[i] > 0) {
      cout << -1 << endl;
      return 0;
    }
    if (freq1[i] == 0 and freq2[i] == 0)
      continue;
  }

  ll ans = 0;

  vector<vector<int>> moji(40);

  for (int i = 0; i < s.size(); i++) {
    moji[s[i] - 'a'].push_back(i);
  }

  for (int i = 0; i < s.size(); i++) {
    moji[s[i] - 'a'].push_back(i + s.size());
  }

  int now = moji[t[0] - 'a'].at(0);
  int terget = 1;
  ans += now;

  while (true) {
    auto next = upper_bound(moji[t[terget] - 'a'].begin(),
                            moji[t[terget] - 'a'].end(), now);
    int dif = *next - now;
    ans += dif;
    now = *next;
    now = now % s.size();
    terget++;
    if (terget == t.size())
      break;
  }

  cout << ans + 1 << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define INF 100000000000
#define MOD 1000000007
#define ll long long int
// #define int long long int
using namespace std;

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

  // able or unable
  vector<int> freq1(40, 0); // s material
  vector<int> freq2(40, 0); // t object

  // cout<<99<<endl;

  for (int i = 0; i < s.size(); i++) {
    freq1[s[i] - 'a']++;
  }

  for (int i = 0; i < t.size(); i++) {
    freq2[t[i] - 'a']++;
  }

  // cout<<888<<endl;

  for (int i = 0; i < 40; i++) {
    if (freq1[i] == 0 and freq2[i] > 0) {
      cout << -1 << endl;
      return 0;
    }
    if (freq1[i] == 0 and freq2[i] == 0)
      continue;
  }

  ll ans = 0;

  vector<vector<int>> moji(40);

  for (int i = 0; i < s.size(); i++) {
    moji[s[i] - 'a'].push_back(i);
  }

  for (int i = 0; i < s.size(); i++) {
    moji[s[i] - 'a'].push_back(i + s.size());
  }

  int now = moji[t[0] - 'a'].at(0);
  int terget = 1;
  ans += now;

  while (true) {
    if (terget == t.size())
      break;

    auto next = upper_bound(moji[t[terget] - 'a'].begin(),
                            moji[t[terget] - 'a'].end(), now);
    int dif = *next - now;
    ans += dif;
    now = *next;
    now = now % s.size();
    terget++;
    if (terget == t.size())
      break;
  }

  cout << ans + 1 << endl;

  return 0;
}
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 < (int)(n); i++)

int main() {
  string s, t;
  cin >> s >> t;
  string ss = s;
  int tlong = t.size();
  int64_t ans = 0;
  int64_t x = 0;
  for (int i = 0; i < tlong; i++) {
    x = ss.find_first_of(t.at(i));
    ss.erase(ss.begin(), ss.begin() + x + 1);
    ss += s;
    // cout <<x+1<<endl;
    if (x >= 0)
      ans += x + 1;
    else {
      ans = -1;
      break;
    }
  }
  cout << ans;
}

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

int main() {
  string s, t;
  cin >> s >> t;
  string ss = s;
  int tlong = t.size();
  int64_t ans = 0;
  int64_t x = 0;
  for (int i = 0; i < tlong; i++) {
    x = ss.find_first_of(t.at(i));
    ss.erase(ss.begin(), ss.begin() + x + 1);
    if (ss.size() < s.size())
      ss += s;
    // cout <<x+1<<endl;
    if (x >= 0)
      ans += x + 1;
    else {
      ans = -1;
      break;
    }
  }
  cout << ans;
}
Problem p02937 (C++)

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

int main() {
  string s, t;
  cin >> s >> t;
  int lenS = (int)s.size(), lenT = (int)t.size();

  vector<vector<int>> s_index(26);
  for (int i = 0; i < lenS; i++) {
    int c = s.at(i) - 'a';
    s_index.at(c).push_back(i);
  }
  for (int i = 0; i < lenS; i++) {
    int c = s.at(i) - 'a';
    s_index.at(c).push_back(i + lenS);
  }

  long long ans = 0;
  int p = 0;
  for (int i = 0; i < lenT; i++) {
    int c = t.at(i) - 'a';
    if (s_index.at(c).size() == 0) {
      cout << -1 << endl;
    }
    p = *lower_bound(s_index.at(c).begin(), s_index.at(c).end(), p) + 1;
    if (p >= lenS) {
      p -= lenS;
      ans += lenS;
    }
  }
  ans += p;
  cout << ans << endl;
}

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

int main() {
  string s, t;
  cin >> s >> t;
  int lenS = (int)s.size(), lenT = (int)t.size();

  vector<vector<int>> s_index(26);
  for (int i = 0; i < lenS; i++) {
    int c = s.at(i) - 'a';
    s_index.at(c).push_back(i);
  }
  for (int i = 0; i < lenS; i++) {
    int c = s.at(i) - 'a';
    s_index.at(c).push_back(i + lenS);
  }

  long long ans = 0;
  int p = 0;
  for (int i = 0; i < lenT; i++) {
    int c = t.at(i) - 'a';
    if (s_index.at(c).size() == 0) {
      cout << -1 << endl;
      return 0;
    }
    p = *lower_bound(s_index.at(c).begin(), s_index.at(c).end(), p) + 1;
    if (p >= lenS) {
      p -= lenS;
      ans += lenS;
    }
  }
  ans += p;
  cout << ans << endl;
}
Problem p02937 (C++)

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

using namespace std;

typedef long long int ll;
typedef pair<int, int> P;
#define INF 10000000000000
#define rep(i, n) for (ll i = 0; i < (int)(n); i++)
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;
  ll pos = 0;
  ll pre = -1;
  ll ite = 0;
  bool flag = true;
  vector<vector<int>> alpha(26);

  cin >> s >> t;

  rep(i, s.size()) alpha[s[i] - 'a'].push_back(i + 1);

  rep(i, t.size()) {
    if (alpha[t[i] - 'a'].empty()) {
      flag = false;
      break;
    }
    rep(j, alpha[t[i] - 'a'].size()) {
      if (pre < alpha[t[i] - 'a'][j]) {
        pos = ite * s.size() + alpha[t[i] - 'a'][j];
        pre = alpha[t[i] - 'a'][j];
        break;
      }

      if (j == alpha[t[i] - 'a'].size() - 1) {
        ite++;
        pos = ite * s.size() + alpha[t[i] - 'a'][0];
        pre = alpha[t[i] - 'a'][0];
        break;
      }
    }
  }

  if (!flag)
    cout << "-1" << endl;
  else if (pos > s.size() * pow(10, 100))
    cout << "-1" << endl;
  else
    cout << pos << endl;

  return 0;
}

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

using namespace std;

typedef long long int ll;
typedef pair<int, int> P;
#define INF 10000000000000
#define rep(i, n) for (ll i = 0; i < (int)(n); i++)
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;
  ll pos = 0;
  ll pre = -1;
  ll ite = 0;
  bool flag = true;
  vector<vector<int>> alpha(26);

  cin >> s >> t;

  rep(i, s.size()) alpha[s[i] - 'a'].push_back(i + 1);

  rep(i, t.size()) {
    if (alpha[t[i] - 'a'].empty()) {
      flag = false;
      break;
    }
    auto it =
        upper_bound(alpha[t[i] - 'a'].begin(), alpha[t[i] - 'a'].end(), pre);
    if (it != alpha[t[i] - 'a'].end()) {
      pos = ite * s.size() + *it;
      pre = *it;
    } else {
      ite++;
      pos = ite * s.size() + alpha[t[i] - 'a'][0];
      pre = alpha[t[i] - 'a'][0];
    }
  }

  if (!flag)
    cout << "-1" << endl;
  else if (pos > s.size() * pow(10, 100))
    cout << "-1" << endl;
  else
    cout << pos << endl;

  return 0;
}
Problem p02937 (C++)

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

int main() {
  string s, t;
  cin >> s >> t;
  map<char, set<int>> mp;
  vector<vector<int>> v;
  rep(i, s.size()) { v[s[i] - 'a'].push_back(i); }
  int id = -1;
  ll ans = 0;
  rep(i, t.size()) {
    int x = t[i] - 'a';
    if (v[x].size() == 0) {
      cout << -1 << endl;
      return 0;
    }
    auto it = upper_bound(v[x].begin(), v[x].end(), id);
    if (it == v[x].end()) {
      ans += s.size();
      id = *(v[x].begin());
    } else {
      id = *it;
    }
  }
  ans += id + 1;
  cout << ans << endl;
  return 0;
}


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

int main() {
  string s, t;
  cin >> s >> t;
  map<char, set<int>> mp;
  vector<vector<int>> v(26);
  rep(i, s.size()) { v[s[i] - 'a'].push_back(i); }
  int id = -1;
  ll ans = 0;
  rep(i, t.size()) {
    int x = t[i] - 'a';
    if (v[x].size() == 0) {
      cout << -1 << endl;
      return 0;
    }
    auto it = upper_bound(v[x].begin(), v[x].end(), id);
    if (it == v[x].end()) {
      ans += s.size();
      id = *(v[x].begin());
    } else {
      id = *it;
    }
  }
  ans += id + 1;
  cout << ans << endl;
  return 0;
}
Problem p02937 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  string s, t;
  cin >> s >> t;
  string ss = s + s;
  vector<int> v[26];
  for (int i = 0; i < ss.size(); i++) {
    v[ss[i] - 'a'].emplace_back(i);
  }
  long long curr = v[t[0] - 'a'][0];
  if (t.size() == 1) {
    cout << curr + 1 << endl;
    return 0;
  }
  for (int i = 1; i < t.size(); i++) {
    int c = t[i] - 'a';
    int x = curr % s.size();
    if (v[c].empty()) {
      cout << -1 << endl;
      return 0;
    }
    int next = *upper_bound(v[c].begin(), v[c].end(), x);
    curr += next - x;
  }
  cout << curr + 1 << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  string s, t;
  cin >> s >> t;
  string ss = s + s;
  vector<int> v[26];
  for (int i = 0; i < ss.size(); i++) {
    v[ss[i] - 'a'].emplace_back(i);
  }
  if (v[t[0] - 'a'].empty()) {
    cout << -1 << endl;
    return 0;
  }
  long long curr = v[t[0] - 'a'][0];
  if (t.size() == 1) {
    cout << curr + 1 << endl;
    return 0;
  }
  for (int i = 1; i < t.size(); i++) {
    int c = t[i] - 'a';
    int x = curr % s.size();
    if (v[c].empty()) {
      cout << -1 << endl;
      return 0;
    }
    int next = *upper_bound(v[c].begin(), v[c].end(), x);
    curr += next - x;
  }
  cout << curr + 1 << endl;
  return 0;
}
Problem p02937 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define ALL(v) (v).begin(), (v).end()
#define REP(i, p, n) for (int i = p; i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list)                                                             \
  cout << "{ ";                                                                \
  for (auto nth : list) {                                                      \
    cout << nth << " ";                                                        \
  }                                                                            \
  cout << "}" << endl;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

using namespace std;

int main() {
  string s, t;
  cin >> s;
  cin >> t;
  int n = s.size(), m = t.size();
  vector<vector<int>> is(26);
  rep(i, n) is[s[i] - 'a'].push_back(i);
  rep(i, n) is[s[i] - 'a'].push_back(i + n);
  ll ans = 0;
  int p = 0;
  rep(i, m) {
    int c = t[i] - 'a';
    if (is[c].size() == 0) {
      puts("-1");
    }
    p = *lower_bound(is[c].begin(), is[c].end(), p) + 1;
    if (p >= n) {
      p -= n;
      ans += n;
    }
  }
  ans += p;
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define ALL(v) (v).begin(), (v).end()
#define REP(i, p, n) for (int i = p; i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list)                                                             \
  cout << "{ ";                                                                \
  for (auto nth : list) {                                                      \
    cout << nth << " ";                                                        \
  }                                                                            \
  cout << "}" << endl;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

using namespace std;

int main() {
  string s, t;
  cin >> s;
  cin >> t;
  int n = s.size(), m = t.size();
  vector<vector<int>> is(26);
  rep(i, n) is[s[i] - 'a'].push_back(i);
  rep(i, n) is[s[i] - 'a'].push_back(i + n);
  ll ans = 0;
  int p = 0;
  rep(i, m) {
    int c = t[i] - 'a';
    if (is[c].size() == 0) {
      puts("-1");
      return 0;
    }
    p = *lower_bound(is[c].begin(), is[c].end(), p) + 1;
    if (p >= n) {
      p -= n;
      ans += n;
    }
  }
  ans += p;
  cout << ans << endl;
  return 0;
}
Problem p02937 (C++)

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

int main() {
  string s, t;
  cin >> s >> t;
  int n = s.size(), m = t.size();
  map<char, vector<int>> mp;
  rep(i, n) mp[s[i]].push_back(i);
  rep(i, m) if (mp[t[i]].empty()) {
    cout << -1 << endl;
    return 0;
  }
  ll ans = 0;
  int now = -1;
  rep(i, m) {
    char c = t[i];
    for (int j = 0; j < mp[t[i]].size(); j++) {
      if (now < mp[t[i]][j]) {
        ans += mp[t[i]][j] - now;
        now = mp[t[i]][j];
        break;
      }
      if (j == mp[t[i]].size() - 1) {
        ans += n - now + mp[t[i]][0];
        now = mp[t[i]][0];
      }
    }
  }
  cout << ans << endl;
}


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

int main() {
  string s, t;
  cin >> s >> t;
  int n = s.size(), m = t.size();
  map<char, vector<int>> mp;
  rep(i, n) mp[s[i]].push_back(i);
  rep(i, m) if (mp[t[i]].empty()) {
    cout << -1 << endl;
    return 0;
  }
  ll ans = 0;
  int now = -1;
  rep(i, m) {
    char c = t[i];
    int idx = upper_bound(mp[c].begin(), mp[c].end(), now) - mp[c].begin();
    if (idx == mp[c].size()) {
      ans += n - now + mp[c][0];
      now = mp[c][0];
    } else {
      ans += mp[c][idx] - now;
      now = mp[c][idx];
    }
  }
  cout << ans << endl;
}
Problem p02937 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define inf 10e17
#define rep(i, n) for (long long i = 0; i < n; i++)
#define repr(i, n, m) for (long long i = m; i < n; i++)
#define mod 1000000007
#define sorti(x) sort(x.begin(), x.end())
#define sortd(x) sort(x.begin(), x.end(), std::greater<long long>())
#define debug(x) std::cerr << (x) << std::endl;
#define roll(x)                                                                \
  for (auto &&itr : x) {                                                       \
    cerr << (itr) << " ";                                                      \
  }

template <class T> inline void chmax(T &ans, T t) {
  if (t > ans)
    ans = t;
}
template <class T> inline void chmin(T &ans, T t) {
  if (t < ans)
    ans = t;
}

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

  map<char, vector<ll>> mp;
  rep(i, s.size()) { mp[s[i]].push_back(i); }

  for (auto i : t) {
    if (mp[i].size() == 0) {
      cout << "-1" << endl;
      return 0;
    }
  }

  ll sum = 0, pre = -1;
  for (auto t_ch : t) {
    auto ch_vec = mp[t_ch];
    auto it = upper_bound(ch_vec.begin(), ch_vec.end(), pre);
    // cout << *it << endl;
    if (it == ch_vec.end() || *it <= pre) {
      sum += 1;
      pre = ch_vec.front();
    } else {
      pre = *it;
    }
  }

  cout << sum * s.size() + pre + 1 << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define inf 10e17
#define rep(i, n) for (long long i = 0; i < n; i++)
#define repr(i, n, m) for (long long i = m; i < n; i++)
#define mod 1000000007
#define sorti(x) sort(x.begin(), x.end())
#define sortd(x) sort(x.begin(), x.end(), std::greater<long long>())
#define debug(x) std::cerr << (x) << std::endl;
#define roll(x)                                                                \
  for (auto &&itr : x) {                                                       \
    cerr << (itr) << " ";                                                      \
  }

template <class T> inline void chmax(T &ans, T t) {
  if (t > ans)
    ans = t;
}
template <class T> inline void chmin(T &ans, T t) {
  if (t < ans)
    ans = t;
}

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

  map<char, vector<ll>> mp;
  rep(i, s.size()) { mp[s[i]].push_back(i); }

  for (auto i : t) {
    if (mp[i].size() == 0) {
      cout << "-1" << endl;
      return 0;
    }
  }

  ll sum = 0, pre = -1;
  for (auto t_ch : t) {
    auto &&ch_vec = mp[t_ch];
    auto &&it = upper_bound(ch_vec.begin(), ch_vec.end(), pre);
    // cout << *it << endl;
    if (it == ch_vec.end() || *it <= pre) {
      sum += 1;
      pre = ch_vec.front();
    } else {
      pre = *it;
    }
  }

  cout << sum * s.size() + pre + 1 << endl;
}
Problem p02937 (C++)

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

using namespace std;
typedef long long ll;
const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;

int main() {

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

  vector<vector<ll>> ma(26);
  for (ll i = 0; i < (ll)s.length(); ++i) {
    ll u = s[i] - 'a';
    ma[u].push_back(i + 1);
  }

  ll ans = 0;
  ll cnt = 0;
  for (ll i = 0; i < (ll)t.length(); ++i) {
    ll u = t[i] - 'a';

    if (ma[u].empty()) {
      cout << -1 << endl;
      return 0;
    }

    bool match = false;
    for (auto v : ma[u]) {
      if (cnt < v) {
        ans += v - cnt;
        match = true;
        cnt = v;
        break;
      }
    }

    if (!match) {
      ans += s.length() - cnt;
      ans += ma[u][0];
      cnt = ma[u][0];
    }
  }

  cout << ans << endl;
}

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

using namespace std;
typedef long long ll;
const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;

int main() {

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

  vector<vector<ll>> ma(26);
  for (ll i = 0; i < (ll)s.length(); ++i) {
    ll u = s[i] - 'a';
    ma[u].push_back(i + 1);
  }

  ll ans = 0;
  ll cnt = 0;
  for (ll i = 0; i < (ll)t.length(); ++i) {
    ll u = t[i] - 'a';

    if (ma[u].empty()) {
      cout << -1 << endl;
      return 0;
    }

    bool match = false;
    auto it = upper_bound(ma[u].begin(), ma[u].end(), cnt);
    if (it != ma[u].end()) {
      ll id = distance(ma[u].begin(), it);
      ans += ma[u][id] - cnt;
      match = true;
      cnt = ma[u][id];
    }

    if (!match) {
      ans += s.length() - cnt;
      ans += ma[u][0];
      cnt = ma[u][0];
    }
  }

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

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

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

  long long n = s.size();
  long long m = t.size();

  vector<vector<long long>> index(30);

  for (long long i = 0; i < 30; i++)
    index[s[i] - 'a'].push_back(i + 1);

  long long ans = 0;
  long long last = 0;

  for (long long i = 0; i < m; i++) {
    long long nch = t[i] - 'a';
    if (index[nch].size() <= 0) {
      printf("-1\n");
      return 0;
    }

    auto itr = upper_bound(index[nch].begin(), index[nch].end(), last);

    if (itr == index[nch].end()) {
      ans += n;
      itr = index[nch].begin();
    }
    last = *itr;
  }
  ans += last;
  cout << ans << endl;
  return 0;
}

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

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

  long long n = s.size();
  long long m = t.size();

  vector<vector<long long>> index(30);

  for (long long i = 0; i < n; i++)
    index[s[i] - 'a'].push_back(i + 1);

  long long ans = 0;
  long long last = 0;

  for (long long i = 0; i < m; i++) {
    long long nch = t[i] - 'a';
    if (index[nch].size() <= 0) {
      printf("-1\n");
      return 0;
    }

    auto itr = upper_bound(index[nch].begin(), index[nch].end(), last);

    if (itr == index[nch].end()) {
      ans += n;
      itr = index[nch].begin();
    }
    last = *itr;
  }
  ans += last;
  cout << ans << endl;
  return 0;
}
Problem p02937 (C++)

Buggy submission (status: Runtime Error):

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

typedef long long ll;
typedef vector<ll> vll;
const ll INF = 1e18;
const ll N = 1e5;
const ll M = 1e9 + 7;

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

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

  vll p[26];
  for (ll i = 0; i < s.size(); ++i)
    p[s[i] - 'a'].push_back(i);

  ll a[t.size()];
  a[0] = p[t[0] - 'a'][0];
  for (ll i = 1; i < t.size(); ++i) {
    ll ch = t[i] - 'a';
    if (p[ch].empty()) {
      cout << -1 << '\n';
      return 0;
    }
    ll b = upper_bound(p[ch].begin(), p[ch].end(), a[i - 1]) - p[ch].begin();
    if (b == p[ch].size())
      b = 0;
    a[i] = p[ch][b];
  }

  ll c = 0;
  for (ll i = 0; i < t.size() - 1; ++i)
    if (a[i + 1] <= a[i])
      ++c;

  cout << s.size() * c + a[t.size() - 1] + 1 << '\n';
}


Fixed submission:

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

typedef long long ll;
typedef vector<ll> vll;
const ll INF = 1e18;
const ll N = 1e5;
const ll M = 1e9 + 7;

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

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

  vll p[26];
  for (ll i = 0; i < s.size(); ++i)
    p[s[i] - 'a'].push_back(i);

  ll a[t.size()];
  if (p[t[0] - 'a'].empty()) {
    cout << -1 << '\n';
    return 0;
  }
  a[0] = p[t[0] - 'a'][0];
  for (ll i = 1; i < t.size(); ++i) {
    ll ch = t[i] - 'a';
    if (p[ch].empty()) {
      cout << -1 << '\n';
      return 0;
    }
    ll b = upper_bound(p[ch].begin(), p[ch].end(), a[i - 1]) - p[ch].begin();
    if (b == p[ch].size())
      b = 0;
    a[i] = p[ch][b];
  }

  ll c = 0;
  for (ll i = 0; i < t.size() - 1; ++i)
    if (a[i + 1] <= a[i])
      ++c;

  cout << s.size() * c + a[t.size() - 1] + 1 << '\n';
}
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)

typedef long long ll;
typedef pair<ll, ll> P;

const int MAX = 1e5 + 10;
const int INF = 1e9;
const int MOD = 1e9 + 7;

string s, t;

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

  map<char, vector<int>> m;
  rep(i, s.size()) {
    char c = s[i];
    m[c].push_back(i);
  }

  ll ans = 0;
  int x = -1;
  rep(i, t.size()) {
    char c = t[i];

    vector<int> index = m[c];
    if (index.size() == 0) {
      cout << -1 << endl;
      return 0;
    }

    auto itr = upper_bound(index.begin(), index.end(), x);
    if (itr == index.end()) {
      ans += s.size();
      x = m[c][0];
    } else {
      x = *itr;
    }
  }

  ans += x + 1;
  cout << ans << endl;
}

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

typedef long long ll;
typedef pair<ll, ll> P;

const int MAX = 1e5 + 10;
const int INF = 1e9;
const int MOD = 1e9 + 7;

string s, t;

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

  map<char, vector<int>> m;
  rep(i, s.size()) {
    char c = s[i];
    m[c].push_back(i);
  }

  ll ans = 0;
  int x = -1;
  rep(i, t.size()) {
    char c = t[i];

    vector<int> &index = m[c];
    if (index.size() == 0) {
      cout << -1 << endl;
      return 0;
    }

    auto itr = upper_bound(index.begin(), index.end(), x);
    if (itr == index.end()) {
      ans += s.size();
      x = m[c][0];
    } else {
      x = *itr;
    }
  }

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

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <cstring>
typedef long long ll;
const int N = 100010;

int ls, lt, cnt[26], v[26][N];
ll ans;
char s[N], t[N];
int main() {
  scanf("%s%s", s + 1, t + 1);
  ls = strlen(s + 1);
  lt = strlen(t + 1);
  for (int i = 1; i <= ls; ++i) {
    int x = s[i] - 'a';
    v[x][++cnt[x]] = i;
  }
  for (int i = 1; i <= lt; ++i)
    if (!cnt[t[i] - 'a'])
      return puts("-1"), 0;
  int j = 0, k;
  for (int i = 1; i <= lt; ++i) {
    int x = t[i] - 'a';
    k = 1;
    while (v[x][k] <= j && k <= cnt[x])
      ++k;
    if (k > cnt[x])
      k = 1, ans += 1ll * ls;
    ans += 1ll * v[x][k] - j;
    j = v[x][k];
  }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstring>
typedef long long ll;
const int N = 100010;

int ls, lt, cnt[26], v[26][N];
ll ans;
char s[N], t[N];
int main() {
  scanf("%s%s", s + 1, t + 1);
  ls = strlen(s + 1);
  lt = strlen(t + 1);
  for (int i = 1; i <= ls; ++i) {
    int x = s[i] - 'a';
    v[x][++cnt[x]] = i;
  }
  for (int i = 1; i <= lt; ++i)
    if (!cnt[t[i] - 'a'])
      return puts("-1"), 0;
  int j = 0, k;
  for (int i = 1; i <= lt; ++i) {
    int x = t[i] - 'a';
    if (j >= v[x][cnt[x]]) {
      ans += ls - j + v[x][1];
      j = v[x][1];
      continue;
    }
    int l = 1, r = cnt[x];
    while (l < r) {
      int m = l + r >> 1;
      v[x][m] > j ? (r = m) : (l = m + 1);
    }
    ans += 1ll * v[x][r] - j;
    j = v[x][r];
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p02937 (C++)

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

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

  const uint s_len = s.size();
  const uint t_len = t.size();

  vector<int> index['z' + 1];
  int last_index['z' + 1]{0};

  for (uint i = 0; i < s_len; i++)
    index[s[i]].push_back(i);

  uint64_t n = 0;
  int current_index = -1;

  for (uint i = 0; i < t_len;) {
    if (index[t[i]].size() == 0) {
      cout << -1 << endl;
      return 0;
    }
    if (current_index > index[t[i]].back()) {
      n += s_len - current_index - 1;
      current_index = -1;
      for (uint j = 'a'; j <= 'z'; j++)
        last_index[j] = 0;
    }

    for (uint j = last_index[t[i]]; j < index[t[i]].size(); j++) {
      if (current_index < index[t[i]][j]) {
        n += index[t[i]][j] - current_index;
        current_index = index[t[i]][j];
        last_index[t[i]] = j;
        i++;
        break;
      }
    }
  }

  cout << n << endl;

  return 0;
}


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

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

  const uint s_len = s.size();
  const uint t_len = t.size();

  vector<int> index['z' + 1];
  int last_index['z' + 1]{0};

  for (uint i = 0; i < s_len; i++)
    index[s[i]].push_back(i);

  uint64_t n = 0;
  int current_index = -1;

  for (uint i = 0; i < t_len;) {
    if (index[t[i]].size() == 0) {
      cout << -1 << endl;
      return 0;
    }
    if (current_index >= index[t[i]].back()) {
      n += s_len - current_index - 1;
      current_index = -1;
      for (uint j = 'a'; j <= 'z'; j++)
        last_index[j] = 0;
    }

    for (uint j = last_index[t[i]]; j < index[t[i]].size(); j++) {
      if (current_index < index[t[i]][j]) {
        n += index[t[i]][j] - current_index;
        current_index = index[t[i]][j];
        last_index[t[i]] = j;
        i++;
        break;
      }
    }
  }

  cout << n << endl;

  return 0;
}
Problem p02937 (C++)

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

string s, t;
vector<int> harf_indexs[26];
void cons() {
  for (int i = 0; i < s.size(); i++) {
    int m_harf = s[i] - 'a';
    harf_indexs[m_harf].push_back(i);
  }
}

int bin_search(int m_harf, int k) {
  int l = 0, r = harf_indexs[m_harf].size() - 1;
  while (l < r) {
    int mid = (l + r) / 2;
    if (harf_indexs[m_harf][mid] > k) {
      r = mid;
    } else {
      l = mid + 1;
    }
  }
  return harf_indexs[m_harf][l];
}

int main() {
  cin >> s >> t;
  cons();
  int k = -1;
  int s_sayisi = 0;
  for (int i = 0; i < t.size(); i++) {
    int m_harf = t[i] - 'a';
    if (harf_indexs[m_harf].empty()) {
      cout << -1 << endl;
      return 0;
    }
    if (harf_indexs[m_harf].back() <= k) {
      s_sayisi++;
      k = harf_indexs[m_harf][0];
      continue;
    }
    k = bin_search(m_harf, k);
  }
  cout << s_sayisi * s.size() + k + 1 << endl;
  return 9;
}


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

string s, t;
vector<int> harf_indexs[26];
void cons() {
  for (int i = 0; i < s.size(); i++) {
    int m_harf = s[i] - 'a';
    harf_indexs[m_harf].push_back(i);
  }
}

int bin_search(int m_harf, int k) {
  int l = 0, r = harf_indexs[m_harf].size() - 1;
  while (l < r) {
    int mid = (l + r) / 2;
    if (harf_indexs[m_harf][mid] > k) {
      r = mid;
    } else {
      l = mid + 1;
    }
  }
  return harf_indexs[m_harf][l];
}

int main() {
  cin >> s >> t;
  cons();
  int k = -1;
  int s_sayisi = 0;
  for (int i = 0; i < t.size(); i++) {
    int m_harf = t[i] - 'a';
    if (harf_indexs[m_harf].empty()) {
      cout << -1 << endl;
      return 0;
    }
    if (harf_indexs[m_harf].back() <= k) {
      s_sayisi++;
      k = harf_indexs[m_harf][0];
      continue;
    }
    k = bin_search(m_harf, k);
  }
  cout << s_sayisi * s.size() + k + 1 << endl;
  return 0;
}
Problem p02937 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
#define maxn 100005
#define ll long long
using namespace std;
char s[maxn * 2];
int last[maxn * 2][26], v[26];
int main() {
  scanf("%s", s + 1);
  int n = strlen(s + 1);
  for (int i = 1; i <= n; i++)
    s[i + n] = s[i];
  for (int i = 2 * n; i >= 0; i--) {
    for (int j = 0; j < 26; j++)
      last[i][j] = v[j];
    v[s[i] - 'a'] = i;
  }
  scanf("%s", s + 1);
  int m = strlen(s + 1);
  int now = last[0][s[1] - 'a'];
  ll ans = now;
  for (int i = 2; i <= m; i++) {
    if (!now)
      return printf("-1\n"), 0;
    ans += last[now][s[i] - 'a'] - now;
    now = last[now][s[i] - 'a'];
    if (now > n)
      now -= n;
  }
  if (!now)
    ans = -1;
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstring>
#define maxn 100005
#define ll long long
using namespace std;
char s[maxn * 2];
int last[maxn * 2][26], v[26];
int main() {
  scanf("%s", s + 1);
  int n = strlen(s + 1);
  for (int i = 1; i <= n; i++)
    s[i + n] = s[i];
  for (int i = 2 * n; i >= 0; i--) {
    for (int j = 0; j < 26; j++)
      last[i][j] = v[j];
    if (i)
      v[s[i] - 'a'] = i;
  }
  scanf("%s", s + 1);
  int m = strlen(s + 1);
  int now = last[0][s[1] - 'a'];
  ll ans = now;
  for (int i = 2; i <= m; i++) {
    if (!now)
      return printf("-1\n"), 0;
    ans += last[now][s[i] - 'a'] - now;
    now = last[now][s[i] - 'a'];
    if (now > n)
      now -= n;
  }
  if (!now)
    ans = -1;
  printf("%lld\n", ans);
  return 0;
}