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

#include <cstdio>
#include <iostream>

using namespace std;

int m[1500][1500];
int main() {
  int n;
  cin >> n;
  char A[1500];
  char B[1500];
  for (int i = 0; i < n; i++) {
    cin >> A;
    cin >> B;
    int x;
    int y;
    for (x = 0; A[x] != '\\0'; x++) {
      for (y = 0; B[y] != '\\0'; y++) {
        if (A[x] == B[y]) {
          m[x + 1][y + 1] = m[x][y] + 1;
        } else {
          if (m[x][y + 1] > m[x + 1][y]) {
            m[x + 1][y + 1] = m[x][y + 1];
          } else
            m[x + 1][y + 1] = m[x + 1][y];
        }
      }
    }
    cout << m[x][y] << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>

using namespace std;

int m[1000][10000];
int main() {
  int n;
  cin >> n;
  char A[1001];
  char B[1001];
  for (int i = 0; i < n; i++) {
    cin >> A;
    cin >> B;
    int x;
    int y;
    for (x = 0; A[x] != '\\0'; x++) {
      for (y = 0; B[y] != '\\0'; y++) {
        if (A[x] == B[y]) {
          m[x + 1][y + 1] = m[x][y] + 1;
        } else {
          if (m[x][y + 1] > m[x + 1][y]) {
            m[x + 1][y + 1] = m[x][y + 1];
          } else
            m[x + 1][y + 1] = m[x + 1][y];
        }
      }
    }
    cout << m[x][y] << endl;
  }

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

// ALDS1_10C
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

static const int N = 1001;

int lcs(string X, string Y) {
  vector<vector<int>> c(N, vector<int>(N));
  int m = X.size();
  int n = Y.size();
  int maxl = 0;
  X = ' ' + X;
  Y = ' ' + Y;
  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;
}

Error: 0

Fixed submission:

// ALDS1_10C
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

static const int N = 1000;

int lcs(string X, string Y) {
  vector<vector<int>> c(N, vector<int>(N));
  int m = X.size();
  int n = Y.size();
  int maxl = 0;
  X = ' ' + X;
  Y = ' ' + Y;
  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 <cstdio>
#include <cstring>
#include <iostream>
#define _A_ true
using namespace std;

int main() {
  int n, AC = 0, strA, strB;
  int dp[1010][1010];
  char *A = new char[1500];
  char *B = new char[1500];

  scanf("%d", &n);
  while (_A_) {
    if (AC == n)
      break;
    scanf("%s", A);
    scanf("%s", B);
    for (int i = 0; i < 1010; i++)
      for (int j = 0; j < 1010; j++)
        dp[i][j] = 0;
    strA = strlen(A);
    strB = strlen(B);

    for (int i = 0; i < strA; i++) {
      for (int j = 0; j < strB; j++) {
        if (A[i] == B[j]) {
          dp[i + 1][j + 1] = dp[i][j] + 1;
        } else {
          if (dp[i + 1][j] > dp[i][j + 1]) {
            dp[i + 1][j + 1] = dp[i + 1][j];
          } else {
            dp[i + 1][j + 1] = dp[i][j + 1];
          }
        }
      }
    }
    printf("%d\\n", dp[strA][strB]);
    AC++;
  }
  delete[] A;
  delete[] B;

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
#define _A_ true
using namespace std;

int main() {
  int n, AC = 0;
  int dp[1010][1010];
  char A[1500], B[1500];
  scanf("%d", &n);
  while (_A_) {
    if (AC == n)
      break;
    scanf("%s", A);
    scanf("%s", B);
    for (int i = 0; i < 1010; i++)
      for (int j = 0; j < 1010; j++)
        dp[i][j] = 0;

    for (int i = 0; i < strlen(A); i++) {
      for (int j = 0; j < strlen(B); j++) {
        if (A[i] == B[j]) {
          dp[i + 1][j + 1] = dp[i][j] + 1;
        } else {
          if (dp[i + 1][j] > dp[i][j + 1]) {
            dp[i + 1][j + 1] = dp[i + 1][j];
          } else {
            dp[i + 1][j + 1] = dp[i][j + 1];
          }
        }
      }
    }
    printf("%d\\n", dp[strlen(A)][strlen(B)]);
    AC++;
  }
  return 0;
}
Problem p02235 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <map>
#include <string>
#include <vector>

using std::cerr;
using std::cin;
using std::cout;
using std::endl;

// 最長共通部分列 ... len(s) == m, len(t) == n とすると, O(mn)
int lcs(std::string s, std::string t) {
  std::vector<std::vector<int>> dp(s.size() + 1,
                                   std::vector<int>(t.size() + 1, 0));

  for (int i = 1; i <= s.size(); ++i) {
    for (int j = 1; j <= t.size(); ++j) {
      if (s[i - 1] == t[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      } else if (dp[i - 1][j] >= dp[i][j - 1]) {
        dp[i][j] = dp[i - 1][j];
      } else {
        dp[i][j] = dp[i][j - 1];
      }
    }
  }

  return dp[s.size()][t.size()];
}

int main() {
  int q;
  std::string s, t;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    cin >> s >> t;
    cout << lcs(s, t) << endl;
  }

  return 0;
}


Error: -11

Fixed submission:

#include <iostream>
#include <map>
#include <string>
#include <vector>

using std::cerr;
using std::cin;
using std::cout;
using std::endl;

// 最長共通部分列 ... len(s) == m, len(t) == n とすると, O(mn)
int lcs(std::string s, std::string t) {
  std::vector<std::vector<int>> dp(s.size() + 1,
                                   std::vector<int>(t.size() + 1, 0));

  for (int i = 0; i < s.size(); ++i) {
    for (int j = 0; j < t.size(); ++j) {
      if (s[i] == t[j]) {
        dp[i][j] = (i == 0 ? (j == 0 ? 1 : 0) : 0);
      } else if (i >= 0 && j >= 0 && dp[i - 1][j] >= dp[j][i - 1]) {
        dp[i][j] = dp[i - 1][j];
      } else if (i >= 0 && j >= 0) {
        dp[i][j] = dp[i][j - 1];
      }
    }
  }

  return dp[s.size()][t.size()];
}

int main() {
  int q;
  std::string s, t;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    cin >> s >> t;
    cout << lcs(s, t) << endl;
  }

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

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

using namespace std;

// I can't understand,so I imitated yutaka watabe's book.
// I think this problem has 5 star rank difficulty.

void calc(char X[], char Y[]) {
  int X_length = 0, Y_length = 0;
  int table[1001][1001];
  for (int i = 0; i < 1001; i++) {
    for (int k = 0; k < 1001; k++)
      table[i][k] = 0;
  }
  for (int i = 0; X[i] != '\\0'; i++)
    X_length++;
  for (int i = 0; Y[i] != '\\0'; i++)
    Y_length++;
  int max_succeed = 0;
  for (int i = 0; i < X_length; i++) {
    for (int k = 0; k < Y_length; k++) {
      if (X[i] == Y[k]) {
        table[i + 1][k + 1] = table[i][k] + 1;
      } else {
        table[i + 1][k + 1] = max(table[i + 1][k], table[i][k + 1]);
      }
      max_succeed = max(max_succeed, table[i + 1][k + 1]);
    }
  }
  printf("%d\\n", max_succeed);
}

int main() {
  int q;
  char X[1001], Y[1001];
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%s %s", X, Y);
    calc(X, Y);
  }
  return 0;
}

Error: MLE

Fixed submission:

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

using namespace std;

// I can't understand,so I imitated yutaka watabe's book.
// I think this problem has 5 star rank difficulty.

void calc(char X[], char Y[]) {
  int X_length = 0, Y_length = 0;
  int **table = new int *[1001];
  for (int i = 0; i < 1001; i++) {
    table[i] = new int[1001];
    for (int k = 0; k < 1001; k++)
      table[i][k] = 0;
  }
  for (int i = 0; X[i] != '\\0'; i++)
    X_length++;
  for (int i = 0; Y[i] != '\\0'; i++)
    Y_length++;
  int max_succeed = 0;
  for (int i = 0; i < X_length; i++) {
    for (int k = 0; k < Y_length; k++) {
      if (X[i] == Y[k]) {
        table[i + 1][k + 1] = table[i][k] + 1;
      } else {
        table[i + 1][k + 1] = max(table[i + 1][k], table[i][k + 1]);
      }
      max_succeed = max(max_succeed, table[i + 1][k + 1]);
    }
  }
  printf("%d\\n", max_succeed);
}

int main() {
  int q;
  char X[1001], Y[1001];
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%s %s", X, Y);
    calc(X, Y);
  }
  return 0;
}
Problem p02235 (C++) — buggy submission, verdict: Runtime Error

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

static const int N = 1000;
int dp[N + 1][N + 1];

int lcs(string x, string y) {
  int m = x.length();
  int n = y.length();
  x = ' ' + x;
  y = ' ' + y;

  dp[0][0] = 0;
  for (int i = 1; i <= m; i++)
    dp[i][0] = 0;
  for (int j = 1; j <= n; j++)
    dp[0][j] = 0;

  int res = 0;
  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
      if (x[i] == y[j])
        dp[i][j] = dp[i - 1][j - 1] + 1;
      else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }

  return dp[m][n];
}

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


Error: 0

Fixed submission:

#include <iostream>

using namespace std;

int dp[1001][1001];

int main() {
  int q, m, n;
  cin >> q;

  while (q-- > 0) {
    string x, y;
    cin >> x >> y;
    x = ' ' + x;
    y = ' ' + y;
    m = x.length();
    n = y.length();
    for (int i = 1; i <= m; i++)
      dp[i][0];
    for (int i = 1; i <= n; i++)
      dp[0][i];

    int res = 0;
    for (int i = 1; i <= m; i++) {
      for (int j = 1; j <= n; j++) {
        if (x[i] == y[j])
          dp[i][j] = dp[i - 1][j - 1] + 1;
        else
          dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }

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

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

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

int dp[1001][1001];
string s, t;
int n, m, a;

int main() {
  cin >> a;
  for (int b = 0; b < a; b++) {
    cin >> s >> t;
    for (int i = 0; i <= 1000; i++)
      for (int j = 0; j <= 1000; j++)
        dp[i][j] = 0;
    m = s.size();
    n = t.size();
    for (int i = 1; i <= m; i++) {
      for (int j = 1; j <= n; 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]);
        }
      }
    }
    cout << dp[m][n] << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#include <string>

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

using namespace std;

int q;
string s, t;
int dp[151][151];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> q;
  rep(k, q) {
    cin >> s >> t;
    rep(i, 101) rep(j, 101) dp[i][j] = 0;
    rep(i, s.size()) {
      rep(j, t.size()) {
        if (s[i] == t[j]) {
          dp[i + 1][j + 1] = dp[i][j] + 1;
        } else {
          dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]);
        }
      }
    }
    cout << dp[s.size()][t.size()] << "\\n";
  }
  return 0;
}
Problem p02235 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

string str1, str2;
int dp[1001][1001];

void solve() {
  REP(i, str1.size())
  if (str1[i] == str2[str2.size() - 1])
    dp[i][str2.size() - 1] = 1;
  else
    dp[i][str2.size() - 1] = 0;
  for (int i = str2.size() - 2; i >= 0; i--)
    if (str1[str1.size() - 1] == str2[i])
      dp[str1.size() - 1][i] = 1;
    else
      dp[str1.size() - 1][i] = dp[str1.size() - 1][i + 1];
  for (int i = str1.size() - 2; i >= 0; i--) {
    for (int j = str2.size() - 2; j >= 0; j--) {
      if (str1[i] == str2[j])
        dp[i][j] = dp[i + 1][j + 1] + 1;
      else
        dp[i][j] = max(dp[i + 1][j], dp[i][j + 1]);
    }
  }
}

int main() {
  int q, m;
  cin >> q;
  cin.ignore();
  REP(i, q) {
    getline(cin, str1);
    getline(cin, str2);
    fill(&dp[0][0], &dp[1001][0], -1);
    solve();
    m = dp[0][0];
    swap(str1, str2);
    fill(&dp[0][0], &dp[1001][0], -1);
    solve();
    cout << ((m > dp[0][0]) ? m : dp[0][0]) << endl;
  }
}

Error: TLE

Fixed submission:

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

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

string str1, str2;
int dp[1001][1001];
int k;

int solve(int i, int j) {
  if (dp[i][j] > 0)
    return dp[i][j];
  if (i >= str1.size() || j >= str2.size()) {
    if (i < str1.size()) {
      return solve(i + 1, k);
    } else {
      return 0;
    }
  }
  if (str1[i] == str2[j]) {
    k = j + 1;
    int n, m = 0;
    for (int l = i + 1; l < str1.size(); l++) {
      n = solve(l, j + 1);
      m = (n > m) ? n : m;
    }
    dp[i][j] = m + 1;
    return dp[i][j];
  } else {
    return solve(i, j + 1);
  }
}

int main() {
  int q, n, ans = 0;
  cin >> q;
  cin.ignore();
  REP(i, q) {
    getline(cin, str1);
    getline(cin, str2);
    if (str1.size() < str2.size())
      swap(str1, str2);
    fill(&dp[0][0], &dp[1001][0], -1);
    for (int j = str1.size() - 1; j >= 0; j--) {
      n = solve(j, 0);
      ans = (n > ans) ? n : ans;
    }
    cout << ans << endl;
    ans = 0;
  }
}
Problem p02235 (C++) — buggy submission, verdict: Time Limit Exceeded

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

string X;
string Y;

int calc(int x, int y, vector<vector<int>> &Memo) {
  if (X[x] == '\\0' || Y[y] == '\\0')
    return 0;
  if (Memo[x][y] != -1)
    return Memo[x][y];

  int unuse = calc(x + 1, y, Memo);
  int use = 0;
  int y_target = Y.find(X[x], y);
  if (y_target == string::npos) {
    use = 0;
  } else {
    use = calc(x + 1, y_target + 1, Memo) + 1;
  }
  return Memo[x][y] = max(unuse, use);
}

int main() {
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> X;
    cin >> Y;
    vector<int> tmp(Y.size(), -1);
    vector<vector<int>> Memo(X.size(), tmp);
    cout << calc(0, 0, Memo) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

string X;
string Y;

int calc(int x, int y) {
  if (X[x] == '\\0')
    return 0;
  if (y == string::npos)
    return 0;

  int unuse = calc(x + 1, y);
  int use = 0;
  int y_target = Y.find(X[x], y);
  if (y_target == string::npos) {
    use = 0;
  } else {
    use = calc(x + 1, y_target + 1) + 1;
  }
  return max(unuse, use);
}

int main() {
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> X;
    cin >> Y;
    cout << calc(0, 0) << endl;
  }

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

#include <bits/stdc++.h>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()
const int INF = 1e9;

using namespace std;
int dp[1005][1005];

int main() {
  int n;
  cin >> n;
  REP(k, n) {
    REP(i, 1005) REP(j, 1005) dp[i][j] = 0;
    string a, b;
    cin >> a >> b;
    REP(i, a.length()) {
      string q = a.substr(i, 1);
      REP(j, b.length()) {
        string t = b.substr(j, 1);
        if (q == t) {
          if (i == 0 || j == 0)
            dp[i][j] = 1;
          else
            dp[i][j] = (dp[i - 1][j] > dp[i][j - 1])
                           ? max(dp[i - 1][j - 1] + 1, dp[i - 1][j])
                           : max(dp[i - 1][j - 1] + 1, dp[i][j - 1]);
        } else if (i != 0 && j != 0)
          dp[i][j] =
              (dp[i - 1][j] > dp[i][j - 1]) ? dp[i - 1][j] : dp[i][j - 1];
        else if (i != 0)
          dp[i][j] = dp[i - 1][j];
        else
          dp[i][j] = dp[i][j - 1];
      }
    }
    cout << dp[a.length() - 1][b.length() - 1] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <tuple>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()
const int INF = 1e9;

using namespace std;
int dp[1005][1005];

void dfs(int i, int j, string q, string b) {
  int x = j;
  string tar = b.substr(x, 1);
  while (x < b.length() && q != tar) {
    x++;
    tar = b.substr(x, 1);
  }
  if (i == 0) {
    while (x < b.length()) {
      x++;
      dp[i][x] = 1;
    }
    return;
  }
  while (x < b.length()) {
    x++;
    dp[i][x] = max(dp[i - 1][j] + 1, dp[i][x]);
  }
  dp[i][j] = max(dp[i][j], dp[i - 1][j]);
  return;
}

int main() {
  int n;
  cin >> n;
  REP(k, n) {
    REP(i, 1005) REP(j, 1005) dp[i][j] = 0;
    string a, b;
    cin >> a >> b;
    REP(i, a.length()) {
      string q = a.substr(i, 1);
      REP(j, b.length() + 1) dfs(i, j, q, b);
    }
    cout << dp[a.length() - 1][b.length()] << endl;
  }
  return 0;
}
Problem p02235 (C++) — buggy submission, verdict: Runtime Error

#include <cstring>
#include <iostream>

using namespace std;

/**
 *  lcs(m,n) =
 *  if x[m] == y[n] lcs[m-1,n-1]+1
 *  max(lcs[m-1,n],lcs[m,n-1])
 *
 */

string x, y;

const int N = 1000;

int c[N][N];

int lcs(int m, int n) {
  if (m == -1 || n == -1)
    return 0;
  if (x[m] == y[n])
    return lcs(m - 1, n - 1) + 1;
  int &cc = c[m][n];
  if (cc != -1)
    return cc;
  int xx = lcs(m - 1, n);
  int yy = lcs(m, n - 1);
  cc = xx > yy ? xx : yy;
  return cc;
}

int main() {
  int n = 0;
  x.reserve(N);
  y.reserve(N);
  cin >> n;
  for (int i = 0; i < n; i++) {
    memset(c, -1, sizeof(c));
    cin >> x;
    cin >> y;
    cout << lcs(x.size() - 1, y.size() - 1) << endl;
    x.clear();
    y.clear();
  }
}

Error: WA

Fixed submission:

#include <cstring>
#include <iostream>

using namespace std;

/**
 *  lcs(m,n) =
 *  if x[m] == y[n] lcs[m-1,n-1]+1
 *  max(lcs[m-1,n],lcs[m,n-1])
 *
 */

string x, y;

const int q = 150;

int c[q][q];

int lcs(int m, int n) {
  if (m == -1 || n == -1)
    return 0;
  if (x[m] == y[n])
    return lcs(m - 1, n - 1) + 1;
  int &cc = c[m][n];
  if (cc != -1)
    return cc;
  int xx = lcs(m - 1, n);
  int yy = lcs(m, n - 1);
  cc = xx > yy ? xx : yy;
  return cc;
}

int main() {
  int n = 0;
  x.resize(q);
  y.resize(q);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x;
    cin >> y;
    cout << lcs(x.size() - 1, y.size() - 1) << endl;
    x.clear();
    y.clear();

    memset(c, -1, sizeof(c));
  }
}
Problem p02235 (C++) — buggy submission, verdict: Runtime Error

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

const int MAX_LENGTH = 1024;

char A[MAX_LENGTH + 1];
char B[MAX_LENGTH + 1];

int tu[MAX_LENGTH][MAX_LENGTH];

void lcs(int, int);

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> A >> B;
    int lA = strlen(A), lB = strlen(B);
    lcs(lA, lB);
    cout << tu[lA][lB] << endl;
  }
}

void lcs(int lA, int lB) {
  for (int i = 0; i < lA; ++i)
    tu[0][i] = 0;
  for (int i = 0; i < lB; ++i)
    tu[i][0] = 0;

  for (int i = 1; i <= lA; i++) {
    for (int j = 1; j <= lB; j++) {
      if (A[i - 1] == B[j - 1])
        tu[i][j] = tu[i - 1][j - 1] + 1;
      else
        tu[i][j] = max(tu[i][j - 1], tu[i - 1][j]);
    }
  }
}

Error: 0

Fixed submission:

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

const int MAX_LENGTH = 256;

char A[MAX_LENGTH + 1];
char B[MAX_LENGTH + 1];

int tu[MAX_LENGTH][MAX_LENGTH];

void lcs(int, int);

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> A >> B;
    int lA = strlen(A), lB = strlen(B);
    lcs(lA, lB);
    cout << tu[lA][lB] << endl;
  }
}

