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

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

Error: 0

Fixed submission:

#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;
}
Problem p02235 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "iostream"
#include "string.h"

using namespace std;

int L[1001][1001];

int lcs(char s1[], char s2[], int l1, int l2) {

  if (L[l1][l2] != 0)
    return L[l1][l2];

  if (l1 == 0 || l2 == 0)
    return 0;
  else if (s1[l1 - 1] == s2[l2 - 1])
    L[l1][l2] = lcs(s1, s2, l1 - 1, l2 - 1) + 1;
  else {
    int lsc1 = lcs(s1, s2, l1 - 1, l2);
    int lsc2 = lcs(s1, s2, l1, l2 - 1);

    if (lsc1 > lsc2)
      L[l1][l2] = lsc1;
    else
      L[l1][l2] = lsc2;
  }

  return L[l1][l2];
}

int main() {

  int n;
  char s1[1000], s2[1000];

  cin >> n;

  int ans[150];

  for (int i = 0; i < n; i++) {
    cin >> s1;
    cin >> s2;

    int l1 = strlen(s1), l2 = strlen(s2);

    ans[i] = lcs(s1, s2, l1, l2);

    for (int j = 0; j <= l1; j++) {
      for (int k = 0; k <= l2; k++) {
        L[j][k] = 0;
      }
    }
  }

  for (int i = 0; i < n; i++) {
    cout << ans[i] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include "iostream"
#include "string.h"

using namespace std;

int L[1001][1001];

int lcs(char s1[], char s2[], int l1, int l2) {

  if (L[l1][l2] != 0)
    return L[l1][l2];

  if (l1 == 0 || l2 == 0)
    return 0;
  else if (s1[l1 - 1] == s2[l2 - 1])
    L[l1][l2] = lcs(s1, s2, l1 - 1, l2 - 1) + 1;
  else {
    int lsc1 = lcs(s1, s2, l1 - 1, l2);
    int lsc2 = lcs(s1, s2, l1, l2 - 1);

    if (lsc1 > lsc2)
      L[l1][l2] = lsc1;
    else
      L[l1][l2] = lsc2;
  }

  return L[l1][l2];
}

int main() {

  int n;
  char s1[1000], s2[1000];

  cin >> n;

  int ans[150];

  for (int i = 0; i < n; i++) {
    cin >> s1;
    cin >> s2;

    ans[i] = lcs(s1, s2, strlen(s1), strlen(s2));

    for (int j = 0; j <= strlen(s1); j++) {
      for (int k = 0; k <= strlen(s2); k++) {
        L[j][k] = 0;
      }
    }
  }

  for (int i = 0; i < n; i++) {
    cout << ans[i] << endl;
  }

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

///
// File:  alds1_10_c.cpp
// Author: ymiyamoto
//
// Created on Tue Oct 17 01:36:16 2017
//

#include <cstdint>
#include <iostream>
#include <string>

using namespace std;
static uint32_t dp[1001][1001];

static void init_dp(uint32_t len1, uint32_t len2) {
  for (uint32_t i = 0; i < len1; i++) {
    dp[i][0] = 0;
  }
  for (uint32_t i = 0; i < len2; i++) {
    dp[0][i] = 0;
  }
}

int32_t main() {
  uint32_t n;
  cin >> n;

  for (uint32_t i = 0; i < n; i++) {
    string str1, str2;
    cin >> str1;
    cin >> str2;
    init_dp(str1.size(), str2.size());
    for (uint32_t i = 1; i <= str1.size(); i++) {
      for (uint32_t j = 1; j <= str2.size(); j++) {
        if (str1[i - 1] == str2[j - 1]) {
          dp[i][j] = dp[i - 1][j - 1] + 1;
        } else {
          dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
        }
      }
    }
    cout << dp[str1.size()][str2.size()] << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

///
// File:  alds1_10_c.cpp
// Author: ymiyamoto
//
// Created on Tue Oct 17 01:36:16 2017
//

#include <cstdint>
#include <iostream>
#include <string>

using namespace std;
static uint32_t dp[1000][1000];

static void init_dp(uint32_t len1, uint32_t len2) {
  for (uint32_t i = 0; i < len1; i++) {
    dp[i][0] = 0;
  }
  for (uint32_t i = 0; i < len2; i++) {
    dp[0][i] = 0;
  }
}

int32_t main() {
  uint32_t n;
  cin >> n;

  for (uint32_t i = 0; i < n; i++) {
    string str1, str2;
    cin >> str1;
    cin >> str2;
    init_dp(str1.size(), str2.size());
    for (uint32_t i = 1; i <= str1.size(); i++) {
      for (uint32_t j = 1; j <= str2.size(); j++) {
        if (str1[i - 1] == str2[j - 1]) {
          dp[i][j] = dp[i - 1][j - 1] + 1;
        } else {
          dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
        }
      }
    }
    cout << dp[str1.size()][str2.size()] << endl;
  }

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

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#define NIL -1
using namespace std;

int count(const string &X, int i, const string Y, int j,
          vector<vector<int>> &MEMO) {
  if (i >= X.size() || j >= Y.size())
    return 0;
  if (MEMO[i][j] != NIL)
    return MEMO[i][j];
  int j_tmp = j;
  int saiyou = 0;
  while (j_tmp < Y.size()) {
    if (X[i] == Y[j_tmp]) {
      saiyou = count(X, i + 1, Y, j_tmp + 1, MEMO) + 1;
      break;
    };
    j_tmp++;
  }
  int next_X = count(X, i + 1, Y, j, MEMO);
  int next_Y = count(X, i, Y, j + 1, MEMO);
  return MEMO[i][j] = max({saiyou, next_X, next_Y});
}

int main() {
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    string X, Y;
    cin >> X >> Y;
    vector<vector<int>> MEMO(X.size(), vector<int>(Y.size(), NIL));
    cout << count(X, 0, Y, 0, MEMO) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#define NIL -1
using namespace std;

int count(const string &X, int i, const string Y, int j,
          vector<vector<int>> &MEMO) {
  if (i >= X.size() || j >= Y.size())
    return 0;
  if (MEMO[i][j] != NIL)
    return MEMO[i][j];
  int next_Y = count(X, i, Y, j + 1, MEMO);
  int next_X = count(X, i + 1, Y, j, MEMO);
  int max_ = max(next_Y, next_X);
  int j_tmp = j;
  while (X[i] != Y[j_tmp]) {
    j_tmp++;
    if (j_tmp >= Y.size())
      return max_;
  }
  int saiyou = count(X, i + 1, Y, j_tmp + 1, MEMO) + 1;
  if (max_ < saiyou)
    max_ = saiyou;
  return MEMO[i][j] = max_;
}

int main() {
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    string X, Y;
    cin >> X >> Y;
    vector<vector<int>> MEMO(X.size(), vector<int>(Y.size(), NIL));
    cout << count(X, 0, Y, 0, MEMO) << endl;
  }
  return 0;
}
Problem p02235 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int dat[1024][1024];
int getLcbLen(char *x, int xe, char *y, int ye) {
  if (xe <= 0 || ye <= 0)
    return 0;
  if (dat[xe][ye] != -1)
    return dat[xe][ye];
  return dat[xe][ye] =
             (x[xe - 1] == y[ye - 1] ? 1 + getLcbLen(x, xe - 1, y, ye - 1)
                                     : max(getLcbLen(x, xe - 1, y, ye),
                                           getLcbLen(x, xe, y, ye - 1)));
}

int main() {
  int set;
  scanf("%d", &set);

  while (set--) {
    char x[1024], y[1024];
    scanf("%s %s", x, y);

    for (int i = 0; i < 1024; i++) {
      for (int k = 0; k < 1024; k++) {
        dat[i][k] = -1;
      }
    }
    printf("%d\\n", getLcbLen(x, (int)strlen(x), y, (int)strlen(y)));
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int dat[1024][1024];
int getLcbLen(char *x, int xe, char *y, int ye) {
  cout << xe << ":" << ye << endl;
  if (xe <= 0 || ye <= 0)
    return 0;
  if (dat[xe][ye] != -1)
    return dat[xe][ye];
  return dat[xe][ye] =
             (x[xe - 1] == y[ye - 1] ? 1 + getLcbLen(x, xe - 1, y, ye - 1)
                                     : max(getLcbLen(x, xe - 1, y, ye),
                                           getLcbLen(x, xe, y, ye - 1)));
}

int main() {
  int set;
  scanf("%d", &set);

  while (set--) {
    char x[1024], y[1024];
    scanf("%s %s", x, y);

    for (int i = 0; i < 1024; i++) {
      for (int k = 0; i < 1024; k++) {
        dat[i][k] = -1;
      }
    }
    printf("%d\\n", getLcbLen(x, (int)strlen(x), y, (int)strlen(y)));
  }
  return 0;
}
Problem p02235 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <cstdio>
#define MAX 1002
using namespace std;

int Lcc(const char *X, const char *Y) {
  int lcc = 0;
  int dp[MAX][MAX];
  for (int i = 0; i < MAX; i++)
    for (int j = 0; j < MAX; j++)
      dp[i][j] = 0;

  int x = 1, y = 1;
  for (int i = 0;; i++) {
    if (X[i] == ',') {
      x = i;
      break;
    }
    for (int j = 0;; j++) {
      if (Y[j] == ',') {
        y = j;
        break;
      }
      if (Y[j] == ',') {
        y = j;
        break;
      } else {
        if (X[i] == Y[j]) {
          dp[i + 1][j + 1] = fmax(dp[i][j] + 1, dp[i + 1][j]);
        } else {
          dp[i + 1][j + 1] = fmax(dp[i + 1][j], dp[i][j + 1]);
        }
      }
    }
  }
  return dp[x - 1][y - 1];
}

int main() {
  int q;
  scanf("%d", &q);
  for (int i = 0; i < (q); i++) {
    char X[MAX], Y[MAX];
    for (int i = 0; i < MAX; i++) {
      X[i] = ',';
      Y[i] = ',';
    }
    scanf(" %s", X);
    scanf(" %s", Y);
    int LCC = Lcc(X, Y);
    printf("%d\\n", LCC);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cmath>
#include <cstdio>
#define MAX 1001
using namespace std;

int Lcc(const char *X, const char *Y) {
  int lcc = 0;
  int dp[MAX][MAX];
  for (int i = 0; i < MAX; i++)
    for (int j = 0; j < MAX; j++)
      dp[i][j] = 0;

  int x = 0, y = 0;
  for (int i = 0;; i++) {
    if (X[i] == ',') {
      x = i;
      break;
    }
    for (int j = 0;; j++) {
      if (Y[j] == ',') {
        y = j;
        break;
      } else {
        if (X[i] == Y[j]) {
          dp[i + 1][j + 1] = fmax(dp[i][j] + 1, dp[i + 1][j]);
        } else {
          dp[i + 1][j + 1] = fmax(dp[i + 1][j], dp[i][j + 1]);
        }
      }
    }
  }
  return dp[x - 1][y - 1];
}

int main() {
  int q;
  scanf("%d", &q);
  for (int i = 0; i < (q); i++) {
    char X[MAX], Y[MAX];
    for (int i = 0; i < MAX; i++) {
      X[i] = ',';
      Y[i] = ',';
    }
    scanf(" %s", X);
    scanf(" %s", Y);
    int LCC = Lcc(X, Y);
    printf("%d\\n", LCC);
  }
  return 0;
}
Problem p02235 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int q;
string X, Y;
int L[1001][1001];

int max(int i, int j) { return i > j ? i : j; }

int getL(int i, int j) {
  if (L[i][j] != -1)
    return L[i][j];

  int res;

  if (i == 0 || j == 0)
    res = 0;
  else if (X[i - 1] == Y[j - 1])
    res = 1 + getL(i - 1, j - 1);
  else
    res = max(getL(i, j - 1), getL(i - 1, j));

  L[i][j] = res;
  return res;
}

int main() {

  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> X >> Y;
    for (int j = 0; j <= X.size(); j++) {
      for (int k = 0; k <= Y.size(); k++)
        L[j][k] = -1;
    }
    cout << getL(X.size(), Y.size()) << endl;
  }
}

Error: TLE

Fixed submission:

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

int q;
string X, Y;
int L[1001][1001];

int max(int i, int j) { return i > j ? i : j; }

int getL(int i, int j) {
  if (L[i][j] != -1)
    return L[i][j];

  if (i == 0 || j == 0)
    return 0;
  if (X[i - 1] == Y[j - 1])
    return 1 + getL(i - 1, j - 1);
  return max(getL(i, j - 1), getL(i - 1, j));
}

int main() {

  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> X >> Y;
    for (int j = 0; j <= X.size(); j++) {
      for (int k = 0; k <= Y.size(); k++)
        L[j][k] = -1;
    }
    cout << getL(X.size(), Y.size()) << endl;
  }
}
Problem p02235 (C++) — buggy submission, verdict: Runtime Error

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 1001
void solve(void);
int q, count[MAX][MAX] = {}, xlength, ylength;
char X[MAX], Y[MAX];

void solve(void) {
  int i, j;

  xlength = strlen(X);
  ylength = strlen(Y);

  for (i = 1; i <= xlength; i++) {
    for (j = 1; j <= ylength; j++) {
      if (X[i - 1] == Y[j - 1])
        count[i][j] = count[i - 1][j - 1] + 1;
      else if (count[i - 1][j] >= count[i][j - 1])
        count[i][j] = count[i - 1][j];
      else
        count[i][j] = count[i][j - 1];
    }
  }
}

int main() {
  int i;

  scanf("%d", &q);

  for (i = 1; i <= q; i++) {
    scanf("%s", X);
    scanf("%s", Y);
    solve();
    printf("%d\\n", count[xlength][ylength]);
  }

  /*for(i=0;i<q;i++){
    printf("%d\\n",count[][]);
    }*/

  return 0;
}

Error: 0

Fixed submission:

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 1000
void solve(void);
int q, count[MAX][MAX] = {}, xlength, ylength;
char X[MAX], Y[MAX];

void solve(void) {
  int i, j;

  xlength = strlen(X);
  ylength = strlen(Y);

  for (i = 1; i <= xlength; i++) {
    for (j = 1; j <= ylength; j++) {
      if (X[i - 1] == Y[j - 1])
        count[i][j] = count[i - 1][j - 1] + 1;
      else if (count[i - 1][j] >= count[i][j - 1])
        count[i][j] = count[i - 1][j];
      else
        count[i][j] = count[i][j - 1];
    }
  }
}

int main() {
  int i;

  scanf("%d", &q);

  for (i = 1; i <= q; i++) {
    scanf("%s", X);
    scanf("%s", Y);
    solve();
    printf("%d\\n", count[xlength][ylength]);
  }

  /*for(i=0;i<q;i++){
    printf("%d\\n",count[][]);
    }*/

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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
using namespace std;
int dp[1001][1001];
int solve(string a, string b) {

  int maxn = 0;
  int len1 = a.size(), len2 = b.size();

  for (int i = 1; i <= len1; i++)
    for (int j = 1; j <= len2; j++) {
      if (a[i - 1] == b[j - 1])
        dp[i][j] = dp[i - 1][j - 1] + 1;
      else
        dp[i][j] = max(
            dp[i - 1][j],
            dp[i][j - 1]); // 这个只是表示某个状态的不是最大的，这个是自己设定的
      maxn = max(maxn, dp[i][j]);
    }
  return maxn;
}
int main() {
  int n;
  string a, b;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a >> b;
    cout << solve(a, b) << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
int dp[1001][1001];
int n;
int strlen_1(char a[]) {
  int len = 0;
  while (a[len] != '\\0')
    len++;
  return len;
}
int solve(char a[], char b[]) {

  int maxn = 0;
  int len1 = strlen_1(a), len2 = strlen_1(b);

  for (int i = 1; i <= strlen_1(a); i++)
    for (int j = 1; j <= strlen_1(b); j++) {
      if (a[i - 1] == b[j - 1])
        dp[i][j] = dp[i - 1][j - 1] + 1;
      else
        dp[i][j] = max(
            dp[i - 1][j],
            dp[i][j - 1]); // 这个只是表示某个状态的不是最大的，这个是自己设定的
      maxn = max(maxn, dp[i][j]);
    }
  return maxn;
}
char a[1001], b[1001];
int main() {

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a >> b;
    cout << solve(a, b) << endl;
  }
  return 0;
}
Problem p02235 (C++) — buggy submission, verdict: Runtime Error

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

const int s = 1001;

using namespace std;

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

  int q;
  static unsigned int dp[s][s] = {0};

  dp[0][0] = 0;
  dp[0][1] = 0;
  dp[1][0] = 0;

  cin >> q;
  for (int k = 0; k < q; k++) {
    string X, Y;
    cin >> X >> Y;
    int i, j;
    for (i = 0; i < X.size(); i++) {
      for (j = 0; j < Y.size(); j++) {
        if (X[i] == Y[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[i][j] << "\\n";
  }
  return 0;
}


Error: 0

Fixed submission:

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

const int s = 1000;

using namespace std;

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

  int q;
  string X, Y;
  static unsigned int dp[s][s] = {0};

  dp[0][0] = 0;
  dp[0][1] = 0;
  dp[1][0] = 0;

  cin >> q;
  for (int k = 0; k < q; k++) {
    cin >> X >> Y;
    int i, j;
    for (i = 0; i < X.size(); i++) {
      for (j = 0; j < Y.size(); j++) {
        if (X[i] == Y[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[i][j] << "\\n";
  }
  return 0;
}
Problem p02235 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
#define MAX 1000

int solve(string x, string y) {
  int xl = x.size();
  int yl = y.size();
  int lcs[MAX + 1][MAX + 1];

  int i, j;
  for (i = 0; i < xl; i++)
    lcs[i][0] = 0;
  for (j = 0; j < yl; j++)
    lcs[0][j] = 0;

  // shift
  x = " " + x;
  y = " " + y;

  int maxlcs = 0;
  for (i = 1; i <= xl; i++) {
    for (j = 1; j <= yl; j++) {
      if (x[i] == y[j]) {
        lcs[i][j] = lcs[i - 1][j - 1] + 1;
      } else {
        int a = lcs[i][j - 1];
        int b = lcs[i - 1][j];
        lcs[i][j] = max(a, b);
      }
      maxlcs = max(maxlcs, lcs[i][j]);
    }
  }
  return maxlcs;
}

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
#define MAX 1000

int solve(string x, string y) {
  int lcs[MAX + 1][MAX + 1];
  int xl = x.size();
  int yl = y.size();

  int i, j;
  for (i = 0; i < xl; i++)
    lcs[i][0] = 0;
  for (j = 0; i < yl; j++)
    lcs[0][j] = 0;

  // shift
  x = " " + x;
  y = " " + y;

  int maxlcs = 0;
  for (i = 1; i <= xl; i++) {
    for (j = 1; j <= yl; j++) {
      if (x[i] == y[j]) {
        lcs[i][j] = lcs[i - 1][j - 1] + 1;
      } else {
        int a = lcs[i][j - 1];
        int b = lcs[i - 1][j];
        lcs[i][j] = max(a, b);
      }
      maxlcs = max(maxlcs, lcs[i][j]);
    }
  }
  return maxlcs;
}

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

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

// ---------------------
// repetition
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

// ---------------------
#define INF 922337203685477580
typedef long long ll;

int dp[1010][1010];

int main() {
  int q;
  cin >> q;
  REP(loop, q) {
    char S[1000], T[1000];
    cin >> S >> T;

    REP(i, strlen(S)) {
      REP(j, strlen(T)) { dp[i][j] = 0; }
    }
    for (int i = 0; i < strlen(S); i++) {
      for (int j = 0; j < strlen(T); j++) {
        if (S[i] == T[j]) {
          dp[i + 1][j + 1] = dp[i][j] + 1;
        } else {
          dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]);
        }
      }
    }
    cout << dp[strlen(S)][strlen(T)] << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

// ---------------------
// repetition
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

// ---------------------
#define INF 922337203685477580
typedef long long ll;

int dp[1000][1000];

int main() {
  int q;
  cin >> q;
  REP(loop, q) {
    char S[1000], T[1000];
    cin >> S >> T;

    REP(i, strlen(S)) {
      REP(j, strlen(T)) { dp[i][j] = 0; }
    }
    for (int i = 0; i < strlen(S); i++) {
      for (int j = 0; j < strlen(T); j++) {
        if (S[i] == T[j]) {
          dp[i + 1][j + 1] = dp[i][j] + 1;
        } else {
          dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]);
        }
      }
    }
    cout << dp[strlen(S)][strlen(T)] << endl;
  }

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

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

Error: 0

Fixed submission:

#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;
}
Problem p02235 (C++) — buggy submission, verdict: Runtime Error

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

int N, len1, len2;
string seq1, seq2;
int dp[1000 + 10][1000 + 10];

int main() {
  cin >> N;
  for (int M = 0; M < N; M++) {
    cin >> seq1 >> seq2;
    len1 = seq1.length();
    len2 = seq2.length();
    for (int i = 1; i <= len1; i++) {
      for (int j = 1; j <= len2; j++) {
        if (seq1[i - 1] == seq2[j - 1]) {
          dp[i][j] = dp[i - 1][j - 1] + 1;
        } else if (dp[i][j - 1] > dp[i - 1][j]) {
          dp[i][j] = dp[i][j - 1];
        } else {
          dp[i][j] = dp[i - 1][j];
        }
      }
    }
    cout << dp[len1][len2] << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

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

int N, len1, len2;
string seq1, seq2;
int dp[1000][1000];

int main() {
  cin >> N;
  for (int M = 0; M < N; M++) {
    cin >> seq1 >> seq2;
    len1 = seq1.length();
    len2 = seq2.length();
    for (int i = 1; i <= len1; i++) {
      for (int j = 1; j <= len2; j++) {
        if (seq1[i - 1] == seq2[j - 1]) {
          dp[i][j] = dp[i - 1][j - 1] + 1;
        } else if (dp[i][j - 1] > dp[i - 1][j]) {
          dp[i][j] = dp[i][j - 1];
        } else {
          dp[i][j] = dp[i - 1][j];
        }
      }
    }
    cout << dp[len1][len2] << endl;
  }

  return 0;
}