prithivMLmods/Coder-Stat huggingface dataset:
Problem p02234 (C++)

Buggy submission (status: Runtime Error):
#include <climits>
#include <iostream>
using namespace std;
int m[100][100];

int MCM(int p[], int n) {
  for (int i = 0; i <= n; i++)
    m[i][i] = 0;
  for (int l = 2; l <= n; l++) {
    for (int i = 1; i <= n - l + 1; i++) {
      int j = i + l - 1;
      m[i][j] = INT_MAX;
      for (int k = i; k <= j; k++) {
        int q = m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j];
        if (m[i][j] > q) {
          m[i][j] = q;
        }
      }
    }
  }
  return m[1][n];
}

int main() {
  int n, p[200];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> p[i] >> p[i + 1];
  }
  cout << MCM(p, n) << endl;
  return 0;
}

Fixed submission:
#include <climits>
#include <iostream>
using namespace std;
int m[100][100];

int MCM(int p[], int n) {
  for (int i = 0; i <= n; i++)
    m[i][i] = 0;
  for (int l = 2; l <= n; l++) {
    for (int i = 1; i <= n - l + 1; i++) {
      int j = i + l - 1;
      m[i][j] = INT_MAX;
      for (int k = i; k <= j - 1; k++) {
        int q = m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j];
        if (m[i][j] > q) {
          m[i][j] = q;
        }
      }
    }
  }
  return m[1][n];
}

int main() {
  int n, p[200];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> p[i] >> p[i + 1];
  }
  cout << MCM(p, n) << endl;
  return 0;
}
Problem p02235 (Python)

Buggy submission (status: Time Limit Exceeded):
q = int(input())
for _ in range(q):
    x = input()
    y = input()
    m = len(x)
    n = len(y)
    c = [0] * (n + 1)
    for i in range(m):
        tmp = x[i]
        c_ = c[:]
        for j in range(n):
            if tmp == y[j]:
                c[j + 1] = c_[j] + 1
            elif c[j + 1] < c[j]:
                c[j + 1] = c[j]
    print(c[-1])


Fixed submission:
def lcs(x, y):
    a = len(x)
    b = len(y)
    c1 = [0] * (b + 1)
    for i in range(a):
        e1 = x[i]
        c2 = c1[:]
        for j in range(b):
            if e1 == y[j]:
                c1[j + 1] = c2[j] + 1
            elif c1[j + 1] < c1[j]:
                c1[j + 1] = c1[j]
    return c1[-1]


n = int(input().rstrip())
ret = []
for i in range(n):
    s1 = input().rstrip()
    s2 = input().rstrip()
    ret.append(lcs(s1, s2))
print(*ret, sep="\n")
Problem p02235 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repr(i, a, n) for (int i = a; i >= n; i--)
#define INF 999999999
#define pb(a) push_back(a)

using namespace std;
typedef pair<int, int> pii;
typedef long long int ll;
typedef vector<pii> VP;

int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};

int main() {
  int num;
  cin >> num;
  rep(k, 0, num) {
    string s1, s2;
    cin >> s1 >> s2;

    int dp
        [101]
        [101]; // s1???i??????????????§???s2???j??????????????§??§???LCS????±???????????????????
    int n = s1.length();
    int m = s2.length();
    rep(i, 0, m + 1) dp[0][i] = 0;

    rep(i, 0, n) {
      rep(j, 0, m) {
        if (s1[i] == s2[j])
          dp[i + 1][j + 1] = dp[i][j] + 1;
        else
          dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);
      }
    }
    cout << dp[n][m] << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repr(i, a, n) for (int i = a; i >= n; i--)
#define INF 999999999
#define pb(a) push_back(a)

using namespace std;
typedef pair<int, int> pii;
typedef long long int ll;
typedef vector<pii> VP;

int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};

