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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
#define MAX_N 1001

int solve(std::string s, std::string t) {
  int dp[MAX_N][MAX_N] = {0};
  int n, m;
  n = s.size();
  m = 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] = std::max(dp[i][j + 1], dp[i + 1][j]);
    }
  }
  return dp[n][m];
}

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

Error: 0

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
#define MAX_N 1000

int solve(std::string s, std::string t) {
  int dp[MAX_N][MAX_N] = {0};
  int n, m;
  n = s.size();
  m = 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] = std::max(dp[i][j + 1], dp[i + 1][j]);
    }
  }
  return dp[n][m];
}

int main() {
  int q;
  std::cin >> q;
  for (int i = 0; i < q; i++) {
    std::string s, t;
    std::cin >> s >> t;
    std::cout << solve(s, t) << std::endl;
  }
  return 0;
}
Problem p02235 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
#include <string>
#define MAX 1001

using namespace std;

string a, b;
int lcs_memo[1001][1001];

int lcs(int i, int j) {
  if (i == 0 || j == 0)
    return 0;
  if (lcs_memo[i][j] != -1)
    return lcs_memo[i][j];
  if (a[i] == b[j]) {
    lcs_memo[i][j] = lcs(i - 1, j - 1) + 1;
  } else {
    lcs_memo[i][j] = max(lcs(i, j - 1), lcs(i - 1, j));
  }
  return lcs_memo[i][j];
}

void clear() {
  for (int i = 0; i < 1001; i++) {
    for (int j = 0; j < 1001; j++) {
      lcs_memo[i][j] = -1;
    }
  }
}

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

  for (int i = 0; i < n; i++) {
    clear();
    cin >> a >> b;
    a = ' ' + a;
    b = ' ' + b;
    cout << lcs(a.size() - 1, b.size() - 1) << endl;
  }
}

Error: 0

Fixed submission:

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

using namespace std;

string a, b;
int lcs_memo[1000][1000];

int lcs(int i, int j) {
  if (i == 0 || j == 0)
    return 0;
  if (lcs_memo[i][j] != -1)
    return lcs_memo[i][j];
  if (a[i] == b[j]) {
    lcs_memo[i][j] = lcs(i - 1, j - 1) + 1;
  } else {
    lcs_memo[i][j] = max(lcs(i, j - 1), lcs(i - 1, j));
  }
  return lcs_memo[i][j];
}

void clear() {
  for (int i = 0; i < 1000; i++) {
    for (int j = 0; j < 1000; j++) {
      lcs_memo[i][j] = -1;
    }
  }
}

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

  for (int i = 0; i < n; i++) {
    clear();
    cin >> a >> b;
    a = ' ' + a;
    b = ' ' + b;
    cout << lcs(a.size() - 1, b.size() - 1) << endl;
  }
}
Problem p02235 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

int lcs(string x, string y);

int main() {
  int n;
  cin >> n;
  string x, y;
  for (int i = 0; i < n; i++) {
    cin >> x >> y;
    cout << lcs(x, y) << endl;
  }
  cin.get();
  cin.get();
}

int lcs(string x, string y) {
  int m = x.size(), n = y.size();
  x = ' ' + x;
  y = ' ' + y;
  int **c;
  c = new int *[m + 1];
  for (int i = 0; i <= m; i++)
    c[i] = new int[n + 1];
  for (int i = 0; i <= m; i++)
    c[i][0] = 0;
  for (int j = 0; j <= n; j++)
    c[0][j] = 0;
  int maxl = 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 <= m; i++)
    delete[] c[i];
  delete[] c;
  return maxl;
}

Error: MLE

Fixed submission:

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

int lcs(string x, string y);

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

int lcs(string x, string y) {
  int m = x.size(), n = y.size();
  x = ' ' + x;
  y = ' ' + y;
  int **c;
  c = new int *[m + 1];
  for (int i = 0; i <= m; i++)
    c[i] = new int[n + 1];
  for (int i = 0; i <= m; i++)
    c[i][0] = 0;
  for (int j = 0; 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]);
      if (i == m && j == n)
        return c[i][j];
    }
  }

  for (int i = 0; i <= m; i++)
    delete[] c[i];
  delete[] c;
}
Problem p02235 (C++) — buggy submission, verdict: Runtime Error

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

