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

#include <iostream>
#include <vector>
using namespace std;

void fnInput(vector<string> &rvsStrX, vector<string> &rvsStrY) {
  int nMaxSiz;
  cin >> nMaxSiz;
  rvsStrX.resize(nMaxSiz);
  rvsStrY.resize(nMaxSiz);

  for (int i = 0; i < nMaxSiz; ++i) {
    cin >> rvsStrX[i] >> rvsStrY[i];
    rvsStrX[i] = ' ' + rvsStrX[i];
    rvsStrY[i] = ' ' + rvsStrY[i];
  }
}

int fnLngCmnSubseq(const vector<string> &cnrvsStrX,
                   const vector<string> &cnrvsStrY) {
  vector<vector<int>> vvnLcsDP;
  for (int i = 0; i < cnrvsStrX.size(); ++i) {
    const int nMAXNX = cnrvsStrX[i].size();
    const int nMAXNY = cnrvsStrY[i].size();

    vvnLcsDP.clear();
    vvnLcsDP.resize(nMAXNX, vector<int>(nMAXNY, 0));

    for (int nx = 1; nx < nMAXNX; ++nx)
      for (int ny = 1; ny < nMAXNY; ++ny)
        if (cnrvsStrX[i][nx] == cnrvsStrY[i][ny])
          vvnLcsDP[nx][ny] = vvnLcsDP[nx - 1][ny - 1] + 1;
        else
          vvnLcsDP[nx][ny] = max(vvnLcsDP[nx - 1][ny], vvnLcsDP[nx][ny - 1]);

    cout << vvnLcsDP[nMAXNX - 1][nMAXNY - 1] << endl;
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  vector<string> vsStrX, vsStrY;

  fnInput(vsStrX, vsStrY);
  fnLngCmnSubseq(vsStrX, vsStrY);
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

void fnInput(vector<string> &rvsStrX, vector<string> &rvsStrY) {
  int nMaxSiz;
  cin >> nMaxSiz;
  rvsStrX.resize(nMaxSiz);
  rvsStrY.resize(nMaxSiz);

  for (int i = 0; i < nMaxSiz; ++i) {
    cin >> rvsStrX[i] >> rvsStrY[i];
    rvsStrX[i] = ' ' + rvsStrX[i];
    rvsStrY[i] = ' ' + rvsStrY[i];
  }
}

int fnLngCmnSubseq(const vector<string> &cnrvsStrX,
                   const vector<string> &cnrvsStrY) {
  vector<vector<int>> vvnLcsDP;
  for (int i = 0; i < cnrvsStrX.size(); ++i) {
    const int nMAXNX = cnrvsStrX[i].size();
    const int nMAXNY = cnrvsStrY[i].size();

    //    vvnLcsDP.clear();
    vvnLcsDP.resize(nMAXNX, vector<int>(nMAXNY, 0));

    for (int nx = 1; nx < nMAXNX; ++nx)
      for (int ny = 1; ny < nMAXNY; ++ny)
        if (cnrvsStrX[i][nx] == cnrvsStrY[i][ny])
          vvnLcsDP[nx][ny] = vvnLcsDP[nx - 1][ny - 1] + 1;
        else
          vvnLcsDP[nx][ny] = max(vvnLcsDP[nx - 1][ny], vvnLcsDP[nx][ny - 1]);

    cout << vvnLcsDP[nMAXNX - 1][nMAXNY - 1] << endl;
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  vector<string> vsStrX, vsStrY;

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

#include <bits/stdc++.h>
using namespace std;
struct cww {
  cww() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
} star;
#define P(x) cout << (x) << endl
#define p(x) cout << (x)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define vv(type, c, m, n, i) vector<vector<type>> c(m, vector<type>(n, i));
#define rep(i, a, n) for (int i = (a), i##_len = (n); i < i##_len; ++i)
#define rrep(i, a, n) for (int i = (a), i##_len = (n); i > i##_len; --i)
#define len(x) ((int)(x).size())
#define mp make_pair
#define eb emplace_back
typedef long long ll;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long long> vll;
typedef vector<string> vs;
typedef vector<bool> vb;
constexpr int MAX = 1005;
inline int lcs(string X, string Y) {
  vv(int, c, MAX, MAX, 0);
  int m = len(X);
  int n = len(Y);
  int maxl = 0;
  X = ' ' + X;
  Y = ' ' + Y;
  rep(i, 1, m + 1) {
    rep(j, 1, n + 1) {
      if (X[i] == Y[j])
        c[i][j] = c[i - 1][j - 1] + 1;
      else
        c[i][j] = max(c[i - 1][j], c[i][j - 1]);
      if (maxl < c[i][j])
        maxl = c[i][j];
    }
  }
  return maxl;
}
int main() {
  int n;
  cin >> n;
  string s1, s2;
  rep(i, 0, n) {
    cin >> s1 >> s2;
    P(lcs(s1, s2));
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
struct cww {
  cww() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
} star;
#define P(x) cout << (x) << endl
#define p(x) cout << (x)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define vv(type, c, m, n, i) vector<vector<type>> c(m, vector<type>(n, i));
#define rep(i, a, n) for (int i = (a), i##_len = (n); i < i##_len; ++i)
#define rrep(i, a, n) for (int i = (a), i##_len = (n); i > i##_len; --i)
#define len(x) ((int)(x).size())
#define mp make_pair
#define eb emplace_back
typedef long long ll;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long long> vll;
typedef vector<string> vs;
typedef vector<bool> vb;
constexpr int MAX = 1e4;
inline int lcs(string X, string Y) {
  vv(int, c, MAX + 1, MAX + 1, 0);
  int m = len(X);
  int n = len(Y);
  int maxl = 0;
  X = ' ' + X;
  Y = ' ' + Y;
  rep(i, 1, m + 1) {
    rep(j, 1, n + 1) {
      if (X[i] == Y[j])
        c[i][j] = c[i - 1][j - 1] + 1;
      else
        c[i][j] = max(c[i - 1][j], c[i][j - 1]);
      if (maxl < c[i][j])
        maxl = c[i][j];
    }
  }
  return maxl;
}
int main() {
  int n;
  cin >> n;
  string s1, s2;
  rep(i, 0, n) {
    cin >> s1 >> s2;
    P(lcs(s1, s2));
  }
  return 0;
}
Problem p02235 (C++) — buggy submission, verdict: Time Limit Exceeded

// ALDS1_10_C.cpp
// Dynamic Programming - Longest Common Subsequence

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int LCS(string s, string t) {
  int res = 0;
  if (s.empty() || t.empty())
    return res;
  int s_len = s.size();
  int t_len = t.size();
  int len = min(s_len, t_len);
  //    int lcs[s_len][t_len];
  vector<vector<int>> lcs(s_len, vector<int>(t_len));
  bool found = false;
  for (int i = 0; i < s_len; i++) {
    if (s[i] == t[0] || found) {
      lcs[i][0] = 1;
      found = true;
    }
  }
  found = false;
  for (int j = 0; j < t_len; j++) {
    if (s[0] == t[j] || found) {
      lcs[0][j] = 1;
      found = true;
    }
  }
  for (int i = 1; i < s_len; i++) {
    for (int j = 1; j < t_len; j++) {
      if (s[i] == t[j]) {
        lcs[i][j] = 1 + lcs[i - 1][j - 1];
      } else {
        lcs[i][j] = max(lcs[i - 1][j], lcs[i][j - 1]);
      }
    }
  }
  return lcs[s_len - 1][t_len - 1];
}

int main() {
  int n;
  cin >> n;
  string s, t;
  while (n--) {
    cin >> s >> t;
    int res = LCS(s, t);
    cout << res << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

// ALDS1_10_C.cpp
// Dynamic Programming - Longest Common Subsequence

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int LCS(string s, string t) {
  int res = 0;
  if (s.empty() || t.empty())
    return res;
  string s1(s.begin() + 1, s.end());
  string t1(t.begin() + 1, t.end());
  if (s[0] == t[0]) {
    res = 1 + LCS(s1, t1);
  } else {
    res = max(LCS(s1, t), LCS(s, t1));
  }
  return res;
}

int main() {
  int n;
  cin >> n;
  string s, t;
  while (n--) {
    cin >> s >> t;
    int res = LCS(s, t);
    cout << res << endl;
  }
  return 0;
}
Problem p02235 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
#include <stdio.h>
#include <string>

#define MAX_LENGTH 1000
#define NIL -1
using namespace std;
// think easy make easy

int q; // num of datasets
string X;
string Y;
int map[MAX_LENGTH + 1][MAX_LENGTH + 1];

int findMaxLength(int x, int y) { // x:Xの終端.y:Yの終端(0オリジン)
  int val;

  if (x < 0 || y < 0)
    return 0;

  if (map[x][y] == NIL) {
    if (X.at(x) == Y.at(y)) {
      val = findMaxLength(x - 1, y - 1) + 1;
    } else {
      val = max(findMaxLength(x - 1, y), findMaxLength(x, y - 1));
    }
    map[x][y] = val; // メモ
    return val;
  } else
    return map[x][y];
}

int main() {

  scanf("%d", &q);

  for (int i = 0; i < q; i++) {
    cin >> X;
    cin >> Y;
    for (int i = 0; i < X.size(); i++) {
      for (int j = 0; j < Y.size(); j++) {
        map[i][j] = NIL;
      }
    }

    cout << findMaxLength(X.size() - 1, Y.size() - 1) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <stdio.h>
#include <string>

#define MAX_LENGTH 1000
#define NIL -1
using namespace std;
// think easy make easy

int q; // num of datasets
string X;
string Y;
char map[MAX_LENGTH + 1][MAX_LENGTH + 1];

int findMaxLength(int x, int y) { // x:Xの終端.y:Yの終端(0オリジン)
  if (x < 0 || y < 0)
    return 0;
  if (map[x][y] == NIL) {
    if (X.at(x) == Y.at(y)) {
      return findMaxLength(x - 1, y - 1) + 1;
    } else {
      return max(findMaxLength(x - 1, y), findMaxLength(x, y - 1));
    }
  } else
    return map[x][y];
}

int main() {

  scanf("%d", &q);

  for (int i = 0; i < q; i++) {
    cin >> X;
    cin >> Y;
    for (int i = 0; i < X.size(); i++) {
      for (int j = 0; j < Y.size(); j++) {
        map[i][j] = NIL;
      }
    }

    cout << findMaxLength(X.size() - 1, Y.size() - 1) << endl;
  }

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

#include <iostream>
#include <string>
#define int long long
// #define _DEBUG
using namespace std;
string sa, sb;
int T[1009][1009];
int solve() {
  int sal = sa.length(), sbl = sb.length();
  for (int a = 0; a <= sal; a++) {
    for (int b = 0; b <= sbl; b++) {
      if (a == 0 || b == 0) {
        T[a][b] = 0;
      } else if (sa[a - 1] == sb[b - 1]) {
        T[a][b] = 1 + T[a - 1][b - 1];
      } else {
        T[a][b] = max(T[a][b - 1], T[a - 1][b]);
      }
    }
  }
  return T[sal][sbl];
}
signed main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> sa >> sb;
    cout << solve() << endl;
  }
}


Error: 0

Fixed submission:

#include <iostream>
#include <string>
#define int long long
// #define _DEBUG
using namespace std;
string sa, sb;
int T[1000][1000];
int solve() {
  int sal = sa.length(), sbl = sb.length();
  for (int a = 0; a <= sal; a++) {
    for (int b = 0; b <= sbl; b++) {
      if (a == 0 || b == 0) {
        T[a][b] = 0;
      } else if (sa[a - 1] == sb[b - 1]) {
        T[a][b] = 1 + T[a - 1][b - 1];
      } else {
        T[a][b] = max(T[a][b - 1], T[a - 1][b]);
      }
    }
  }
  return T[sal][sbl];
}
signed main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> sa >> sb;
    cout << solve() << endl;
  }
}
Problem p02235 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <list>
#include <stack>
#include <stdio.h>
#include <string>
#define MAX 1001
using namespace std;
int main() {
  int n;
  cin >> n;
  string x, y;
  int data[MAX][MAX] = {0, 0};
  for (int d = 0; d < n; d++) {
    cin >> x >> y;
    for (int i = 0; i < y.size(); i++) {
      for (int j = 0; j < x.size(); j++) {
        if (y[i] == x[j]) {
          data[j + 1][i + 1] = data[j][i] + 1;
        } else {
          data[j + 1][i + 1] = max(data[j + 1][i], data[j][i + 1]);
        }
      }
    }
    cout << data[x.size()][y.size()] << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <list>
#include <stack>
#include <stdio.h>
#include <string>
#define MAX 101
using namespace std;
int main() {
  int n;
  cin >> n;
  string x, y;
  int data[MAX][MAX] = {0, 0};
  for (int d = 0; d < n; d++) {
    cin >> x >> y;
    for (int i = 0; i < y.size(); i++) {
      for (int j = 0; j < x.size(); j++) {
        if (y[i] == x[j]) {
          data[j + 1][i + 1] = data[j][i] + 1;
        } else {
          data[j + 1][i + 1] = max(data[j + 1][i], data[j][i + 1]);
        }
      }
    }
    cout << data[x.size()][y.size()] << endl;
  }

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

#include <stdio.h>
#include <string.h>

#define M 1001

int n;

char x[M], y[M];

void lcs(void) {
  int i, j;
  int len_x, len_y, graph[M][M];

  len_x = strlen(x);
  len_y = strlen(y);
  for (i = 1; i < M; i++) {
    for (j = 1; j < M; j++) {
      graph[i][j] = 0;
    }
  }
  for (i = 1; i <= len_x; i++) {
    for (j = 1; j <= len_y; j++) {
      if (x[i - 1] == y[j - 1])
        graph[i][j] = graph[i - 1][j - 1] + 1;

      else {
        if (graph[i - 1][j] >= graph[i][j - 1])
          graph[i][j] = graph[i - 1][j];
        else
          graph[i][j] = graph[i][j - 1];
      }
    }
  }
  printf("%d\\n", graph[len_x][len_y]);
}

int main() {
  int i;

  scanf("%d", &n);

  for (i = 0; i < n; i++) {
    scanf("%s", x);
    scanf("%s", y);
    lcs();
  }
  return 0;
}

Error: -11

Fixed submission:

#include <stdio.h>
#include <string.h>

#define N 10000

int lcs(char *x, char *y) {
  int c[N + 1][N + 1];
  int m = strlen(x);
  int n = strlen(y);
  int maxl = 0;
  x = strcat(" ", x);
  y = strcat(" ", y);
  int i, j;
  for (i = 1; i <= m; i++) {
    c[i][0] = 0;
  }
  for (j = 0; j <= n; j++) {
    c[0][j] = 0;
  }

  for (i = 1; i <= m; i++) {
    for (j = 1; j <= n; j++) {
      if (x[i] == y[j]) {
        c[i][j] = c[i - 1][j - 1] + 1;
      } else {
        if (c[i - 1][j] > c[i][j - 1])
          c[i][j] = c[i - 1][j];
        else
          c[i][j] = c[i][j - 1];
      }
      if (maxl > c[i][j])
        maxl = maxl;
      else
        maxl = c[i][j];
    }
  }
  return maxl;
}

int main() {
  char s1[1000];
  char s2[1000];
  int n;
  scanf("%d", &n);
  int i;
  for (i = 0; i < n; i++) {
    scanf("%s%s", s1, s2);
    printf("%d\\n", lcs(s1, s2));
  }
  return 0;
}
Problem p02235 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n;
  cin >> n;
  for (int k = 0; k < n; k++) {
    string x, y;
    cin >> x >> y;
    int p = x.size();
    int q = y.size();
    int l[1001][1001] = {};
    for (int i = 1; i < p + 1; i++) {
      for (int j = 1; j < q + 1; j++) {
        if (x[i - 1] == y[j - 1]) {
          l[i][j] = 1 + l[i - 1][j - 1];
        } else {
          l[i][j] = max(l[i - 1][j], l[i][j - 1]);
        }
      }
    }
    cout << l[p][q] << endl;
  }
}

Error: 0

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  for (int k = 0; k < n; k++) {
    string x, y;
    cin >> x >> y;
    int p = x.size();
    int q = y.size();
    int l[1000][1000] = {};
    for (int i = 1; i < p + 1; i++) {
      for (int j = 1; j < q + 1; j++) {
        if (x[i - 1] == y[j - 1]) {
          l[i][j] = 1 + l[i - 1][j - 1];
        } else {
          l[i][j] = max(l[i - 1][j], l[i][j - 1]);
        }
      }
    }
    cout << l[p][q] << endl;
  }
}
Problem p02235 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define REP(i, n) for (long long i = 0; i < n; ++i)
#define FOR(i, a, b) for (long long i = a; i < b; ++i)
using namespace std;
int main() {
  int q;
  scanf("%d", &q);
  string x, y;
  REP(i, q) {
    cin >> x >> y;
    int dp[x.size() + 1][y.size() + 1];
    fill(dp[0], dp[x.size()], -1);
    REP(j, x.size() + 1) dp[j][0] = 0;
    REP(j, y.size() + 1) dp[0][j] = 0;
    FOR(j, 1, x.size() + 1) {
      FOR(k, 1, y.size() + 1) {
        if (x[j - 1] == y[k - 1])
          dp[j][k] = dp[j - 1][k - 1] + 1;
        else
          dp[j][k] = max(dp[j][k - 1], dp[j - 1][k]);
      }
    }
    printf("%d\\n", dp[x.size()][y.size()]);
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (long long i = 0; i < n; ++i)
#define FOR(i, a, b) for (long long i = a; i < b; ++i)
using namespace std;
int main() {
  int q;
  scanf("%d", &q);
  string x, y;
  REP(i, q) {
    cin >> x >> y;
    int dp[x.size() + 1][y.size() + 1];
    fill(dp[0], dp[y.size()], -1);
    REP(j, x.size() + 1) {
      REP(k, y.size() + 1) {
        if (j == 0 || k == 0)
          dp[j][k] = 0;
        else if (x[j] == y[k])
          dp[j][k] = dp[j - 1][k - 1] + 1;
        else
          dp[j][k] = max(dp[j][k - 1], dp[j - 1][k]);
      }
    }
    printf("%d\\n", dp[x.size()][y.size()]);
  }
  return 0;
}
Problem p02235 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>

int hantei(char x[], char y[]) {
  int xn = 0, yn = 0, flag = 0, i, j, max = 0;
  int c[1001][1001];

  while (flag == 0) {
    if (x[xn] != '\\0') {
      xn++;
    } else {
      flag = 1;
    }
  }

  flag = 0;

  while (flag == 0) {
    if (y[yn] != '\\0') {
      yn++;
    } else {
      flag = 1;
    }
  }
  for (i = 1; i < xn + 1; i++)
    c[i][0] = 0;
  for (j = 1; j < yn + 1; j++)
    c[0][j] = 0;

  for (i = 1; i < xn + 1; i++) {
    for (j = 1; j < yn + 1; j++) {
      if (x[i - 1] == y[j - 1]) {
        c[i][j] = c[i - 1][j - 1] + 1;
      } else if (c[i - 1][j] >= c[i][j - 1]) {
        c[i][j] = c[i - 1][j];
      } else {
        c[i][j] = c[i][j - 1];
      }
      if (max < c[i][j])
        max = c[i][j];
    }
  }

  return max;
}

int main(void) {
  char x[1000], y[1000];
  int q, i;

  scanf("%d", &q);

  for (i = 0; i < q; i++) {
    scanf("%s", x);
    scanf("%s", y);
    printf("%d\\n", hantei(x, y));
  }

  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>

int hantei(char x[], char y[]) {
  int xn = 0, yn = 0, flag = 0, i, j, max = 0;
  int c[101][101];

  while (flag == 0) {
    if (x[xn] != '\\0') {
      xn++;
    } else {
      flag = 1;
    }
  }

  flag = 0;

  while (flag == 0) {
    if (y[yn] != '\\0') {
      yn++;
    } else {
      flag = 1;
    }
  }
  for (i = 1; i < xn + 1; i++)
    c[i][0] = 0;
  for (j = 1; j < yn + 1; j++)
    c[0][j] = 0;

  for (i = 1; i < xn + 1; i++) {
    for (j = 1; j < yn + 1; j++) {
      if (x[i - 1] == y[j - 1]) {
        c[i][j] = c[i - 1][j - 1] + 1;
      } else if (c[i - 1][j] >= c[i][j - 1]) {
        c[i][j] = c[i - 1][j];
      } else {
        c[i][j] = c[i][j - 1];
      }
      if (max < c[i][j])
        max = c[i][j];
    }
  }

  return max;
}

int main(void) {
  char x[1000], y[1000];
  int q, i;

  scanf("%d", &q);

  for (i = 0; i < q; i++) {
    scanf("%s", x);
    scanf("%s", y);
    printf("%d\\n", hantei(x, y));
  }

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

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

#define MAX 1000

int c[MAX + 1][MAX + 1];

int lcs(string A, string B) {
  int a = A.length();
  int b = B.length();
  int maxl = 0;

  A = ' ' + A;
  B = ' ' + B;
  // 配列を作る
  for (int i = 0; i < a; i++) {
    c[i][0] = 0;
  }
  for (int j = 0; j < b; j++) {
    c[0][j] = 0;
  }
  for (int i = 1; i <= a; i++) {
    for (int j = 1; j <= b; j++) {
      if (A[i] == B[j]) {
        c[i][j] = c[i - 1][j - 1] + 1;
      } else {
        c[i][j] = max(c[i - 1][j], c[i][j - 1]);
      }
      maxl = max(maxl, c[i][j]);
    }
  }

  return maxl;
}

int main() {
  string s1, s2;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s1 >> s2;
    cout << lcs(s1, s2) << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

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

#define MAX 1000

int c[MAX][MAX];

int lcs(string A, string B) {
  int a = A.length();
  int b = B.length();
  int maxl = 0;

  A = ' ' + A;
  B = ' ' + B;
  // 配列を作る
  for (int i = 0; i < a; i++) {
    c[i][0] = 0;
  }
  for (int j = 0; j < b; j++) {
    c[0][j] = 0;
  }
  for (int i = 1; i <= a; i++) {
    for (int j = 1; j <= b; j++) {
      if (A[i] == B[j]) {
        c[i][j] = c[i - 1][j - 1] + 1;
      } else {
        c[i][j] = max(c[i - 1][j], c[i][j - 1]);
      }
      maxl = max(maxl, c[i][j]);
    }
  }

  return maxl;
}

int main() {
  string s1, s2;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s1 >> s2;
    cout << lcs(s1, s2) << endl;
  }
  return 0;
}
Problem p02235 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
static const int N = 1000;

int lcs(string X, string Y) {
  int **c = (int **)malloc(sizeof(int *) * 1001);
  for (int i = 0; i < 1001; i++) {
    c[i] = (int *)malloc(sizeof(int) * 1001);
  }
  int m = X.size();
  int n = Y.size();
  int maxl = 0;
  X = ' ' + X;
  Y = ' ' + Y;
  c[0][0] = 0;
  for (int i = 1; i <= m; i++)
    c[i][0] = 0;
  for (int j = 1; j <= n; j++)
    c[0][j] = 0;

  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
      if (X[i] == Y[j]) {
        c[i][j] = c[i - 1][j - 1] + 1;
      } else {
        c[i][j] = max(c[i - 1][j], c[i][j - 1]);
      }
      maxl = max(maxl, c[i][j]);
    }
  }
  for (int i = 0; i < 1001; i++) {
    free(c[i]);
  }
  free(c);
  return maxl;
}

int main() {
  string s1, s2;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s1 >> s2;
    cout << lcs(s1, s2) << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
static const int N = 1000;

int lcs(string X, string Y) {
  int **c = (int **)malloc(sizeof(int *) * 1001);
  for (int i = 0; i < 1001; i++) {
    c[i] = (int *)malloc(sizeof(int) * 1001);
  }
  int m = X.size();
  int n = Y.size();
  int maxl = 0;
  X = ' ' + X;
  Y = ' ' + Y;
  c[0][0] = 0;
  for (int i = 1; i <= m; i++)
    c[i][0] = 0;
  for (int j = 1; j <= n; j++)
    c[0][j] = 0;

  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
      if (X[i] == Y[j]) {
        c[i][j] = c[i - 1][j - 1] + 1;
      } else {
        c[i][j] = max(c[i - 1][j], c[i][j - 1]);
      }
      maxl = max(maxl, c[i][j]);
    }
  }
  return maxl;
}

int main() {
  string s1, s2;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s1 >> s2;
    cout << lcs(s1, s2) << endl;
  }
  return 0;
}
Problem p02235 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
#define FOR(i, b, e) for (int i = b; i < e; i++)
#define REP(i, n) FOR(i, 0, n)
using namespace std;

int dp[1001][1001];
string s1, s2;

int lcs(int n1, int n2) {
  if (dp[n1][n2] != -1)
    return dp[n1][n2];

  dp[n1][n2] = (s1[n1 - 1] == s2[n2 - 1])
                   ? (1 + lcs(n1 - 1, n2 - 1))
                   : max(lcs(n1 - 1, n2), lcs(n1, n2 - 1));
  return dp[n1][n2];
}

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

  int q;
  cin >> q;
  REP(i, q) {
    REP(i, 1001) {
      REP(j, 1001) { dp[i][j] = -1; }
    }
    REP(i, 1001) dp[i][0] = 0;
    REP(i, 1001) dp[0][i] = 0;

    cin >> s1;
    cin >> s2;

    cout << lcs(s1.length(), s2.length()) << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
#define FOR(i, b, e) for (int i = b; i < e; i++)
#define REP(i, n) FOR(i, 0, n)
using namespace std;

string s1, s2;

int lcs(int n1, int n2) {
  if (n1 < 0 || n2 < 0)
    return 0;

  if (s1[n1] == s2[n2])
    return 1 + lcs(n1 - 1, n2 - 1);
  else
    return max(lcs(n1, n2 - 1), lcs(n1 - 1, n2));
}

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

  int q;
  cin >> q;
  REP(i, q) {
    cin >> s1;
    cin >> s2;

    cout << lcs(s1.length() - 1, s2.length() - 1) << endl;
  }
}
Problem p02257 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;

#define MAX_N (1e8) + 1

int main(void) {
  //?????????????????????
  vector<bool> eratos(MAX_N, true);
  eratos[0] = eratos[1] = false;
  for (int i = 2; i * i < MAX_N; i++)
    if (eratos[i])
      for (int j = 2; i * j < MAX_N; j++)
        eratos[i * j] = false;

  //?????¨?????????
  int n, input, ans = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> input;
    if (eratos[input])
      ++ans;
  }

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

Error: MLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

#define MAX_N (1e9) + 1

int main(void) {
  //?????????????????????
  vector<bool> eratos(MAX_N, true);
  eratos[0] = eratos[1] = false;
  for (int i = 2; i * i < MAX_N; i++)
    if (eratos[i])
      for (int j = 2; i * j < MAX_N; j++)
        eratos[i * j] = false;

  //?????¨?????????
  int n, input, ans = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> input;
    if (eratos[input])
      ++ans;
  }

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