int main() {
  int num;
  cin >> num;
  rep(k, 0, num) {
    string s1, s2;
    cin >> s1 >> s2;

    int dp
        [1001]
        [1001]; // s1???i??????????????§???s2???j??????????????§??§???LCS????±???????????????????
    int n = s1.length();
    int m = s2.length();
    rep(i, 0, m + 1) dp[0][i] = 0;

    rep(i, 0, n) {
      rep(j, 0, m) {
        if (s1[i] == s2[j])
          dp[i + 1][j + 1] = dp[i][j] + 1;
        else
          dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);
      }
    }
    cout << dp[n][m] << endl;
  }
  return 0;
}
Problem p02235 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
constexpr int MOD = 1000000007;
constexpr int INF = 2000000000;
using namespace std;

int lcs(string x, string y) {
  int c[10][10];
  int m = x.size();
  int n = y.size();
  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;
  c[0][0] = 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]);
      }
    }
  }
  return c[m][n];
}

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

Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
constexpr int MOD = 1000000007;
constexpr int INF = 2000000000;
using namespace std;

int lcs(string x, string y) {
  int c[1200][1200];
  int m = x.size();
  int n = y.size();
  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;
  c[0][0] = 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]);
      }
    }
  }
  return c[m][n];
}

int main() {
  int n;
  cin >> n;
  string a, b;
  for (int i = 0; i < n; i++) {
    cin >> a >> b;
    cout << lcs(a, b) << endl;
  }
}
Problem p02235 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repi(i, a, b) for (int i = (a); i < (b); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
int dp[1000][1000];
int main() {
  int n;
  cin >> n;
  while (n--) {
    memset(dp, 0, sizeof(dp));
    string str1, str2;
    cin >> str1 >> str2;
    // int l =min(str1.length(),str2.length());
    for (int i = 0; i < str1.length(); i++) {
      for (int j = 0; j < str2.length(); j++) {
        if (str1[i] == str2[j]) {
          dp[i + 1][j + 1] = dp[i][j] + 1;
        } else {
          dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);
        }
      }
    }
    cout << dp[str1.length()][str2.length()] << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repi(i, a, b) for (int i = (a); i < (b); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
int dp[1050][1050];
int main() {
  int n;
  cin >> n;
  while (n--) {
    memset(dp, 0, sizeof(dp));
    string str1, str2;
    cin >> str1 >> str2;
    // int l =min(str1.length(),str2.length());
    for (int i = 0; i < str1.length(); i++) {
      for (int j = 0; j < str2.length(); j++) {
        if (str1[i] == str2[j]) {
          dp[i + 1][j + 1] = dp[i][j] + 1;
        } else {
          dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);
        }
      }
    }
    cout << dp[str1.length()][str2.length()] << endl;
  }
  return 0;
}
Problem p02235 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
// #include <chrono> //1e+9で割る auto end= chrono::system_clock::now()-st;
// cout<<end.count()%1e+9<<endl;
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef long double db;

#define fr first
#define sc second
#define pb push_back
#define rep(i, x) for (int i = 0; i < x; i++)
#define rep1(i, x) for (int i = 1; i <= x; i++)
#define rrep(i, x) for (int i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (int i = x; i > 0; i--)

int q;
string s1, s2;
int ans;
int dp[150][150];
int main() {
  cin >> q;
  rep(i, q) {
    cin >> s1 >> s2;
    rep(i, s1.size()) {
      rep(j, s2.size()) { dp[i][j] = 0; }
    }
    if (s1[0] == s2[0])
      dp[0][0] = 1;
    rep1(i, s1.size() - 1) {
      if (s1[i] == s2[0])
        dp[i][0] = 1;
      dp[i][0] = max(dp[i][0], dp[i - 1][0]);
    }
    rep1(i, s2.size() - 1) {
      if (s1[0] == s2[i])
        dp[0][i] = 1;
      dp[0][i] = max(dp[0][i], dp[0][i - 1]);
    }
    rep1(i, s1.size() - 1) {
      rep1(j, s2.size() - 1) {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
        if (s1[i] == s2[j])
          dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1);
      }
    }
    cout << dp[s1.size() - 1][s2.size() - 1] << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
// #include <chrono> //1e+9で割る auto end= chrono::system_clock::now()-st;
// cout<<end.count()%1e+9<<endl;
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef long double db;

#define fr first
#define sc second
#define pb push_back
#define rep(i, x) for (int i = 0; i < x; i++)
#define rep1(i, x) for (int i = 1; i <= x; i++)
#define rrep(i, x) for (int i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (int i = x; i > 0; i--)

int q;
string s1, s2;
int ans;
int dp[1510][1501];
int main() {
  cin >> q;
  rep(i, q) {
    cin >> s1 >> s2;
    rep(i, s1.size()) {
      rep(j, s2.size()) { dp[i][j] = 0; }
    }
    if (s1[0] == s2[0])
      dp[0][0] = 1;
    rep1(i, s1.size() - 1) {
      if (s1[i] == s2[0])
        dp[i][0] = 1;
      dp[i][0] = max(dp[i][0], dp[i - 1][0]);
    }
    rep1(i, s2.size() - 1) {
      if (s1[0] == s2[i])
        dp[0][i] = 1;
      dp[0][i] = max(dp[0][i], dp[0][i - 1]);
    }
    rep1(i, s1.size() - 1) {
      rep1(j, s2.size() - 1) {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
        if (s1[i] == s2[j])
          dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1);
      }
    }
    cout << dp[s1.size() - 1][s2.size() - 1] << endl;
  }
}
Problem p02235 (C++)

Buggy submission (status: 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.shrink_to_fit();
    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;
}

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 (status: 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[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;
}

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[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;
}
Problem p02235 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>

using namespace std;

// L[i][j] X[0]..X[i]??¨X[0]..Y[j]???LCS
int L[1000][1000] = {};
int main() {
  int testnum;
  cin >> testnum;
  for (int t = 0; t < testnum; ++t) {
    string X, Y;
    cin >> X >> Y;

    for (int i = 1; i <= X.length(); ++i) {
      for (int j = 1; j <= Y.length(); ++j) {
        if (X[i - 1] == Y[j - 1])
          L[i][j] = 1 + L[i - 1][j - 1];
        else
          L[i][j] = max(L[i][j - 1], L[i - 1][j]);
      }
    }

    cout << L[X.length()][Y.length()] << endl;
  }

  return 0;
}

Fixed submission:
#include <iostream>
#include <string>

using namespace std;

// L[i][j] X[0]..X[i]??¨X[0]..Y[j]???LCS
int L[1001][1001] = {};
int main() {
  int testnum;
  cin >> testnum;
  for (int t = 0; t < testnum; ++t) {
    string X, Y;
    cin >> X >> Y;

    for (int i = 1; i <= X.length(); ++i) {
      for (int j = 1; j <= Y.length(); ++j) {
        if (X[i - 1] == Y[j - 1])
          L[i][j] = 1 + L[i - 1][j - 1];
        else
          L[i][j] = max(L[i][j - 1], L[i - 1][j]);
      }
    }

    cout << L[X.length()][Y.length()] << endl;
  }

  return 0;
}
Problem p02235 (C++)

Buggy submission (status: 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[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;
}


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[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;
}
Problem p02235 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

string s, t;
int dp[100][100];

void solve() {
  cin >> s >> t;
  for (int i = 0; i < s.size(); i++) {
    for (int j = 0; j < t.size(); j++) {
      dp[i][j] =
          (i < 1 || j < 1 ? 0 : dp[i - 1][j - 1]) + (s[i] == t[j] ? 1 : 0);
      int a = (i < 1 ? 0 : dp[i - 1][j]);
      int b = (j < 1 ? 0 : dp[i][j - 1]);
      dp[i][j] = max(dp[i][j], max(a, b));
    }
  }

  cout << dp[s.size() - 1][t.size() - 1] << endl;
}

int main() {
  int n;
  cin >> n;
  while (n--)
    solve();

  return 0;
}

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

string s, t;
int dp[1000][1000];

void solve() {
  cin >> s >> t;
  for (int i = 0; i < s.size(); i++) {
    for (int j = 0; j < t.size(); j++) {
      dp[i][j] =
          (i < 1 || j < 1 ? 0 : dp[i - 1][j - 1]) + (s[i] == t[j] ? 1 : 0);
      int a = (i < 1 ? 0 : dp[i - 1][j]);
      int b = (j < 1 ? 0 : dp[i][j - 1]);
      dp[i][j] = max(dp[i][j], max(a, b));
    }
  }

  cout << dp[s.size() - 1][t.size() - 1] << endl;
}

int main() {
  int n;
  cin >> n;
  while (n--)
    solve();

  return 0;
}
Problem p02235 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string.h>

#define MAX 10000

using namespace std;

int lcs(string s1, string s2) {
  int C[MAX + 1][MAX + 1];
  int m = s1.size();
  int n = s2.size();
  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 (s1[i - 1] == s2[j - 1]) {
        C[i][j] = C[i - 1][j - 1] + 1;
      } else {
        C[i][j] = max(C[i][j - 1], C[i - 1][j]);
      }
    }
  }

  return C[m][n];
}

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

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string.h>

#define MAX 1000

using namespace std;

int lcs(string s1, string s2) {
  int C[MAX + 1][MAX + 1];
  int m = s1.size();
  int n = s2.size();
  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 (s1[i - 1] == s2[j - 1]) {
        C[i][j] = C[i - 1][j - 1] + 1;
      } else {
        C[i][j] = max(C[i][j - 1], C[i - 1][j]);
      }
    }
  }

  return C[m][n];
}

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
using namespace std;