int Max(int &a, int b) { return a = max(a, b); }

int LCS(string &a, string &b) {
  int n = a.size();
  int m = b.size();
  vector<vector<int>> dp(n + 1, vector<int>(m + 1));

  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= m; j++) {
      if (i < n && j < m && a[i] == b[j])
        Max(dp[i + 1][j + 1], dp[i][j] + 1);
      if (i < n)
        Max(dp[i + 1][j], dp[i][j]);
      if (j < m)
        Max(dp[i][j + 1], dp[i][j]);
    }
  return dp[n][m];
}

int main() {
  int q;
  cin >> q;
  while (q--) {
    string a, b;
    cin >> a >> b;
    cout << LCS(a, b) << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

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

int Max(int &a, int b) { return a = max(a, b); }

int LCS(string &a, string &b) {
  int n = a.size();
  int m = b.size();
  vector<vector<int>> dp(n + 1, vector<int>(m + 1));

  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= m; j++) {
      if (i < n && j < m && a[i] == b[j])
        Max(dp[i + 1][j + 1], dp[i][j] + 1);
      Max(dp[i + 1][j], dp[i][j]);
      Max(dp[i][j + 1], dp[i][j]);
    }
  return dp[n][m];
}

int main() {
  int q;
  cin >> q;
  while (q--) {
    string a, b;
    cin >> a >> b;
    cout << LCS(a, b) << endl;
  }
  return 0;
}
Problem p02235 (C++) — buggy submission, verdict: Runtime Error

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

int LCS(string p, string q) {
  vector<vector<int>> f(p.size() + 1, vector<int>(q.size() + 1, 0));
  int t;
  for (int i = 1; i <= p.size(); i++) {
    for (int j = 1; j <= q.size(); j++) {
      if (p[i - 1] == q[j - 1])
        f[i][j] = f[i - 1][j - 1] + 1;
      else if (f[i][j - 1] > f[i - 1][j])
        f[i][j] = f[i][j - 1];
      else
        f[i][j] = f[i - 1][j];
    }
  }
  return f[p.size()][q.size()];
}

int main() {
  int n;
  string p[300];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> p[2 * i] >> p[2 * i + 1];
  }
  for (int i = 0; i < n; i++) {
    cout << LCS(p[2 * i], p[2 * i + 1]) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

int LCS(string p, string q) {
  vector<vector<int>> f(p.size() + 1, vector<int>(q.size() + 1, 0));
  int t;
  for (int i = 1; i <= p.size(); i++) {
    for (int j = 1; j <= q.size(); j++) {
      if (p[i - 1] == q[j - 1])
        f[i][j] = f[i - 1][j - 1] + 1;
      else if (f[i][j - 1] > f[i - 1][j])
        f[i][j] = f[i][j - 1];
      else
        f[i][j] = f[i - 1][j];
    }
  }
  return f[p.size()][q.size()];
}

int main() {
  int n;
  string p[150];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> p[2 * i] >> p[2 * i + 1];
  }
  for (int i = 0; i < n; i++) {
    cout << LCS(p[2 * i], p[2 * i + 1]) << endl;
  }
  return 0;
}
Problem p02235 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

string str1, str2;

int m[1000][1000];

int LCS(int i, int j) {

  if (i == -1 || j == -1) {
    return 0;
  } else if (m[i][j] != 0) {
    return m[i][j];
  } else if (str1[i] == str2[j]) {
    m[i][j] = LCS(i - 1, j - 1) + 1;
    return m[i][j];
  } else {
    m[i][j] = max(LCS(i - 1, j), LCS(i, j - 1));
    return m[i][j];
  }
}

int main() {
  int N;

  cin >> N;

  for (int i = 0; i < N; i++) {
    cin >> str1 >> str2;
    for (int j = 0; j < 1000; j++) {
      for (int k = 0; k < 1000; k++) {
        m[j][k] = 0;
      }
    }
    cout << LCS(str1.size() - 1, str2.size() - 1) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

string str1, str2;

int LCS(int i, int j) {

  if (i == -1 || j == -1) {
    return 0;
  } else if (str1[i] == str2[j]) {
    return LCS(i - 1, j - 1) + 1;
  } else {
    return max(LCS(i - 1, j), LCS(i, j - 1));
  }
}

int main() {
  int N;

  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> str1 >> str2;
    cout << LCS(str1.size() - 1, str2.size() - 1) << endl;
  }
  return 0;
}
Problem p02235 (C++) — buggy submission, verdict: Runtime Error

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

constexpr int MAX_XY = 1010;

int lx, ly, q, dp[MAX_XY][MAX_XY];
std::string sx, sy;

void fill_dp() {
  for (int i = 1; i <= lx; ++i) {
    for (int j = 1; j <= ly; ++j) {
      if (sx[i - 1] == sy[j - 1])
        dp[i][j] = dp[i - 1][j - 1] + 1;
      else
        dp[i][j] = std::max(dp[i - 1][j], dp[i][j - 1]);
    }
  }
}

void solve() {
  fill_dp();
  std::cout << dp[lx][ly] << std::endl;
  return;
}

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

  for (int i = 0; i < q; ++i) {
    std::cin >> sx >> sy;
    lx = sx.size(), ly = sy.size();
    solve();
  }
  return 0;
}

Error: 0

Fixed submission:

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

constexpr int MAX_XY = 1000;

int lx, ly, q, dp[MAX_XY][MAX_XY];
std::string sx, sy;

void fill_dp() {
  for (int i = 1; i <= lx; ++i) {
    for (int j = 1; j <= ly; ++j) {
      if (sx[i - 1] == sy[j - 1])
        dp[i][j] = dp[i - 1][j - 1] + 1;
      else
        dp[i][j] = std::max(dp[i - 1][j], dp[i][j - 1]);
    }
  }
}

void solve() {
  fill_dp();
  std::cout << dp[lx][ly] << std::endl;
  return;
}

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

  for (int i = 0; i < q; ++i) {
    std::cin >> sx >> sy;
    lx = sx.size(), ly = sy.size();
    solve();
  }
  return 0;
}
Problem p02235 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

