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

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string>
#define MAX 1000

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

int main() {
  std::string s1, s2;
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    std::cin >> s1 >> s2;
    int a = lcs(s1, s2);
    printf("%d\\n", lcs(s1, s2));
  }

  return 0;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string>
#define MAX 10000

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

int main() {
  std::string s1, s2;
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    std::cin >> s1 >> s2;
    int a = lcs(s1, s2);
    printf("%d\\n", lcs(s1, s2));
  }

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

#include <bits/stdc++.h>
using namespace std;
const long long INF = 123456789;
const long long MOD = 1000000007;
const long double PI = 3.1415926;
#define FOR(i, r, n) for (ll i = (ll)(r); i < (ll)(n); i++)
#define RFOR(i, r, n) for (ll i = (ll)(n - 1); i >= r; i--)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define RALL(x) x.rbegin(), x.rend()
typedef long long int ll;
typedef vector<ll> vi;
typedef vector<pair<ll, ll>> vp;
typedef vector<string> vs;
typedef vector<char> vc;
typedef list<ll> lst;

ll n, m, k, ans = 0, sum = 0, cnt = 0;
string s;
char c;

#define Endl endl

/*--------------------template--------------------*/

ll dp[1006][1006] = {0};

int main() {
  cin >> n;
  ll ma = 0;
  REP(i, n) {
    string t;
    cin >> s >> t;
    ma = 0;
    s = ' ' + s; //????????????????????????
    t = ' ' + t;
    FOR(i, 1, s.size()) { dp[i][0] = 0; }
    FOR(i, 1, t.size()) { dp[0][i] = 0; }
    FOR(i, 1, s.size()) {
      FOR(j, 1, t.size()) {
        if (s[i] == t[j]) {
          dp[i][j] = dp[i - 1][j - 1] + 1;
        } else {
          dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
        }
        ma = max(ma, dp[i][j]);
      }
    }
    cout << ma << Endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const long long INF = 123456789;
const long long MOD = 1000000007;
const long double PI = 3.1415926;
#define FOR(i, r, n) for (ll i = (ll)(r); i < (ll)(n); i++)
#define RFOR(i, r, n) for (ll i = (ll)(n - 1); i >= r; i--)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define RALL(x) x.rbegin(), x.rend()
typedef long long int ll;
typedef vector<ll> vi;
typedef vector<pair<ll, ll>> vp;
typedef vector<string> vs;
typedef vector<char> vc;
typedef list<ll> lst;

ll n, m, k, ans = 0, sum = 0, cnt = 0;
string s;
char c;

#define Endl endl

/*--------------------template--------------------*/

int main() {
  cin >> n;
  ll ma = 0;
  REP(i, n) {
    string t;
    cin >> s >> t;
    ll c[100][100] = {0};
    ma = 0;
    s = ' ' + s; //????????????????????????
    t = ' ' + t;
    FOR(i, 1, s.size()) { c[i][0] = 0; }
    FOR(i, 1, t.size()) { c[0][i] = 0; }
    FOR(i, 1, s.size()) {
      FOR(j, 1, t.size()) {
        if (s[i] == t[j]) {
          c[i][j] = c[i - 1][j - 1] + 1;
        } else {
          c[i][j] = max(c[i - 1][j], c[i][j - 1]);
        }
        ma = max(ma, c[i][j]);
      }
    }
    cout << ma << Endl;
  }
}
Problem p02235 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define INF 99999999

#define MAX_S 2000
#define MAX_T 2000

template <typename T> int lcs(T s, T t) {
  int lens = s.size();
  int lent = t.size();
  int dp[MAX_S + 1][MAX_T + 1] = {};

  rep(i, lens) {
    rep(j, lent) {
      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]);
    }
  }
  return dp[lens][lent];
}

int main() {
  int n;
  cin >> n;
  rep(i, n) {
    string s, t;
    cin >> s >> t;
    cout << lcs(s, t) << endl;
  }
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define INF 99999999

#define MAX_S 200
#define MAX_T 200

template <typename T> int lcs(T s, T t) {
  int lens = s.size();
  int lent = t.size();
  int dp[MAX_S + 1][MAX_T + 1] = {};

  rep(i, lens) {
    rep(j, lent) {
      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]);
    }
  }
  return dp[lens][lent];
}

