alexjercan/bugnet huggingface dataset:
Problem p02235 (C++) — buggy submission, verdict: Runtime Error

// #if 0
//????????????????§£?????????????????????????????????????????????????????????C++??§string??????????????§???
//?????????????????§???????????§???AOJ?????????????????????Runtime
//Error??????????????????????????????
//??£????????¨??????????????????????????????????????????????????????Runtime
//Error???????????¨???????????????
//??§???????????????????????????????????§???
// #endif
#include <iostream>
#include <string>
#define lth 1010

using namespace std;

int main() {
  int n, L[1010][1010], Lmax = 0, *save;
  string *Xi, *Yi;
  cin >> n;
  save = new int[n];

  Xi = new string[lth];
  Yi = new string[lth];

  for (int k = 0; k < n; k++) {
    Lmax = 0;
    string X, Y;
    cin >> X >> Y;
    for (int i = 0; i <= X.length(); i++) {
      if (i != 0)
        Xi[i] = X.substr(0, i);
    }
    for (int i = 0; i <= Y.length(); i++) {
      if (i != 0)
        Yi[i] = Y.substr(0, i);
    }
    for (int i = 0; i <= X.length(); i++)
      for (int j = 0; j <= Y.length(); j++) {
        if (i == 0 || j == 0)
          L[i][j] = 0;
        else if (Xi[i][i - 1] == Yi[j][j - 1]) {
          L[i][j] = L[i - 1][j - 1] + 1;
        } else {
          L[i][j] = (L[i][j - 1] > L[i - 1][j] ? L[i][j - 1] : L[i - 1][j]);
        }
        if (Lmax < L[i][j])
          Lmax = L[i][j];
      }
    save[k] = Lmax;
  }

  for (int i = 0; i < n; i++)
    cout << save[i] << endl;
}

Error: 0

Fixed submission:

// #if 0
//????????????????§£?????????????????????????????????????????????????????????C++??§string??????????????§???
//?????????????????§???????????§???AOJ?????????????????????Runtime
//Error??????????????????????????????
//??£????????¨??????????????????????????????????????????????????????Runtime
//Error???????????¨???????????????
//??§???????????????????????????????????§???
// #endif
#include <iostream>
#include <string>
#define lth 1000

using namespace std;

int main() {
  int n, L[100][100], Lmax = 0, *save;
  string *Xi, *Yi;
  cin >> n;
  save = new int[n];
  if (n <= 20) {
    Xi = new string[lth];
    Yi = new string[lth];
  } else {
    Xi = new string[100];
    Yi = new string[100];
  }
  for (int k = 0; k < n; k++) {
    Lmax = 0;
    string X, Y;
    cin >> X >> Y;
    for (int i = 0; i <= X.length(); i++) {
      if (i != 0)
        Xi[i] = X.substr(0, i);
    }
    for (int i = 0; i <= Y.length(); i++) {
      if (i != 0)
        Yi[i] = Y.substr(0, i);
    }
    for (int i = 0; i <= X.length(); i++)
      for (int j = 0; j <= Y.length(); j++) {
        if (i == 0 || j == 0)
          L[i][j] = 0;
        else if (Xi[i][i - 1] == Yi[j][j - 1]) {
          L[i][j] = L[i - 1][j - 1] + 1;
        } else {
          L[i][j] = (L[i][j - 1] > L[i - 1][j] ? L[i][j - 1] : L[i - 1][j]);
        }
        if (Lmax < L[i][j])
          Lmax = L[i][j];
      }
    save[k] = Lmax;
  }

  for (int i = 0; i < n; i++)
    cout << save[i] << endl;
}
Problem p02235 (C++) — buggy submission, verdict: Runtime Error

// #if 0
//????????????????§£?????????????????????????????????????????????????????????C++??§string??????????????§???
//?????????????????§???????????§???AOJ?????????????????????Runtime
//Error??????????????????????????????
//??£????????¨??????????????????????????????????????????????????????Runtime
//Error???????????¨???????????????
//??§???????????????????????????????????§???
// #endif
#include <iostream>
#include <string>
#define lth 1010

using namespace std;