const int N = 1000;
int c[N + 1][N + 1];

int lcs(string X, string Y) {
  int m = X.size();
  int n = Y.size();
  int maxl = 0;
  X = ' ' + X;
  Y = ' ' + Y;
  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][j - 1], c[i - 1][j]);
      }
      maxl = max(maxl, c[i][j]);
    }
  }
  return maxl;
}

int main() {

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


Error: -11

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

const int N = 10000;

int lcs(string X, string Y) {
  int c[N + 1][N + 1];
  int m = X.size();
  int n = Y.size();
  int maxl = 0;
  X = ' ' + X;
  Y = ' ' + Y;
  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][j - 1], c[i - 1][j]);
      }
      maxl = max(maxl, c[i][j]);
    }
  }
  return maxl;
}

int main() {

  string s1, s2;
  int p;
  cin >> p;
  for (int i = 0; i < p; i++) {
    cin >> s1 >> s2;
    cout << lcs(s1, s2) << endl;
  }
  return 0;
}
Problem p02235 (C++) — buggy submission, verdict: Runtime Error

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

int main(int argc, char *argv[]) {
  int n;
  cin >> n;
  for (int r = 0; r < n; r++) {

    string x, y;
    cin >> x >> y;
    int dp[y.length() + 1][x.length() + 1];
    memset(dp, 0, sizeof(dp));
    for (int i = 0; i < y.length(); i++) {
      for (int j = 0; j < x.length(); j++) {
        if (y[i] == x[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]);
        }
      }
    }
    std::cout << dp[y.length()][x.length()] << std::endl;
  }
  return 0;
}

Error: WA

Fixed submission:

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

int main(int argc, char *argv[]) {
  int n;
  cin >> n;
  for (int r = 0; r < n; r++) {

    string x, y;
    cin >> x >> y;
    string dp[y.length() + 1][x.length() + 1];

    for (int i = 0; i < y.length(); i++) {
      for (int j = 0; j < x.length(); j++) {
        if (y[i] == x[j]) {
          dp[i + 1][j + 1] = dp[i][j] + y[i];
        } else {
          dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]);
        }
      }
    }
    int ans = 0;
    for (int i = 0; i < n; i++) {
      ans = max(ans, (int)dp[y.length()][i].length());
    }
    std::cout << ans << std::endl;
  }
  return 0;
}
Problem p02235 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <map>
#include <set>
#include <vector>