void lcs(int lA, int lB) {
  for (int i = 0; i < lA; ++i)
    tu[0][i] = 0;
  for (int i = 0; i < lB; ++i)
    tu[i][0] = 0;

  for (int i = 1; i <= lA; i++) {
    for (int j = 1; j <= lB; j++) {
      if (A[i - 1] == B[j - 1])
        tu[i][j] = tu[i - 1][j - 1] + 1;
      else
        tu[i][j] = max(tu[i][j - 1], tu[i - 1][j]);
    }
  }
}
Problem p02235 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <array>
#include <cstdio>
#include <iostream>
#include <list>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define _USE_MATH_DEFINES

#include <map>
#include <math.h>

#define SENTINEL 1000000001

#define min(a, b) (a) > (b) ? (b) : (a)
#define max(a, b) (a) > (b) ? (a) : (b)

using namespace std;

int LCS[1010][1010];

void solve(string x, string y) {
  for (int i = 0; i != x.size(); i++) {
    for (int j = 0; j != y.size(); j++) {
      if (x[i] == y[j]) {
        LCS[i + 1][j + 1] = LCS[i][j] + 1;
      } else {
        LCS[i + 1][j + 1] = max(LCS[i][j + 1], LCS[i + 1][j]);
      }
    }
  }

  cout << LCS[x.size()][y.size()] << endl;
}

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

  for (int i = 0; i < 1000; i++) {
    LCS[0][i] = 0;
    LCS[i][0] = 0;
  }

  for (int i = 0; i < q; i++) {
    string x, y;
    cin >> x >> y;
    solve(x, y);
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <array>
#include <cstdio>
#include <iostream>
#include <list>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define _USE_MATH_DEFINES

#include <map>
#include <math.h>

#define SENTINEL 1000000001

#define min(a, b) (a) > (b) ? (b) : (a)
#define max(a, b) (a) > (b) ? (a) : (b)

using namespace std;

int LCS[1000][1000];

void solve(string x, string y) {
  for (int i = 0; i != x.size(); i++) {
    for (int j = 0; j != y.size(); j++) {
      if (x[i] == y[j]) {
        LCS[i + 1][j + 1] = LCS[i][j] + 1;
      } else {
        LCS[i + 1][j + 1] = max(LCS[i][j + 1], LCS[i + 1][j]);
      }
    }
  }

  cout << LCS[x.size()][y.size()] << endl;
}

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

  for (int i = 0; i < 1000; i++) {
    LCS[0][i] = 0;
    LCS[i][0] = 0;
  }

  for (int i = 0; i < q; i++) {
    string x, y;
    cin >> x >> y;
    solve(x, y);
  }
  return 0;
}
Problem p02235 (C++) — buggy submission, verdict: Runtime Error