int main() {
  int n, L[1010][1010], Lmax = 0, *save;
  string *Xi, *Yi;
  cin >> n;
  save = new int[n];
  if (n <= 20) {
    Xi = new string[lth];
    Yi = new string[lth];
  } else {
    Xi = new string[1010];
    Yi = new string[1010];
  }
  for (int k = 0; k < n; k++) {
    Lmax = 0;
    string X, Y;
    cin >> X >> Y;
    for (int i = 0; i <= X.length(); i++) {
      if (i != 0)
        Xi[i] = X.substr(0, i);
    }
    for (int i = 0; i <= Y.length(); i++) {
      if (i != 0)
        Yi[i] = Y.substr(0, i);
    }
    for (int i = 0; i <= X.length(); i++)
      for (int j = 0; j <= Y.length(); j++) {
        if (i == 0 || j == 0)
          L[i][j] = 0;
        else if (Xi[i][i - 1] == Yi[j][j - 1]) {
          L[i][j] = L[i - 1][j - 1] + 1;
        } else {
          L[i][j] = (L[i][j - 1] > L[i - 1][j] ? L[i][j - 1] : L[i - 1][j]);
        }
        if (Lmax < L[i][j])
          Lmax = L[i][j];
      }
    save[k] = Lmax;
  }

  for (int i = 0; i < n; i++)
    cout << save[i] << endl;
}

Error: 0

Fixed submission:

// #if 0
//????????????????§£?????????????????????????????????????????????????????????C++??§string??????????????§???
//?????????????????§???????????§???AOJ?????????????????????Runtime
//Error??????????????????????????????
//??£????????¨??????????????????????????????????????????????????????Runtime
//Error???????????¨???????????????
//??§???????????????????????????????????§???
// #endif
#include <iostream>
#include <string>
#define lth 1000

using namespace std;

int main() {
  int n, L[1010][1010], Lmax = 0, *save;
  string *Xi, *Yi;
  cin >> n;
  save = new int[n];
  if (n <= 20) {
    Xi = new string[lth];
    Yi = new string[lth];
  } else {
    Xi = new string[100];
    Yi = new string[100];
  }
  for (int k = 0; k < n; k++) {
    Lmax = 0;
    string X, Y;
    cin >> X >> Y;
    for (int i = 0; i <= X.length(); i++) {
      if (i != 0)
        Xi[i] = X.substr(0, i);
    }
    for (int i = 0; i <= Y.length(); i++) {
      if (i != 0)
        Yi[i] = Y.substr(0, i);
    }
    for (int i = 0; i <= X.length(); i++)
      for (int j = 0; j <= Y.length(); j++) {
        if (i == 0 || j == 0)
          L[i][j] = 0;
        else if (Xi[i][i - 1] == Yi[j][j - 1]) {
          L[i][j] = L[i - 1][j - 1] + 1;
        } else {
          L[i][j] = (L[i][j - 1] > L[i - 1][j] ? L[i][j - 1] : L[i - 1][j]);
        }
        if (Lmax < L[i][j])
          Lmax = L[i][j];
      }
    save[k] = Lmax;
  }

  for (int i = 0; i < n; i++)
    cout << save[i] << endl;
}
Problem p02235 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
using namespace std;

int main() {
  int n, m, q;
  string a, b;               // two sequences
  int M[1010][1010] = {{0}}; // Memory matrix

  cin >> q;
  for (int l = 0; l < q; l++) {
    cin >> a >> b;
    n = a.length();
    m = b.length();
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= m; j++) {
        if (a[i] == b[j]) {
          M[i + 1][j + 1] = M[i][j] + 1;
        } else {
          M[i + 1][j + 1] = max(M[i][j + 1], M[i + 1][j]);
        }
      }
    }

    cout << M[n][m] << endl; // The time complexity equals to O (n * m)
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
using namespace std;