using namespace std;

const int kMax = 1000 + 10;

int n, m;
char a_str[kMax], b_str[kMax];
int dp[kMax];

vector<int> init_num() {
  vector<int> res;
  map<char, set<int>> table;
  for (int i = 0; i < n; ++i) {
    if (table.count(a_str[i]) > 0)
      table[a_str[i]].insert(i);
    else {
      set<int> s;
      s.insert(i);
      table[a_str[i]] = s;
    }
  }
  for (int i = 0; i < m; ++i) {
    if (table.count(b_str[i]) > 0) {
      for (auto it = table[b_str[i]].rbegin(); it != table[b_str[i]].rend();
           ++it) {
        res.push_back(*it);
      }
    }
  }
  return res;
}

int solve() {
  n = strlen(a_str);
  m = strlen(b_str);
  vector<int> num = init_num();
  if (num.empty())
    return 0;
  int res = 1;
  dp[0] = num[0];
  for (int i = 1; i < num.size(); ++i) {
    if (dp[res - 1] < num[i])
      dp[res++] = num[i];
    else
      dp[lower_bound(dp, dp + res, num[i]) - dp] = num[i];
  }
  return res;
}

int main() {
  int q;
  scanf("%d", &q);
  while (q--) {
    scanf("%s%s", a_str, b_str);
    printf("%d\\n", solve());
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <map>
#include <set>
#include <vector>

using namespace std;

const int kMax = 1000 + 10;

int n, m;
char a_str[kMax], b_str[kMax];
int dp[kMax];

vector<int> init_num() {
  vector<int> res;
  map<char, set<int>> table;
  for (int i = 0; i < n; ++i) {
    if (table.count(a_str[i]) > 0)
      table[a_str[i]].insert(i);
    else {
      set<int> s;
      s.insert(i);
      table[a_str[i]] = s;
    }
  }
  for (int i = 0; i < m; ++i) {
    if (table.count(b_str[i]) > 0) {
      for (auto it = table[b_str[i]].rbegin(); it != table[b_str[i]].rend();
           ++it) {
        res.push_back(*it);
      }
    }
  }
  return res;
}

int solve() {
  int res = 1;
  n = strlen(a_str);
  m = strlen(b_str);
  vector<int> num = init_num();
  dp[0] = num[0];
  for (int i = 1; i < num.size(); ++i) {
    if (dp[res - 1] < num[i])
      dp[res++] = num[i];
    else
      dp[lower_bound(dp, dp + res, num[i]) - dp] = num[i];
  }
  return res;
}

int main() {
  int q;
  scanf("%d", &q);
  while (q--) {
    scanf("%s%s", a_str, b_str);
    printf("%d\\n", solve());
  }
  return 0;
}
Problem p02235 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define pi 3.141592653589793

int solve(string s1, string s2, int l1, int l2) {
  int a[1001][1001];
  for (int i = 0; i <= l1; i++) {
    a[i][0] = 0;
  }
  for (int i = 0; i <= l2; i++) {
    a[0][i] = 0;
  }
  for (int i = 1; i <= l1; i++) {
    for (int j = 1; j <= l2; j++) {
      if (s1[i - 1] == s2[j - 1])
        a[i][j] = a[i - 1][j - 1] + 1;
      else
        a[i][j] = max(a[i - 1][j], a[i][j - 1]);
    }
  }
  return a[l1][l2];
}

int main() {
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    string s1, s2;
    cin >> s1 >> s2;
    cout << solve(s1, s2, s1.length(), s2.length()) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define pi 3.141592653589793

int solve(string s1, string s2, int l1, int l2) {
  if (l1 == -1 || l2 == -1)
    return 0;
  else if (s1[l1] == s2[l2])
    return solve(s1, s2, l1 - 1, l2 - 1) + 1;
  else
    return max(solve(s1, s2, l1, l2 - 1), solve(s1, s2, l1 - 1, l2));
}

int main() {
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    string s1, s2;
    cin >> s1 >> s2;
    cout << solve(s1, s2, s1.length() - 1, s2.length() - 1) << endl;
  }
  return 0;
}
Problem p02235 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

int LCS(string X, string Y) {

  int m, n;
  m = X.size();
  n = Y.size();

  int c[m + 1][n + 1];

  for (int i = 0; i <= m; ++i) {
    c[i][0] = 0;
  }
  for (int i = 0; i <= n; ++i) {
    c[0][i] = 0;
  }

  X = ' ' + X;
  Y = ' ' + Y;

  int maxl = 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() {

  int q;
  scanf("%d", &q);

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

  return 0;
}

Error: 0

Fixed submission:

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

using namespace std;

int LCS(string X, string Y) {

  int lcs[X.size() + 1][Y.size() + 1];

  for (int i = 0; i < X.size(); ++i) {
    lcs[0][i] = 0;
  }
  for (int i = 0; i < Y.size(); ++i) {
    lcs[i][0] = 0;
  }

  for (int i = 1; i <= X.size(); ++i) {
    for (int j = 1; j <= Y.size(); ++j) {
      if (X[i - 1] == Y[j - 1]) {
        lcs[i][j] = lcs[i - 1][j - 1] + 1;
      } else if (lcs[i - 1][j] > lcs[i][j - 1]) {
        lcs[i][j] = lcs[i - 1][j];
      } else {
        lcs[i][j] = lcs[i][j - 1];
      }
    }
  }

  return lcs[X.size()][Y.size()];
}

int main() {

  int q;
  scanf("%d", &q);

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

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

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

int main() {
  int n;
  char x[1001], y[1001];
  int dp[1010][1010];
  // load n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    // load string
    scanf("%s", x);
    scanf("%s", y);
    // dp
    for (int j = 0; j < strlen(x); j++) {
      for (int k = 0; k < strlen(y); k++) {
        if (x[j] == y[k]) {
          dp[j + 1][k + 1] = dp[j][k] + 1;
        }

        else {
          if (dp[j][k + 1] > dp[j + 1][k]) {
            dp[j + 1][k + 1] = dp[j][k + 1];
          }

          else {
            dp[j + 1][k + 1] = dp[j + 1][k];
          }
        }
      }
    }
    cout << dp[strlen(x)][strlen(y)] << endl;
  }

  return 0;
}

Error: -11

Fixed submission:

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

int main() {
  int n;
  char x[10000], y[10000];
  int dp[10010][10010];
  // load n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    // load string
    scanf("%s", x);
    scanf("%s", y);
    // dp
    for (int j = 0; j < strlen(x); j++) {
      for (int k = 0; k < strlen(y); k++) {
        if (x[j] == y[k]) {
          dp[j + 1][k + 1] = dp[j][k] + 1;
        }

        else {
          if (dp[j][k + 1] > dp[j + 1][k]) {
            dp[j + 1][k + 1] = dp[j][k + 1];
          }

          else {
            dp[j + 1][k + 1] = dp[j + 1][k];
          }
        }
      }
    }
    cout << dp[strlen(x)][strlen(y)] << endl;
  }

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

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <string>
using namespace std;
#define MAX 100000

static const int N = 1000;

int lcs(string X, string Y) {
  int c[N + 1][N + 1];
  int m = X.size();
  int n = Y.size();
  int ans = 0;
  X = ' ' + X;
  Y = ' ' + Y;
  for (int i; i <= m; i++)
    c[i][0] = 0;
  for (int j; 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]);
      }
      ans = max(ans, c[i][j]);
    }
  }
  return ans;
}

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: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <string>
using namespace std;
#define MAX 100000

static const int N = 100;

int lcs(string X, string Y) {
  int c[N + 1][N + 1];
  int m = X.size();
  int n = Y.size();
  int ans = 0;
  X = ' ' + X;
  Y = ' ' + Y;
  for (int i; i <= m; i++)
    c[i][0] = 0;
  for (int j; 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]);
      }
      ans = max(ans, c[i][j]);
    }
  }
  return ans;
}

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;
}