int main() {
  int n;
  cin >> n;
  rep(i, n) {
    string s, t;
    cin >> s >> t;
    cout << lcs(s, t) << endl;
  }
}
Problem p02235 (C++) — buggy submission, verdict: Time Limit Exceeded

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

    // print
    cout << dp[strlen(x)][strlen(y)] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int dp[1010][1010];
int main() {
  int n;
  char x[1001], y[1001];

  // 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 <string>
#include <vector>

using namespace std;

string s, t;

int main() {
  int n;
  cin >> n;
  while (n--) {
    // dp[i][j] :=
    // s???i??????????????§???t???j??????????????§?????£?????¨??????LCS
    vector<vector<int>> dp(2000, vector<int>(2000));
    cin >> s >> t;
    dp[0][0] = (s[0] == t[0]);
    for (int i = 1; i < s.size(); i++)
      dp[i][0] = (bool)(dp[i - 1][0] + (s[i] == t[0]));
    for (int j = 1; j < t.size(); j++)
      dp[0][j] = (bool)(dp[0][j - 1] + (s[0] == t[j]));

    for (int i = 1; i < s.size(); i++) {
      for (int j = 1; j < t.size(); j++) {
        if (s[i] == t[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[s.size() - 1][t.size() - 1] << endl;
  }
}

Error: 0

Fixed submission:

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

using namespace std;

string s, t;

int main() {
  int n;
  cin >> n;
  while (n--) {
    // dp[i][j] :=
    // s???i??????????????§???t???j??????????????§?????£?????¨??????LCS
    vector<vector<int>> dp(200, vector<int>(200));
    cin >> s >> t;
    dp[0][0] = (s[0] == t[0]);
    for (int i = 1; i < s.size(); i++)
      dp[i][0] = (bool)(dp[0][0] + (s[i] == t[0]));
    for (int j = 1; j < t.size(); j++)
      dp[0][j] = (bool)(dp[0][0] + (s[0] == t[j]));

    for (int i = 1; i < s.size(); i++) {
      for (int j = 1; j < t.size(); j++) {
        if (s[i] == t[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[s.size() - 1][t.size() - 1] << endl;
  }
}
Problem p02235 (C++) — buggy submission, verdict: Runtime Error

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

#define MAX(a, b) ((a) > (b) ? (a) : (b))

int LCS(string &wordA, string &wordB) {
  int i, j;
  int sizeA, sizeB;
  vector<vector<int>> lcs;
  sizeA = (int)wordA.size();
  sizeB = (int)wordB.size();
  lcs.resize(sizeA + 1);
  lcs[0].resize(sizeB + 1);
  for (i = 0; i <= sizeB; ++i) {
    lcs[0][i] = 0;
  }
  for (i = 1; i <= sizeA; ++i) {
    lcs[i].resize(sizeB + 1);
    lcs[i][0] = 0;
    for (j = 1; j <= sizeB; ++j) {
      if (wordA[i - 1] == wordB[j - 1])
        lcs[i][j] = lcs[i - 1][j - 1] + 1;
      else
        lcs[i][j] = MAX(lcs[i - 1][j], lcs[i][j - 1]);
    }
  }
  return lcs[sizeA][sizeB];
}

int main() {
  int i, j, k, q;
  cin >> q;
  string wordA, wordB;
  for (i = 0; i < q; ++i) {
    cin >> wordA >> wordB;
    cout << LCS(wordA, wordB) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;

int charCode(char charactor) {
  if (charactor >= 'a' && charactor <= 'z')
    return (int)(charactor - 'a');
  else
    return (int)(charactor - 'A' + 26);
}

bool findSubStr(string &shortword, vector<vector<int>> &charMap) {
  int i, j = 0;
  for (i = 0; i < (int)shortword.size(); ++i) {
    if (charMap[charCode(shortword[i])].empty())
      return false;
    auto itr = charMap[charCode(shortword[i])].begin();
    while (itr != charMap[charCode(shortword[i])].end()) {
      if (*itr < j) {
        ++itr;
      } else {
        break;
      }
    }
    if (itr == charMap[charCode(shortword[i])].end())
      return false;
    j = 1 + *itr;
  }
  return true;
}

int longestSQ(string &shortword, string &longword) {
  int i, j;
  string tempstr, prestr;
  vector<vector<int>> charMap(52);
  queue<string> targetword;
  map<string, bool> inqueue;
  for (i = 0; i < (int)longword.size(); ++i) {
    charMap[charCode(longword[i])].push_back(i);
  }
  targetword.push(shortword);
  while (!targetword.empty()) {
    tempstr = targetword.front();
    if (findSubStr(tempstr, charMap))
      return (int)tempstr.size();
    j = (int)tempstr.size();
    for (i = 0; i < j; ++i) {
      tempstr = targetword.front();
      if (i == 0 || tempstr[i] != tempstr[i - 1]) {
        tempstr.erase(tempstr.begin() + i);
        if (inqueue.find(tempstr) == inqueue.end()) {
          targetword.push(tempstr);
          inqueue[tempstr] = true;
        }
      }
    }
    targetword.pop();
  }
  return 0;
}

int main() {
  int i, q;
  cin >> q;
  string wordA, wordB;
  for (i = 0; i < q; ++i) {
    cin >> wordA >> wordB;
    if (wordA.size() < wordB.size()) {
      printf("%d\\n", longestSQ(wordA, wordB));
    } else {
      printf("%d\\n", longestSQ(wordB, wordA));
    }
  }
  return 0;
}
Problem p02235 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int lengthOfLcs_r(const char x[], const char y[], int ix, int iy, int xsize,
                  int ysize, int **lengthTable) {
  if (ix == xsize || iy == ysize) {
    return 0;
  }
  if (lengthTable[ix][iy] != -1) {
    return lengthTable[ix][iy];
  }
  if (x[ix] == y[iy]) {
    return lengthTable[ix][iy] =
               lengthOfLcs_r(x, y, ix + 1, iy + 1, xsize, ysize, lengthTable) +
               1;
  }
  return lengthTable[ix][iy] =
             max(lengthOfLcs_r(x, y, ix, iy + 1, xsize, ysize, lengthTable),
                 lengthOfLcs_r(x, y, ix + 1, iy, xsize, ysize, lengthTable));
}

int lengthOfLcs(string x, string y) {
  int xsize = x.size(), ysize = y.size();

  int **lengthTable = new int *[xsize];
  for (int i = 0; i < xsize; ++i) {
    lengthTable[i] = new int[ysize];
    fill_n(lengthTable[i], ysize, -1);
  }

  int ret =
      lengthOfLcs_r(x.c_str(), y.c_str(), 0, 0, xsize, ysize, lengthTable);

  for (int i = 0; i < xsize; i++) {
    delete[] lengthTable[i];
  }
  delete[] lengthTable;

  return ret;
}

int main() {
  int q;
  string x, y;

  cin >> q;
  for (int i = 0; i < q; ++i) {
    cin >> x >> y;
    cout << lengthOfLcs(x, y) << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

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

int lengthOfLcs_r(const char x[], const char y[], int ix, int iy, int xsize,
                  int ysize) {
  if (ix == xsize || iy == ysize) {
    return 0;
  }
  if (x[ix] == y[iy]) {
    return lengthOfLcs_r(x, y, ix + 1, iy + 1, xsize, ysize) + 1;
  }
  return max(lengthOfLcs_r(x, y, ix, iy + 1, xsize, ysize),
             lengthOfLcs_r(x, y, ix + 1, iy, xsize, ysize));
}

int lengthOfLcs(string x, string y) {
  return lengthOfLcs_r(x.c_str(), y.c_str(), 0, 0, x.length(), y.length());
}

int main() {
  int q;
  string x, y;

  cin >> q;
  for (int i = 0; i < q; ++i) {
    cin >> x >> y;
    cout << lengthOfLcs(x, y) << endl;
  }
  return 0;
}
Problem p02235 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <string>

using namespace std;

const char *X, *Y;
int X_length, Y_length;
bool alreadySeenTable[256];
int table_lcs[1001][1001];

int lcs(int x_begin, int y_begin) {
  int i, j, to;
  int max = 0;
  int v_lcs;

  if (table_lcs[x_begin][y_begin] != -1) {
    return table_lcs[x_begin][y_begin];
  }

  for (i = 0; i < 256; i++) {
    alreadySeenTable[i] = false;
  }
  i = x_begin;
  j = y_begin;
  to = Y_length - 1;
  while (i <= X_length - 1) {
    bool found = false;
    while (j <= to) {
      if (X[i] == Y[j]) {
        v_lcs = lcs(i + 1, j + 1) + 1;
        if (v_lcs > max) {
          max = v_lcs;
        }
        found = true;
        break;
      } else {
        j++;
      }
    }
    if (found == false) {
      i++;
      j = y_begin;
    } else {
      i++;
      to = j - 1;
      j = y_begin;
    }
  }
  return table_lcs[x_begin][y_begin] = max;
}

int main() {

  int N;
  cin >> N;
  string stringX, stringY;

  for (int i = 1; i <= N; i++) {
    for (int i = 0; i < 1001; i++) {
      for (int j = 0; j < 1001; j++) {
        table_lcs[i][j] = -1;
      }
    }
    cin >> stringX >> stringY;
    X = stringX.c_str();
    X_length = stringX.size();
    Y = stringY.c_str();
    Y_length = stringY.size();
    printf("%d\\n", lcs(0, 0));
  }
  return 0;
}

Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <string>

using namespace std;

const char *X, *Y;

int table_lcs[1001][1001];

int lcs(int x_begin, int y_begin) {
  if (table_lcs[x_begin][y_begin] != -1) {
    return table_lcs[x_begin][y_begin];
  }
  int max = 0;
  for (int i = x_begin; X[i] != '\\0'; i++) {
    for (int j = y_begin; Y[j] != '\\0'; j++) {
      if (X[i] == Y[j]) {
        int v_lcs = lcs(i + 1, j + 1);
        if (v_lcs + 1 > max) {
          max = v_lcs + 1;
        }
      }
    }
  }
  return table_lcs[x_begin][y_begin] = max;
}

int main() {

  int N;
  cin >> N;
  string stringX, stringY;
  for (int i = 1; i <= N; i++) {
    for (int i = 0; i < 1001; i++) {
      for (int j = 0; j < 1001; j++) {
        table_lcs[i][j] = -1;
      }
    }
    cin >> stringX >> stringY;
    X = stringX.c_str();
    Y = stringY.c_str();
    printf("%d\\n", lcs(0, 0));
  }
  return 0;
}
Problem p02235 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

#define pb push_back

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<i_i, int> p_i;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
template <class T> using vv = vector<vector<T>>;

int main() {
  int t;
  cin >> t;
  vi results;
  for (int dataset = 0; dataset < t; dataset++) {
    string a, b;
    cin >> a;
    cin >> b;
    vvi len(a.length() + 1, vi(b.length() + 1, 0));
    for (int ia = 0; ia < a.length(); ia++) {
      for (int ib = 0; ib < b.length(); ib++) {
        if (a[ia] == b[ib]) {
          len[ia + 1][ib + 1] = len[ia][ib] + 1;
        } else {
          len[ia + 1][ib + 1] = max(len[ia + 1][ib], len[ia][ib + 1]);
        }
      }
    }
    results.pb(len[a.length()][b.length()]);
  }

  for (int it = 0; it < t; it++) {
    printf("%d\\n", results[it]);
  }

  return 0;
}

Error: -6
free(): invalid next size (fast)


Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

#define pb push_back

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<i_i, int> p_i;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
template <class T> using vv = vector<vector<T>>;

int main() {
  int t;
  cin >> t;
  vi results;
  for (int dataset = 0; dataset < t; dataset++) {
    string a, b;
    cin >> a;
    cin >> b;
    vvi len(a.length() + 1, vi(b.length(), 0));
    for (int ia = 1; ia <= a.length(); ia++) {
      for (int ib = 1; ib <= b.length(); ib++) {
        if (a[ia] == b[ib]) {
          len[ia][ib] = len[ia - 1][ib - 1] + 1;
        } else {
          len[ia][ib] = max(len[ia][ib - 1], len[ia - 1][ib]);
        }
      }
    }
    results.pb(len[a.length()][b.length()]);
  }

  for (int it = 0; it < t; it++) {
    printf("%d\\n", results[it]);
  }

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

#include <stdio.h>

int judge(char A[], char B[]) {
  int a = 0, b = 0, sw = 0, ans = 0;
  int i, j;
  int C[1001][1001];

  while (sw == 0) {
    if (A[a] != '\\0')
      a++;
    else
      sw = 1;
  }

  sw = 0;

  while (sw == 0) {
    if (B[b] != '\\0')
      b++;
    else
      sw = 1;
  }

  for (i = 1; i < a + 1; i++)
    C[i][0] = 0;
  for (i = 1; i < b + 1; i++)
    C[0][i] = 0;

  for (i = 1; i < a + 1; i++) {
    for (j = 1; j < b + 1; j++) {
      if (A[i - 1] == B[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 (ans < C[i][j])
        ans = C[i][j];
    }
  }

  return ans;
}

int main() {
  char A[1000], B[1000];
  int n, i;

  scanf("%d", &n);

  for (i = 0; i < n; i++) {
    scanf("%s", A);
    scanf("%s", B);
    printf("%d\\n", judge(A, B));
  }

  return 0;
}

Error: WA

Fixed submission:

#include <stdio.h>

int judge(char A[], char B[]) {
  int a = 0, b = 0, sw = 0, ans = 0;
  int i, j;
  int C[1001][1001];

  while (sw == 0) {
    if (A[a] != EOF)
      a++;
    else
      sw = 1;
  }

  sw = 0;

  while (sw == 0) {
    if (B[b] != EOF)
      b++;
    else
      sw = 1;
  }

  for (i = 1; i < a + 1; i++)
    C[i][0] = 0;
  for (i = 1; i < b + 1; i++)
    C[0][i] = 0;

  for (i = 1; i < a + 1; i++) {
    for (j = 1; j < b + 1; j++) {
      if (A[i - 1] == B[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 (ans < C[i][j])
        ans = C[i][j];
    }
  }

  return ans;
}

int main() {
  char A[1000], B[1000];
  int n, i;

  scanf("%d", &n);

  for (i = 0; i < n; i++) {
    scanf("%s", A);
    scanf("%s", B);
    printf("%d\\n", judge(A, B));
  }

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

// #define		FILE_INPUT_DEFINE
#ifdef FILE_INPUT_DEFINE
#include <fstream>
#endif // FILE_INPUT_DEFINE

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

using namespace std;

#define MAX 1000
#define ind(x) ((x)-1)

int C[MAX + 1][MAX + 1];

int LCS(string &X, string &Y) {
  int Xlen = X.length();
  int Ylen = Y.length();

  for (int i = 0; i <= Xlen; ++i) {
    C[i][0] = 0;
  }

  for (int i = 0; i <= Ylen; ++i) {
    C[0][i] = 0;
  }

  for (int i = 1; i <= Xlen; ++i) {
    for (int j = 1; j <= Ylen; ++j) {
      if (X[ind(i)] == Y[ind(j)]) {
        C[i][j] = C[i - 1][j - 1] + 1;
      } else {
        C[i][j] = max(C[i - 1][j], C[i][j - 1]);
      }
    }
  }

  return (C[Xlen][Ylen]);
}

int main() {
#ifdef FILE_INPUT_DEFINE
  // input from txt
  std::ifstream in(R"(C:\\Temp\\input.txt)");
  std::cin.rdbuf(in.rdbuf());
#endif // FILE_INPUT_DEFINE

  string X, Y;
  int n;

  cin >> n;
  cin.clear();
  cin.ignore();

  for (int i = 0; i < n; ++i) {
    getline(cin, X);
    getline(cin, Y);

    cout << LCS(X, Y) << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

// #define		FILE_INPUT_DEFINE
#ifdef FILE_INPUT_DEFINE
#include <fstream>
#endif // FILE_INPUT_DEFINE

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

using namespace std;

#define MAX 1000
#define ind(x) ((x)-1)

int C[MAX][MAX];

int LCS(string &X, string &Y) {
  int Xlen = X.length();
  int Ylen = Y.length();

  for (int i = 0; i <= Xlen; ++i) {
    C[i][0] = 0;
  }

  for (int i = 0; i <= Ylen; ++i) {
    C[0][i] = 0;
  }

  for (int i = 1; i <= Xlen; ++i) {
    for (int j = 1; j <= Ylen; ++j) {
      if (X[ind(i)] == Y[ind(j)]) {
        C[i][j] = C[i - 1][j - 1] + 1;
      } else {
        C[i][j] = max(C[i - 1][j], C[i][j - 1]);
      }
    }
  }

  return (C[Xlen][Ylen]);
}

int main() {
#ifdef FILE_INPUT_DEFINE
  // input from txt
  std::ifstream in(R"(C:\\Temp\\input.txt)");
  std::cin.rdbuf(in.rdbuf());
#endif // FILE_INPUT_DEFINE

  string X, Y;
  int n;

  cin >> n;
  cin.clear();
  cin.ignore();

  for (int i = 0; i < n; ++i) {
    getline(cin, X);
    getline(cin, Y);

    cout << LCS(X, Y) << endl;
  }

  return 0;
}