int main() {
  int n, m, q;
  string a, b;               // two sequences
  int M[1001][1001] = {{0}}; // Memory matrix

  cin >> q;
  for (int l = 0; l < q; l++) {
    cin >> a >> b;
    n = a.length();
    m = b.length();
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= m; j++) {
        if (a[i] == b[j]) {
          M[i + 1][j + 1] = M[i][j] + 1;
        } else {
          M[i + 1][j + 1] = max(M[i][j + 1], M[i + 1][j]);
        }
      }
    }

    cout << M[n][m] << endl; // The time complexity equals to O (n * m)
  }
  return 0;
}
Problem p02235 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <ccomplex>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

void solve() {
  string x, y;
  cin >> x >> y;
  int xlen = x.length();
  int ylen = y.length();
  int dp[1010][1010];
  dp[0][0] = 0;

  for (int i = 0; i <= xlen; i++) {
    for (int j = 0; j <= ylen; j++) {
      if (i == 0 || j == 0) {
        dp[i + 1][j + 1] = 0;
      } else if (x[i - 1] == y[j - 1]) {
        dp[i + 1][j + 1] = dp[i][j] + 1;
      } else {
        dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);
      }
    }
  }
  cout << dp[xlen + 1][ylen + 1] << endl;
}
int main() {
  int q;
  cin >> q;
  for (int i = 0; i < q; i++)
    solve();
}

Error: 0

Fixed submission:

#include <algorithm>
#include <ccomplex>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