int main() {
  int q;
  cin >> q;
  for (int m = 0; m < q; m++) {
    string wordx, wordy;
    cin >> wordx >> wordy;
    int L[1000][1000] = {0};

    for (int j = 0; j < wordy.size(); j++) {
      for (int i = 0; i < wordx.size(); i++) {
        if (wordx[i] == wordy[j]) {
          L[i + 1][j + 1] = 1 + L[i][j];
        } else {
          L[i + 1][j + 1] = max(L[i + 1][j], L[i][j + 1]);
        }
      }
    }
    cout << L[wordx.size()][wordy.size()] << endl;
  }
}

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

int main() {
  int q;
  cin >> q;
  for (int m = 0; m < q; m++) {
    string wordx, wordy;
    cin >> wordx >> wordy;
    int L[1010][1010] = {0};

    for (int j = 0; j < wordy.size(); j++) {
      for (int i = 0; i < wordx.size(); i++) {
        if (wordx[i] == wordy[j]) {
          L[i + 1][j + 1] = 1 + L[i][j];
        } else {
          L[i + 1][j + 1] = max(L[i + 1][j], L[i][j + 1]);
        }
      }
    }
    cout << L[wordx.size()][wordy.size()] << endl;
  }
}
Problem p02235 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int p[1001][1001] = {0};
int main() {
  int i, j, q, e, l, z, m, u, n;
  char s[2001], c[2001];
  cin >> q;
  for (e = 0; e < q; e++) {
    scanf("%s %s", s, c);
    l = strlen(s);
    z = strlen(c);

    for (i = 0; i <= l; i++) {
      for (j = 0; j <= z; j++) {
        p[i][j] = 0;
      }
    }
    for (i = 1; i <= z; i++) {
      for (j = 1; j <= l; j++) {
        if (c[i - 1] == s[j - 1]) {
          m = 0;
          for (u = 0; u < i; u++) {
            for (n = 0; n < j; n++) {
              if (m < p[n][u])
                m = p[n][u];
            }
          }
          p[j][i] = m + 1;
        }
        p[j][i] = max(p[j][i], p[j - 1][i]);
      }
    }
    for (i = 1; i <= z; i++) {
      for (j = i + 1; j <= z; j++) {
        if (p[l][i] > p[l][j]) {
          m = p[l][i];
          p[l][i] = p[l][j];
          p[l][j] = m;
        }
      }
    }
    cout << p[l][z] << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int p[1001][1001] = {0};
int main() {
  int i, j, q, e, l, z, m, u, n;
  char s[2001], c[2001];
  cin >> q;
  for (e = 0; e < q; e++) {
    scanf("%s %s", s, c);
    l = strlen(s);
    z = strlen(c);

    for (i = 0; i <= l; i++) {
      for (j = 0; j <= z; j++) {
        p[i][j] = 0;
      }
    }
    for (i = 1; i <= z; i++) {
      for (j = 1; j <= l; j++) {
        if (c[i - 1] == s[j - 1])
          p[j][i] = p[j - 1][i - 1] + 1;
        else
          p[j][i] = max(p[j - 1][i], p[j][i - 1]);
      }
    }
    cout << p[l][z] << endl;
  }
  return 0;
}
Problem p02235 (C++)

Buggy submission (status: 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;
}

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 (status: Runtime Error):
// 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;
}

Fixed submission:
// 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;
}
Problem p02235 (C++)