#include <cstring>
#include <iostream>
#include <string>
#define repi(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, a) repi(i, 0, a)
#define clr(a, v) memset((a), (v), sizeof(a))

int q;
int dp[1001][1001];
std::string s, t;

int rec(int i, int j) {
  int &ret = dp[i][j];

  if (~ret)
    return ret;

  if (i == s.size() || j == t.size())
    return 0;

  ret = 0;

  if (s[i] == t[j])
    ret = std::max(ret, rec(i + 1, j + 1) + 1);

  ret = std::max(ret, std::max(rec(i + 1, j), rec(i, j + 1)));

  return ret;
}

int main() {
  std::cin >> q;
  while (q--) {
    std::cin >> s >> t;

    clr(dp, -1);

    std::cout << rec(0, 0) << std::endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <cstring>
#include <iostream>
#include <string>
#define repi(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, a) repi(i, 0, a)
#define clr(a, v) memset((a), (v), sizeof(a))

int q;
int dp[1000][1000];
std::string s, t;

int rec(int i, int j) {
  int &ret = dp[i][j];

  if (~ret)
    return ret;

  if (i == s.size() || j == t.size())
    return 0;

  ret = 0;

  if (s[i] == t[j])
    ret = std::max(ret, rec(i + 1, j + 1) + 1);

  ret = std::max(ret, std::max(rec(i + 1, j), rec(i, j + 1)));

  return ret;
}

int main() {
  std::cin >> q;
  while (q--) {
    std::cin >> s >> t;

    clr(dp, -1);

    std::cout << rec(0, 0) << std::endl;
  }

  return 0;
}