void solve() {
  string x, y;
  cin >> x >> y;
  int xlen = x.length();
  int ylen = y.length();
  int dp[155][155];
  dp[0][0] = 0;

  for (int i = 0; i <= xlen; i++) {
    for (int j = 0; j <= ylen; j++) {
      if (i == 0 || j == 0) {
        dp[i + 1][j + 1] = 0;
      } else if (x[i - 1] == y[j - 1]) {
        dp[i + 1][j + 1] = dp[i][j] + 1;
      } else {
        dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);
      }
    }
  }
  cout << dp[xlen + 1][ylen + 1] << endl;
}
int main() {
  int q;
  cin >> q;
  for (int i = 0; i < q; i++)
    solve();
}
Problem p02235 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
using namespace std;
int lcs[1001][1001];
/*int lcs(string a,string b)
{
    if(a.length()<=0||b.length()<=0)
    {
        return 0;
    }
    else if(a[a.length()-1]==b[b.length()-1])
    {
        return 1+lcs(a.substr(0,a.length()-1),b.substr(0,b.length()-1));
    }
    else
    {
        return
max(lcs(a.substr(0,a.length()),b.substr(0,b.length()-1)),lcs(a.substr(0,a.length()-1),b.substr(0,b.length())));
    }
};*/
int main() {
  int num;
  cin >> num;
  for (int l = 0; l < num; l++) {

    for (int i = 0; i < 1001; i++)
      for (int j = 0; j < 1001; j++)
        lcs[i][j] = 0;
    string a, b;
    cin >> a >> b;
    // cout<<lcs(a,b)<<endl;
    for (int i = 0; i < a.length() + 1; i++) {
      for (int j = 0; j < b.length() + 1; j++) {
        if (i == 0 || j == 0) {
          lcs[i][j] = 0;
        } else if (a[i - 1] == b[j - 1]) {
          lcs[i][j] = lcs[i - 1][j - 1] + 1;
        } else {
          if (lcs[i][j - 1] > lcs[i - 1][j])
            lcs[i][j] = lcs[i][j - 1];
          else
            lcs[i][j] = lcs[i - 1][j];
        }
      }
    }
    cout << lcs[a.length()][b.length()] << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int lcs(string a, string b) {
  if (a.length() <= 0 || b.length() <= 0) {
    return 0;
  } else if (a[a.length() - 1] == b[b.length() - 1]) {
    return 1 + lcs(a.substr(0, a.length() - 1), b.substr(0, b.length() - 1));
  } else {
    return max(lcs(a.substr(0, a.length()), b.substr(0, b.length() - 1)),
               lcs(a.substr(0, a.length() - 1), b.substr(0, b.length())));
  }
};
int main() {
  int num;
  cin >> num;
  for (int i = 0; i < num; i++) {
    string a, b;
    cin >> a >> b;
    cout << lcs(a, b) << endl;
  }
}
Problem p02235 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16>>(x);
#define if_range(x, w, h) if (w <= x && x <= h)
#define BitCheck(a, b) (a >> b) & 1
#define BitSet(a, b) a |= (1 << b)
#define BitunSet(a, b) a &= ~(1 << b)
#define max(a, b) (((a) > (b)) ? (a) : (b))
#define min(a, b) (((a) < (b)) ? (a) : (b))

int compare_int(const void *a, const void *b) { return *(int *)a - *(int *)b; }

int main() {
  int n, m, a, dp[1002][1002] = {0};
  char s[1001], t[1001];
  scanf("%d", &a);
  for (int k = 0; a > k; k++) {
    scanf("%s %s", s, t);
    n = strlen(s);
    m = strlen(t);
    for (int i = 1; n >= i; i++) {
      for (int j = 1; m >= j; j++) {
        if (s[i - 1] == t[j - 1])
          dp[i][j] = dp[i - 1][j - 1] + 1;
        else
          dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
    printf("%d\\n", dp[n][m]);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16>>(x);
#define if_range(x, w, h) if (w <= x && x <= h)
#define BitCheck(a, b) (a >> b) & 1
#define BitSet(a, b) a |= (1 << b)
#define BitunSet(a, b) a &= ~(1 << b)
#define max(a, b) (((a) > (b)) ? (a) : (b))
#define min(a, b) (((a) < (b)) ? (a) : (b))

int compare_int(const void *a, const void *b) { return *(int *)a - *(int *)b; }

int main() {
  int n, m, a, dp[102][102] = {0};
  char s[1001], t[1001];
  scanf("%d", &a);
  for (int k = 0; a > k; k++) {
    scanf("%s %s", s, t);
    n = strlen(s);
    m = strlen(t);
    for (int i = 1; n >= i; i++) {
      for (int j = 1; m >= j; j++) {
        if (s[i - 1] == t[j - 1])
          dp[i][j] = dp[i - 1][j - 1] + 1;
        else
          dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
    printf("%d\\n", dp[n][m]);
  }
  return 0;
}
Problem p02235 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int main() {
  int N;
  int data[1001][1001] = {};
  cin >> N;
  string str1, str2;
  for (int i = 0; i < N; i++) {
    cin >> str1 >> str2;
    for (int i = 0; i < str1.length(); i++) {
      for (int j = 0; j < str2.length(); j++) {
        if (str1[i] == str2[j]) {
          data[i + 1][j + 1] = data[i][j] + 1;
        } else {
          data[i + 1][j + 1] =
              data[i + 1][j] > data[i][j + 1] ? data[i + 1][j] : data[i][j + 1];
        }
      }
    }
    cout << data[str1.length()][str2.length()] << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int f(map<string, int> &dmap, const string &dstr1, string dstr2);

int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    string data[2];
    map<string, int> dmap;
    cin >> data[0] >> data[1];
    if (data[0].length() < data[1].length()) {
      cout << f(dmap, data[1], data[0]) << endl;
    } else {
      cout << f(dmap, data[0], data[1]) << endl;
    }
  }
}
int f(map<string, int> &dmap, const string &dstr1, string dstr2) {
  auto dit = dmap.find(dstr2);
  if (dit != dmap.end()) {
    return (*dit).second;
  }
  int j = 0;
  for (int i = 0; i < dstr1.length() && j < dstr2.length(); i++, j++) {
    while (i < dstr1.length() && dstr1[i] != dstr2[j]) {
      i++;
    }
  }
  if (j == dstr2.length()) {
    dmap.insert(pair<string, int>(dstr2, (int)dstr2.length()));
    return dstr2.length();
  }
  if (dstr2.length() == 1) {
    dmap.insert(pair<string, int>(dstr2, 0));
    return 0;
  }
  f(dmap, dstr1, dstr2.substr(1, dstr2.length() - 1));
  f(dmap, dstr1, dstr2.substr(0, dstr2.length() - 1));
  j = 0;
  for (int i = 2; i < dstr2.length(); i++) {
    int c = f(dmap, dstr1,
              dstr2.substr(0, i - 1) + dstr2.substr(i, dstr2.length() - i));
    if (j < c) {
      j = c;
    }
  }
  dmap.insert(pair<string, int>(dstr2, j));
  return j;
}
Problem p02235 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define rep(i, n) FOR(i, 0, n)
#define DEBUG(x) cout << #x << ": " << x << endl
#define vint vector<int>
#define vdouble vector<double>
#define vstring vector<string>
using namespace std;

#include <map>
#include <queue>
#include <set>

typedef long long ll;
typedef unsigned long long ull;

const int MAX_N = 1000000;
const int MAX_L = 10000;
int q;

string data[MAX_N][2];
ll mem[MAX_L][MAX_L];

ll eval(string &s1, string &s2, int i, int j) {
  int ii = i - 1;
  int jj = j - 1;
  if (ii == -1 || jj == -1)
    return 0;
  if (s1[ii] == s2[jj])
    return mem[i - 1][j - 1] + 1;
  if (s1[ii] != s2[jj])
    return max(mem[i - 1][j], mem[i][j - 1]);
  throw "exception";
}

ll solve(string s1, string s2) {

  int L1 = s1.size();
  int L2 = s2.size();
  rep(i, L1 + 1) {
    rep(j, L2 + 1) { mem[i][j] = eval(s1, s2, i, j); }
  }

  return mem[L1][L2];
}

int main() {

  cin >> q;
  rep(i, q) {
    cin >> data[i][0];
    cin >> data[i][1];
  }

  rep(i, q) { cout << solve(data[i][0], data[i][1]) << endl; }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define rep(i, n) FOR(i, 0, n)
#define DEBUG(x) cout << #x << ": " << x << endl
#define vint vector<int>
#define vdouble vector<double>
#define vstring vector<string>
using namespace std;

#include <map>
#include <queue>
#include <set>

typedef long long ll;
typedef unsigned long long ull;

const int MAX_N = 1000000;
const int MAX_L = 10000;
int q;

string data[MAX_N][2];
ll mem[MAX_L][MAX_L];

ll eval(string s1, string s2, int i, int j) {
  int ii = i - 1;
  int jj = j - 1;
  if (ii == -1 || jj == -1)
    return 0;
  if (s1[ii] == s2[jj])
    return mem[i - 1][j - 1] + 1;
  if (s1[ii] != s2[jj])
    return max(mem[i - 1][j], mem[i][j - 1]);
  throw "exception";
}

ll solve(string s1, string s2) {

  int L1 = s1.size();
  int L2 = s2.size();
  rep(i, L1 + 1) {
    rep(j, L2 + 1) { mem[i][j] = eval(s1, s2, i, j); }
  }

  return mem[L1][L2];
}

int main() {

  cin >> q;
  rep(i, q) {
    cin >> data[i][0];
    cin >> data[i][1];
  }

  rep(i, q) { cout << solve(data[i][0], data[i][1]) << endl; }
}
Problem p02235 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <inttypes.h>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>

int LCS(std::string s, std::string t) {
  int n, m, c[1001][1001] = {0};
  n = s.size();
  m = t.size();
  int maxlen = 0;
  for (int i = 1; i <= n; i++) {
    c[i][0] = 0;
  }
  for (int i = 1; i <= m; i++) {
    c[0][i] = 0;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (s[i - 1] == t[j - 1]) {
        c[i][j] = c[i - 1][j - 1] + 1;
      } else {
        c[i][j] = std::max(c[i - 1][j], c[i][j - 1]);
      }
      maxlen = std::max(maxlen, c[i][j]);
    }
  }
  return maxlen;
}
int main() {
  int N;

  std::cin >> N;
  std::string s1, s2;
  for (int i = 0; i < N; i++) {
    std::cin >> s1 >> s2;
    std::cout << LCS(s1, s2) << std::endl;
  }
  return 0;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <inttypes.h>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>

int LCS(std::string s, std::string t) {
  int n, m, c[n + 1][m + 1];
  n = s.size();
  m = t.size();
  int maxlen = 0;
  for (int i = 0; i <= n; i++) {
    c[i][0] = 0;
  }
  for (int i = 0; i <= m; i++) {
    c[0][i] = 0;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (s[i] == t[i]) {
        c[i][j] = c[i - 1][j - 1] + 1;
      } else {
        c[i][j] = std::max(c[i - 1][j], c[i][j - 1]);
      }
      maxlen = std::max(c[i][j], maxlen);
    }
  }
  return maxlen;
}
int main() {
  int N;

  std::cin >> N;
  std::string s1, s2;
  for (int i = 0; i < N; i++) {
    std::cin >> s1 >> s2;
    std::cout << LCS(s1, s2) << std::endl;
  }

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

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

#if __has_include("print.hpp")
#include "print.hpp"
#endif

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

typedef long long ll;
typedef pair<int, int> p;

vector<vector<int>> dp(2000, vector<int>(2000));

void solve(string s, string t) {
  int n = int(s.size());
  int m = int(t.size());

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (s[i] == t[j]) {
        dp[i + 1][j + 1] = dp[i][j] + 1;
      } else {
        dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);
      }
    }
  }

  cout << dp[n][m] << endl;
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    string s, t;
    cin >> s >> t;
    solve(s, t);
  }
}


Error: MLE

Fixed submission:

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

#if __has_include("print.hpp")
#include "print.hpp"
#endif

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

typedef long long ll;
typedef pair<int, int> p;

int n, w;
vector<vector<int>> dp(10000, vector<int>(10000, -1));
vector<int> vv;
vector<int> wv;

void solve() {
  for (int i = n - 1; i >= 0; i--) {
    for (int j = 0; j < w; j++) {
      if (j < wv[i]) {
        dp[i][j] = dp[i + 1][j];
      } else {
        dp[i][j] = max(dp[i + 1][j], dp[i + 1][j - wv[i]] + vv[i]);
      }
    }
  }
}

int main() {
  cin >> n >> w;
  for (int i = 0; i < n; i++) {
    int tmp1, tmp2;
    cin >> tmp1 >> tmp2;
    wv.push_back(tmp1);
    vv.push_back(tmp2);
  }

  solve();
  cout << dp[0][w] << endl;
}
Problem p02235 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>
int main() {
  int n;
  int an[1000];
  int l = 0;
  std::cin >> n;
  std::string x;
  std::string y;
  int ans[1001][1001];
  int j, k;
  for (int i = 0; i < n; i++) {
    std::cin >> x;
    std::cin >> y;
    for (int j = 0; j <= 1000; j++) {
      for (int k = 0; k <= 1000; k++) {
        ans[j][k] = 0;
      }
    }
    for (int j = 1; j < x.size() + 1; j++) {
      for (int k = 1; k < y.size() + 1; k++) {
        if (x[j - 1] == y[k - 1]) {
          ans[j][k] = ans[j - 1][k - 1] + 1;
        } else {
          if (ans[j][k - 1] > ans[j - 1][k]) {
            ans[j][k] = ans[j][k - 1];
          } else {
            ans[j][k] = ans[j - 1][k];
          }
        }
      }
    }
    std::cout << ans[x.size()][y.size()] << '\\n';
  }
  return 0;
}


Error: -11

Fixed submission:

#include <iostream>
#include <string>
int main() {
  int n;
  int an[1000];
  int l = 0;
  std::cin >> n;
  std::string x;
  std::string y;
  int ans[10000][10000];
  int j, k;
  for (int i = 0; i < n; i++) {
    std::cin >> x;
    std::cin >> y;
    for (int j = 0; j < 10000; j++) {
      for (int k = 0; k < 10000; k++) {
        ans[j][k] = 0;
      }
    }
    for (int j = 0; j < x.size(); j++) {
      for (int k = 0; k < y.size(); k++) {
        if (x[j] == y[k]) {
          ans[j][k] = ans[j - 1][k - 1] + 1;
        } else {
          if (ans[j][k - 1] > ans[j - 1][k]) {
            ans[j][k] = ans[j][k - 1];
          } else {
            ans[j][k] = ans[j - 1][k];
          }
        }
      }
    }
    std::cout << ans[x.size() - 1][y.size() - 1] << '\\n';
  }
  return 0;
}