Buggy submission (status: 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]);
    }
  }
  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;
}

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]);
    }
  }
  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;
}
Problem p02235 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
using namespace std;

int main() {
  int setN;
  cin >> setN;
  string A, B;

  for (int i = 0; i < setN; i++) {
    cin >> A;
    cin >> B;
    int L[1000][1000] = {};

    for (int j = 1; j < A.length() + 1; j++) {
      for (int l = 1; l < B.length() + 1; l++) {
        if (A[j - 1] == B[l - 1]) {
          L[j][l] = 1 + L[j - 1][l - 1];

        } else {
          L[j][l] = max(L[j - 1][l], L[j][l - 1]);
        }
      }
    }
    /*
    for(int j=0;j<A.length()+1;j++) {
            cout << endl;
            for(int l=0;l<B.length()+1;l++) {
                    cout << L[j][l] << " ";

            }
    }
    */

    cout << L[A.length()][B.length()] << endl;
  }
}

Fixed submission:
#include <iostream>
#include <string>
using namespace std;

int main() {
  int setN;
  cin >> setN;
  string A, B;

  for (int i = 0; i < setN; i++) {
    cin >> A;
    cin >> B;
    int L[2000][2000] = {};

    for (int j = 1; j < A.length() + 1; j++) {
      for (int l = 1; l < B.length() + 1; l++) {
        if (A[j - 1] == B[l - 1]) {
          L[j][l] = 1 + L[j - 1][l - 1];

        } else {
          L[j][l] = max(L[j - 1][l], L[j][l - 1]);
        }
      }
    }
    /*
    for(int j=0;j<A.length()+1;j++) {
            cout << endl;
            for(int l=0;l<B.length()+1;l++) {
                    cout << L[j][l] << " ";

            }
    }
    */

    cout << L[A.length()][B.length()] << endl;
  }
}
Problem p02235 (C++)

Buggy submission (status: Runtime Error):
#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;
  }
}


Fixed submission:
#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;
  }
}
Problem p02235 (C++)

Buggy submission (status: Runtime Error):
#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;
}

Fixed submission:
#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;
}
Problem p02235 (C++)

Buggy submission (status: Runtime Error):
#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;
}

Fixed submission:
#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;
}
Problem p02235 (C++)

Buggy submission (status: 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[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;
}


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[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;
}
Problem p02235 (C++)

Buggy submission (status: Runtime Error):
#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;
}


Fixed submission:
#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;
}
Problem p02235 (C++)

Buggy submission (status: 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[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;
  }
}

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[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;
  }
}
Problem p02235 (C++)

Buggy submission (status: Runtime Error):
#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;
}

Fixed submission:
#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;
}
Problem p02235 (C++)

Buggy submission (status: 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][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;
}

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 + 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;
}
Problem p02235 (C++)

Buggy submission (status: Runtime Error):
#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;
}

Fixed submission:
#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;
}
Problem p02235 (C++)

Buggy submission (status: 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);
      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;
}

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);
      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;
}
Problem p02235 (C++)

Buggy submission (status: 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[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;
}

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