alexjercan/bugnet huggingface dataset:
Problem p02250 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
int n, sa[1 << 20], r[1 << 20], tmp[1 << 20];
char s[1 << 20];
int k;
bool comp(int i, int j) {
  if (r[i] != r[j])
    return r[i] < r[j];
  else {
    int ri = i + k <= n ? r[i + k] : -1;
    int rj = j + k <= n ? r[j + k] : -1;
    return ri < rj;
  }
}
void construct() {
  n = strlen(s);
  for (int i = 0; i <= n; i++) {
    sa[i] = i;
    r[i] = i < n ? s[i] : -1;
  }
  for (k = 1; k <= n; k *= 2) {
    sort(sa, sa + n + 1, comp);
    tmp[sa[0]] = 0;
    for (int i = 1; i <= n; i++) {
      tmp[sa[i]] = tmp[sa[i - 1]] + comp(sa[i - 1], sa[i]);
    }
    for (int i = 0; i <= n; i++)
      r[i] = tmp[i];
  }
}
char t[1010];
int T;
int c(int i) {
  int j = 0;
  for (; j < T && s[i + j] == t[j]; j++)
    ;
  return j < T ? t[j] - s[i + j] : 0;
}
bool func() {
  int f = 0, l = n + 1;
  while (l - f > 1) {
    int m = (f + l) / 2;
    if (c(sa[m]) < 0)
      l = m;
    else
      f = m;
  }
  return c(sa[f]) == 0;
}
main() {
  int p;
  scanf("%s\\n%d\\n", s, &p);
  construct();
  for (; ~scanf("%s\\n", t);) {
    T = strlen(t);
    puts(func() ? "1" : "0");
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef unsigned long long int ull;

const ull B = 1e9 + 7; // base of hash

bool contain(string s, string a) {
  int sl = s.length(), al = a.length();
  if (sl < al)
    return false;

  ull t = 1;
  for (int i = 0; i < al; i++)
    t *= B;

  ull sh = 0, ah = 0;
  for (int i = 0; i < al; i++) {
    sh = sh * B + s[i];
    ah = ah * B + a[i];
  }

  if (sh == ah)
    return true;

  for (int i = 0; i + al < sl; i++) {
    sh = sh * B + s[i + al] - s[i] * t;
    if (sh == ah)
      return true;
  }

  return false;
}

vector<int> match(string s, string a) {
  int sl = s.length(), al = a.length();
  vector<int> ans;
  if (sl < al)
    return ans;
  ull t = 1;
  ull ah = 0, sh = 0;
  for (int i = 0; i < al; i++) {
    sh = sh * B + s[i];
    ah = ah * B + a[i];
    t *= B;
  }

  if (sh == ah)
    ans.push_back(0);

  for (int i = 0; i + al < sl; i++) {
    sh = sh * B + s[i + al] - s[i] * t;
    if (sh == ah)
      ans.push_back(i + 1);
  }
  return ans;
}

int main() {
  string s;
  cin >> s;
  int n;
  cin >> n;
  string a;
  for (int i = 0; i < n; i++) {
    cin >> a;
    cout << contain(s, a) << endl;
  }
  return 0;
}
Problem p02250 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <functional>
#include <iostream>
#include <string>
using namespace std;
#define MAX_N 1100000
int Rank[MAX_N], x[MAX_N];
int SA[MAX_N];
string S, T;
int V;
bool Compare(int p, int q) {
  if (Rank[p] < Rank[q])
    return true;
  if (Rank[p] > Rank[q])
    return false;
  int D1 = -1, D2 = -1;
  if (p + V <= S.size())
    D1 = Rank[p + V];
  if (q + V <= S.size())
    D2 = Rank[q + V];
  if (D1 < D2)
    return true;
  return false;
}
void Build() {
  for (int i = 0; i < S.size(); i++) {
    SA[i] = i;
    Rank[i] = S[i];
  }
  SA[S.size()] = S.size();
  Rank[S.size()] = -1;
  for (V = 1; V <= S.size(); V *= 2) {
    sort(SA, SA + S.size() + 1, Compare);
    x[SA[0]] = 0;
    for (int i = 1; i <= S.size(); i++) {
      x[SA[i]] = x[SA[i - 1]] + Compare(SA[i - 1], SA[i]);
    }
    for (int i = 0; i <= S.size(); i++)
      Rank[i] = x[i];
  }
}
int hantei(int C, string &V2) {
  int H = min(S.size() - C, V2.size());
  for (int i = 0; i < H; i++) {
    if (S[i + C] < V2[i])
      return -1;
    if (S[i + C] > V2[i])
      return 1;
  }
  return 0;
}
int main() {
  cin >> S;
  S += '!';
  int Q;
  cin >> Q;
  Build();
  for (int i = 0; i < Q; i++) {
    cin >> T;
    int L = 0, R = S.size() + 1, M;
    while (L + 1 < R) {
      M = (L + R) / 2;
      if (hantei(SA[M], T) <= 0)
        L = M;
      else {
        R = M;
      }
    }
    if (hantei(SA[L], T) == 0)
      cout << "1" << endl;
    else
      cout << "0" << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
string S;
string U;
vector<string> T;
int N;
int main() {
  cin >> S >> N;
  for (int i = 0; i < S.size(); i++) {
    int L = S.size() - i;
    if (L > 250)
      L = 250;
    T.push_back(S.substr(i, L));
  }
  sort(T.begin(), T.end());
  for (int i = 0; i < N; i++) {
    cin >> U;
    if (U.size() >= 250) {
      U = U.substr(0, 250);
    }
    int pos = lower_bound(T.begin(), T.end(), U) - T.begin();
    string V;
    if (pos < T.size() && U.size() <= T[pos].size()) {
      V = T[pos].substr(0, U.size());
    }
    if (U == V) {
      cout << "1" << endl;
    } else {
      cout << "0" << endl;
    }
  }
  return 0;
}
Problem p02250 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int n, k;
string s;
vector<int> order;
vector<int> tmp;
vector<int> sa;

bool compare_sa(const int &i, const int &j) {
  if (order[i] != order[j])
    return order[i] < order[j];
  else {
    int ri = i + k <= n ? order[i + k] : -1;
    int rj = j + k <= n ? order[j + k] : -1;
    return ri < rj;
  }
}

void construct_sa() {
  for (int i = 0; i <= n; i++) {
    sa[i] = i;
    order[i] = i < n ? s[i] : -1;
  }
  for (k = 1; k <= n; k *= 2) {
    sort(sa.begin(), sa.end(), compare_sa);
    tmp[sa[0]] = 0;
    for (int i = 1; i <= n; i++)
      tmp[sa[i]] = tmp[sa[i - 1]] + (compare_sa(sa[i - 1], sa[i]) ? 1 : 0);
    for (int i = 0; i <= n; i++)
      order[i] = tmp[i];
  }
}

void init() {
  n = s.size();
  order.resize(n + 1);
  tmp.resize(n + 1);
  sa.resize(n + 1);
  construct_sa();
}

bool contain(string t) {
  int l = 0, r = n;
  while (r - l > 1) {
    int m = (l + r) / 2;
    if (s.compare(sa[m], t.size(), t) < 0)
      l = m;
    else
      r = m;
  }
  return s.compare(sa[r], t.size(), t) == 0;
}

int main() {

  cin >> s;
  string t;
  int q;
  cin >> q;
  init();
  construct_sa();
  for (int i = 0; i < q; ++i) {
    cin >> t;
    cout << contain(t) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

#define REP(i, n) for (int i = 0; i < n; i++)

using namespace std;

int n, k;
string s;
vector<int> order;
vector<int> tmp;
vector<int> sa;

bool compare_sa(const int &i, const int &j) {
  if (order[i] != order[j])
    return order[i] < order[j];
  else {
    int ri = i + k <= n ? order[i + k] : -1;
    int rj = j + k <= n ? order[j + k] : -1;
    return ri < rj;
  }
}

void construct_sa() {
  for (int i = 0; i <= n; i++) {
    sa[i] = i;
    order[i] = i < n ? s[i] : -1;
  }
  for (k = 1; k <= n; k *= 2) {
    sort(sa.begin(), sa.end(), compare_sa);
    tmp[sa[0]] = 0;
    for (int i = 1; i <= n; i++)
      tmp[sa[i]] = tmp[sa[i - 1]] + (compare_sa(sa[i - 1], sa[i]) ? 1 : 0);
    for (int i = 0; i <= n; i++)
      order[i] = tmp[i];
  }
}

void init() {
  n = s.size();
  order.resize(n + 1);
  tmp.resize(n + 1);
  sa.resize(n + 1);
  construct_sa();
}

bool contain(string t) {
  int l = 0, r = n;
  while (r - l > 1) {
    int m = (l + r) / 2;
    if (s.compare(sa[m], t.size(), t) < 0)
      l = m;
    else
      r = m;
  }
  return s.compare(sa[r], t.size(), t) == 0;
}

int main() {
  cin >> s;
  string t;
  int q;
  cin >> q;
  init();
  construct_sa();
  REP(i, q) {
    cin >> t;
    cout << contain(t) << endl;
  }
  return 0;
}
Problem p02250 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>

#define MAX 1000000

using namespace std;

int n, k;
int rankk[MAX + 1];
int tmp[MAX + 1];
int sa[MAX + 1];

bool compare_sa(int i, int j) {
  if (rankk[i] != rankk[j])
    return rankk[i] < rankk[j];
  else {
    int ri = (i + k <= n ? rankk[i + k] : -1);
    int rj = (j + k <= n ? rankk[j + k] : -1);
    return ri < rj;
  }
}

void construct_sa(string S) {
  n = S.length();
  //???????¨???????, 1????????§, rank????????????????????§??????.
  for (int i = 0; i <= n; i++) {
    sa[i] = i;
    rankk[i] = (i < n ? S[i] : -1);
  }

  // k??????????????????sort?????????????????¨?????????, 2k????????§sort
  for (k = 1; k <= n; k *= 2) {
    sort(sa, sa + n + 1, compare_sa);
    // tmp????¬????rank????¨????, rank????§?????????????.
    tmp[sa[0]] = 0;
    for (int i = 1; i <= n; i++) {
      tmp[sa[i]] = tmp[sa[i - 1]] + (compare_sa(sa[i - 1], sa[i]) ? 1 : 0);
    }
    for (int i = 0; i <= n; i++) {
      rankk[i] = tmp[i];
    }
  }
}

bool contains(string S, string P) {
  int a = 0;
  int b = S.length();
  while (b - a > 1) {
    int c = (a + b) / 2;
    // S???sa[C]???????????????|T|????????¨T?????????
    if (S.compare(sa[c], P.length(), P) < 0)
      a = c;
    else
      b = c;
  }
  return S.compare(sa[b], P.length(), P) == 0;
}

int main() {
  int q;
  bool ans;
  string T, P;
  cin >> T;
  construct_sa(T); // Construct Suffix Array
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    cin.ignore();
    cin >> P;
    ans = contains(T, P);
    if (ans == true)
      printf("1\\n");
    else
      printf("0\\n");
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>

using namespace std;

typedef unsigned long long ull;
const ull B = 100000007;
ull thash[1001];
ull BB[1001];
string T;
int bl;

bool contain(string a) {
  int al = a.length();
  ull t;
  // B???al???????¨????
  /*
  ull t=1;
  for(int i=0;i<al;i++) t*=B;
  */
  // a??¨b????????????al???????????¢?????????????????\\???????¨????
  ull ah = 0, bh = 0;
  for (int i = 0; i < al; i++)
    ah = ah * B + a[i];
  // for(int i=0;i<al;i++) bh=bh*B+b[i];

  // b?????´????????????????????????????????????????????\\???????????§??????
  bh = thash[al];
  t = BB[al];
  for (int i = 0; i + al <= bl; i++) {
    if (ah == bh)
      return true;
    bh = bh * B + T[i + al] - T[i] * t;
  }
  return false;
}

int main() {
  int k;
  bool ans;
  string P;

  cin >> T;
  bl = T.length();

  thash[0] = 0;
  for (int i = 1; i <= 1000; i++)
    thash[i] = thash[i - 1] * B + T[i - 1];
  BB[0] = 1;
  for (int i = 1; i <= 1000; i++)
    BB[i] = BB[i - 1] * B;

  scanf("%d", &k);
  for (int i = 0; i < k; i++) {
    cin >> P;
    if (T.size() >= P.size()) {
      ans = contain(P); // P???T?????????????????????????
      if (ans)
        printf("1\\n");
      else
        printf("0\\n");
    } else
      printf("0\\n");
  }
}
Problem p02250 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <string.h>

using namespace std;

char str[1000001];
int k, len;
int sa[1000001], Rank[1000001], temp[1000001];

bool compare_sa(int i, int j) {
  if (Rank[i] == Rank[j]) {
    int t1 = -1, t2 = -1;
    if (i + k <= len) {
      t1 = Rank[i + k];
    }
    if (j + k <= len) {
      t2 = Rank[j + k];
    }
    return t1 < t2;
  } else {
    return Rank[i] < Rank[j];
  }
}

void construct_sa() {
  for (int i = 0; i <= len; i++) {
    sa[i] = i;
    if (i == len) {
      Rank[i] = -1;
    } else {
      Rank[i] = str[i];
    }
  }

  for (k = 1; k <= len; k *= 2) {
    sort(sa, sa + len + 1, compare_sa);

    temp[sa[0]] = 0;
    for (int i = 1; i <= len; i++) {
      temp[sa[i]] = temp[sa[i - 1]] + compare_sa(sa[i - 1], sa[i]);
    }
    for (int i = 0; i <= len; i++) {
      Rank[i] = temp[i];
    }
  }
}

int main() {
  cin >> str;
  len = strlen(str);

  construct_sa();

  int n, tlen;
  char t[1001];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> t;
    tlen = strlen(t);
    int left = 0, right = len + 1, pivot;
    while (left + 1 < right) {
      pivot = (left + right) / 2;

      if (strncmp(str + sa[pivot], t, tlen) <= 0) {
        left = pivot;
      } else {
        right = pivot;
      }
    }
    cout << (strncmp(str + sa[left], t, tlen) == 0) << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

vector<string> sa;
string P;
int Plen;

void qmultisort(vector<string> &src, int size, int suffix) {
  if (size <= 1) {
    return;
  }
  vector<string> lower, middle, larger;
  char pivot = src[size / 2][suffix];

  for (int i = 0; i < size; i++) {
    if (src[i][suffix] == pivot) {
      middle.push_back(src[i]);
    } else if (src[i][suffix] < pivot) {
      lower.push_back(src[i]);
    } else {
      larger.push_back(src[i]);
    }
  }

  int lowsize = lower.size(), midsize = middle.size(), larsize = larger.size(),
      now = 0;
  qmultisort(lower, lowsize, suffix);
  qmultisort(middle, midsize, suffix + 1);
  qmultisort(larger, larsize, suffix);

  for (int i = 0; i < lowsize; i++) {
    src[now++] = lower[i];
  }
  for (int i = 0; i < midsize; i++) {
    src[now++] = middle[i];
  }
  for (int i = 0; i < larsize; i++) {
    src[now++] = larger[i];
  }
}

bool binary_search(int left, int right) {
  if (left > right) {
    return false;
  }

  int pivot = (left + right) / 2;
  if (P == sa[pivot].substr(0, Plen)) {
    return true;
  } else if (P < sa[pivot]) {
    return binary_search(0, pivot - 1);
  } else {
    return binary_search(pivot + 1, right);
  }
}

int main() {
  string T;
  int Tlen, n;
  cin >> T;
  Tlen = T.size();

  sa.resize(Tlen);
  for (int i = 0; i < Tlen; i++) {
    sa[i] = T.substr(i);
  }
  qmultisort(sa, Tlen, 0);

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> P;
    Plen = P.size();
    cout << binary_search(0, Tlen - 1) << endl;
  }

  return 0;
}
Problem p02250 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

#define REP(i, n) for (int i = 0; i < n; i++)

using namespace std;

int n, k;
string s;
vector<int> order;
vector<int> tmp;
vector<int> sa;

bool compare_sa(const int &i, const int &j) {
  if (order[i] != order[j])
    return order[i] < order[j];
  else {
    int ri = i + k <= n ? order[i + k] : -1;
    int rj = j + k <= n ? order[j + k] : -1;
    return ri < rj;
  }
}

void construct_sa() {
  for (int i = 0; i <= n; i++) {
    sa[i] = i;
    order[i] = i < n ? s[i] : -1;
  }
  for (k = 1; k <= n; k *= 2) {
    sort(sa.begin(), sa.end(), compare_sa);
    tmp[sa[0]] = 0;
    for (int i = 1; i <= n; i++)
      tmp[sa[i]] = tmp[sa[i - 1]] + (compare_sa(sa[i - 1], sa[i]) ? 1 : 0);
    for (int i = 0; i <= n; i++)
      order[i] = tmp[i];
  }
}

void init() {
  n = s.size();
  order.resize(n + 1);
  tmp.resize(n + 1);
  sa.resize(n + 1);
  construct_sa();
}

bool contain(string t) {
  int l = 0, r = n;
  while (r - l > 1) {
    int m = (l + r) / 2;
    if (s.compare(sa[m], t.size(), t) < 0)
      l = m;
    else
      r = m;
  }
  return s.compare(sa[r], t.size(), t) == 0;
}

int main() {
  cin >> s;
  string t;
  int q;
  cin >> q;
  init();
  construct_sa();
  REP(i, q) {
    cin >> t;
    cout << contain(t) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define REP(i, n) for (int i = 0; i < n; i++)

using namespace std;

class SuffixArray {
private:
  int n, k;
  string s;
  vector<int> rank;
  vector<int> tmp;
  bool compare_sa(const int &i, const int &j) {
    if (rank[i] != rank[j])
      return rank[i] < rank[j];
    else {
      int ri = i + k <= n ? rank[i + k] : -1;
      int rj = j + k <= n ? rank[j + k] : -1;
      return ri < rj;
    }
  }
  void construct_sa() {
    for (int i = 0; i <= n; i++) {
      sa[i] = i;
      rank[i] = i < n ? s[i] : -1;
    }
    for (k = 1; k <= n; k *= 2) {
      sort(sa.begin(), sa.end(), *this);
      tmp[sa[0]] = 0;
      for (int i = 1; i <= n; i++)
        tmp[sa[i]] = tmp[sa[i - 1]] + (compare_sa(sa[i - 1], sa[i]) ? 1 : 0);
      for (int i = 0; i <= n; i++)
        rank[i] = tmp[i];
    }
  }

public:
  vector<int> sa;
  bool operator()(const int &i, const int &j) const {
    if (rank[i] != rank[j])
      return rank[i] < rank[j];
    else {
      int ri = i + k <= n ? rank[i + k] : -1;
      int rj = j + k <= n ? rank[j + k] : -1;
      return ri < rj;
    }
  }
  SuffixArray(string s) : s(s) {
    n = s.size();
    rank.resize(n + 1);
    tmp.resize(n + 1);
    sa.resize(n + 1);
    construct_sa();
  }

  bool contain(string t) {
    int l = 0, r = n;
    while (r - l > 1) {
      int m = (l + r) / 2;
      if (s.compare(sa[m], t.size(), t) < 0)
        l = m;
      else
        r = m;
    }
    return s.compare(sa[r], t.size(), t) == 0;
  }
};

int main() {
  string s, t;
  cin >> s;
  auto sa = SuffixArray(s);
  int q;
  cin >> q;
  REP(i, q) {
    cin >> t;
    cout << sa.contain(t) << endl;
  }
  return 0;
}
Problem p02255 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int main() {
  int numOfElement;
  int elem[100] = {};

  cin >> numOfElement;

  for (int i = 0; i < numOfElement; i++) {
    cin >> elem[i];
  }

  for (int i = 0; i < numOfElement; i++) {
    for (int j = i; j > 0 && elem[j] < elem[j - 1]; j--) {
      int buf = elem[j - 1];
      elem[j - 1] = elem[j];
      elem[j] = buf;
    }
    for (int j = 0; j < numOfElement; j++) {
      if (j + 1 >= numOfElement) {
        cout << elem[j];
        break;
      }
      cout << elem[j] << " ";
    }
    cout << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int numOfElement;
  int elem[99] = {};

  cin >> numOfElement;

  for (int i = 0; i < numOfElement; i++) {
    cin >> elem[i];
  }

  for (int i = 0; i < numOfElement; i++) {
    for (int j = i; j > 0 && elem[j] < elem[j - 1]; j--) {
      int buf = elem[j - 1];
      elem[j - 1] = elem[j];
      elem[j] = buf;
    }
    for (int j = 0; j < numOfElement; j++) {
      if (j + 1 >= numOfElement) {
        cout << elem[j];
        break;
      }
      cout << elem[j] << " ";
    }
    cout << endl;
  }

  return 0;
}
Problem p02255 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>

int main() {
  int A[1000], N = 0;
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    scanf("%d", &A[i]);
  }
  for (int i = 0; i < N; i++) {
    int v;
    v = A[i];
    int j = i - 1;
    while (j >= 0 && A[j] > v) {
      A[j + 1] = A[j];
      j--;
      A[j + 1] = v;
    }
    for (int i = 0; i < N - 1; i++) {
      printf("%d ", A[i]);
    }
    printf("%d\\n", A[N - 1]);
  }
}

Error: -11

Fixed submission:

#include <stdio.h>

int main() {
  int A[1000], N = 0;
  scanf("%d", N);
  for (int i = 0; i < N; i++) {
    scanf("%d", A[i]);
  }
  for (int i = 0; i < N; i++) {
    int v;
    v = A[i];
    int j = i - 1;
    while (j >= 0 && A[j] > v) {
      A[j + 1] = A[j];
      j--;
      A[j + 1] = v;
    }
    for (int i = 0; i < N - 1; i++) {
      printf("%d ", A[i]);
    }
    printf("%d\\n", A[N - 1]);
  }
}
Problem p02255 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

namespace myspace {

class vector {
public:
  void push_back(int);
  int size();
  int &operator[](int);
  vector();
  ~vector();

private:
  int s;
  int *box;
};

vector::vector() {
  box = NULL;
  s = 0;
}

vector::~vector() {
  if (box)
    delete[] box;
}

int &vector::operator[](int i) { return box[i]; }

int vector::size() { return s; }

void vector::push_back(int input) {
  int *box2 = box;
  box = new int[s + 1];
  for (int i = 0; i < s; i++)
    box[i] = box2[i];

  box[s] = input;
  s++;
}
} // namespace myspace

using namespace myspace;
using namespace std;

void Output(vector &A) {
  int i;
  for (i = 0; i < A.size() - 1; i++) {
    cout << A[i] << " ";
  }
  cout << A[i] << endl;
}

void InsertionSort(vector &A, int n) {
  for (int i = 0; i < n; i++) {
    int v = A[i];
    int j = i - 1;
    while (j >= 0 && A[j] > v) {
      A[j + 1] = A[j];
      j--;
    }
    A[j + 1] = v;
    Output(A);
  }
}

int main() {
  int n;
  cin >> n;

  vector A;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    A.push_back(a);
  }
  InsertionSort(A, n);

  return 0;
}

Error: -6
free(): double free detected in tcache 2


Fixed submission:

#include <iostream>

namespace myspace {

class vector {
public:
  void push_back(int);
  int size();
  int &operator[](int);
  vector();
  ~vector();

private:
  int s;
  int *box;
};

vector::vector() {
  box = NULL;
  s = 0;
}

vector::~vector() {
  if (box)
    delete[] box;
}

int &vector::operator[](int i) { return box[i]; }

int vector::size() { return s; }

void vector::push_back(int input) {
  int *box2 = box;
  box = new int[s + 1];
  for (int i = 0; i < s; i++)
    box[i] = box2[i];

  box[s] = input;
  s++;
}
} // namespace myspace

using namespace myspace;
using namespace std;

void Output(vector A) {
  int i;
  for (i = 0; i < A.size() - 1; i++) {
    cout << A[i] << " ";
  }
  cout << A[i] << endl;
}

void InsertionSort(vector A, int n) {
  for (int i = 0; i < n; i++) {
    int v = A[i];
    int j = i - 1;
    while (j >= 0 && A[j] > v) {
      A[j + 1] = A[j];
      j--;
    }
    A[j + 1] = v;
    Output(A);
  }
}

int main() {
  int n;
  cin >> n;

  vector A;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    A.push_back(a);
  }
  InsertionSort(A, n);

  return 0;
}
Problem p02255 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <iostream>
#include <vector>

using namespace std;

void insortionSort(vector<int> A) {
  for (int i = 0; i < A.size(); i++) {
    int v = A[i];
    int j = i - 1;
    while (j >= 0 && A[j] > v) {
      A[j + 1] = A[j];
      j--;
    }
    A[j + 1] = v;
    for (int i = 0; i < A.size(); i++) {
      cout << A[i];
      if (i != A.size() - 1) {
        cout << ' ';
      }
    }
    cout << endl;
  }
}

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  insortionSort(A);
}


Error: TLE

Fixed submission:


#include <iostream>
#include <vector>

using namespace std;

void insortionSort(vector<int> A) {
  for (int i = 0; i < A.size(); i++) {
    int v = A[i];
    int j = i - 1;
    while (j >= 0 && A[j] > v) {
      A[j + 1] = A[j];
      j--;
    }
    A[j + 1] = v;
    for (int i : A) {
      cout << i;
    }
    cout << endl;
  }
}

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  insortionSort(A);
}