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

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

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

  vector<string> v(N), w(N);
  for (int i = 0; i < N; ++i) {
    cin >> v[i];
    w[i] = v[i];
  }
  sort(w.begin(), w.end());

  for (int i = 0; i < N; ++i) {
    int win = lower_bound(w.begin(), w.end(), v[i]) - w.begin();
    int lose = w.end() - upper_bound(w.begin(), w.end(), v[i]);
    int draw = N - win - lose - 1;
    cout << win * 3 + draw << endl;
  }
  return 0;
}


Error: -11

Fixed submission:

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

int main() {
  int N;
  vector<string> v, w;

  while (cin >> N, N) {
    for (int i = 0; i < N; ++i) {
      cin >> v[i];
      w[i] = v[i];
    }
    sort(w.begin(), w.end());

    for (int i = 0; i < N; ++i) {
      int win = lower_bound(w.begin(), w.end(), v[i]) - w.begin();
      int lose = w.end() - upper_bound(w.begin(), w.end(), v[i]);
      int draw = N - win - lose - 1;
      cout << win * 3 + draw << endl;
    }
  }
  return 0;
}
Problem p02099 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;

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

  vector<pair<double, int>> GPA(N);
  for (int i = 0; i < N; i++) {
    double val;
    cin >> val;
    GPA[i] = make_pair(val, i);
  }
  sort(GPA.begin(), GPA.end());

  vector<int> ans(N);
  for (int i = 0; i < N; i++) {
    int id = GPA[i].second;

    int lb = lower_bound(GPA.begin(), GPA.end(), make_pair(GPA[i].first, -1)) -
             GPA.begin();
    int ub =
        upper_bound(GPA.begin(), GPA.end(), make_pair(GPA[i].first, N + 1)) -
        GPA.begin();

    // printf("lb = %d, ub = %d\\n", lb, ub);
    ans[id] = 3 * lb + (ub - lb - 1);
  }
  for (int i = 0; i < N; i++)
    cout << ans[i] << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

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

  vector<double> GPA(N);
  for (int i = 0; i < N; i++) {
    cin >> GPA[i];
  }

  vector<int> ans(N);
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      if (i == j)
        continue;
      if (GPA[i] > GPA[j])
        ans[i] += 3;
      else if (GPA[i] == GPA[j])
        ans[i] += 1;
    }
  }

  for (int i = 0; i < N; i++)
    cout << ans[i] << endl;
  return 0;
}
Problem p02235 (C++) — buggy submission, verdict: 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[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;
  }
}

Error: 0

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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <memory>
#include <string>
using namespace std;
#define MAX 1003

char A[MAX];
char B[MAX];

void print() {}
int DP[MAX][MAX];
int dp(int i, int j) {
  if (i < 0 || j < 0)
    return 0;
  if (DP[i][j] != -1)
    return DP[i][j];
  if (A[i] == B[j])
    return DP[i][j] = dp(i - 1, j - 1) + 1;
  return DP[i][j] = max(dp(i, j - 1), dp(i - 1, j));
}
void solve() {

  int tmp;
  scanf("%d", &tmp);
  int r, c;
  while (tmp--) {

    scanf("%s%s", A, B);
    r = strlen(A);
    c = strlen(B);
    memset(DP, -1, sizeof(DP));
    printf("%d\\n", dp(r - 1, c - 1));
  }
}

int main() {

  // freopen("in","r",stdin);
  solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
#define MAX(a, b) (a > b ? a : b)
using namespace std;
const int MAXN = 1010;
int dp[MAXN][MAXN];
char a[MAXN], b[MAXN];
int main() {
  int n;
  cin >> n;
  for (int k = 0; k < n; k++) {
    while (scanf("%s%s", a + 1, b + 1)) {
      memset(dp, 0, sizeof(dp));
      int i, j;
      for (i = 1; a[i]; i++) {
        for (j = 1; b[j]; j++) {
          if (a[i] == b[j])
            dp[i][j] = dp[i - 1][j - 1] + 1;
          else
            dp[i][j] = MAX(dp[i][j - 1], dp[i - 1][j]);
        }
      }
      printf("%d\\n", dp[i - 1][j - 1]);
    }
  }
  return 0;
}
Problem p02235 (C++) — buggy submission, verdict: 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])
          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;
}

Error: TLE

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

#include <cstdio>
#include <cstring>
#define N 1000

int LCS[N + 1][N + 1];

int LCSLength(char X[], int m, char Y[], int n) {
  for (int i = 0; i <= m; i++)
    LCS[i][n] = 0;
  for (int j = 0; j <= n; j++)
    LCS[m][j] = 0;
  for (int i = m - 1; i >= 0; i--) {
    for (int j = n - 1; j >= 0; j--) {
      LCS[i][j] = LCS[i + 1][j + 1];
      if (X[i] == Y[j])
        LCS[i][j]++;
      if (LCS[i][j + 1] > LCS[i][j])
        LCS[i][j] = LCS[i][j + 1];
      if (LCS[i + 1][j] > LCS[i][j])
        LCS[i][j] = LCS[i + 1][j];
    }
  }
  return LCS[0][0];
}

int main() {
  char X[N + 1], Y[N + 1];
  int num;
  scanf("%d", &num);
  for (int i = 1; i <= num; i++) {
    scanf("%s %s", X, Y);
    int m = strlen(X);
    int n = strlen(Y);
    printf("%d\\n", LCSLength(X, m, Y, n));
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <cstring>
#define N 1000

int LCS[N][N];

int LCSLength(char X[], int m, char Y[], int n) {
  for (int i = 0; i <= m; i++)
    LCS[i][n] = 0;
  for (int j = 0; j <= n; j++)
    LCS[m][j] = 0;
  for (int i = m - 1; i >= 0; i--) {
    for (int j = n - 1; j >= 0; j--) {
      LCS[i][j] = LCS[i + 1][j + 1];
      if (X[i] == Y[j])
        LCS[i][j]++;
      if (LCS[i][j + 1] > LCS[i][j])
        LCS[i][j] = LCS[i][j + 1];
      if (LCS[i + 1][j] > LCS[i][j])
        LCS[i][j] = LCS[i + 1][j];
    }
  }
  return LCS[0][0];
}

int main() {
  char X[N], Y[N];
  int num;
  scanf("%d", &num);
  for (int i = 1; i <= num; i++) {
    scanf("%s %s", X, Y);
    int m = strlen(X);
    int n = strlen(Y);
    printf("%d\\n", LCSLength(X, m, Y, n));
  }
  return 0;
}
Problem p02235 (C++) — buggy submission, verdict: 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[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;
  }
}

Error: 0

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

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

Error: -11

Fixed submission:

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

// -*- mode:c++; coding:utf-8; c-basic-offset:2; -*-
// ALDS1_10_C: Longest Common Subsequence

#include <algorithm>
#include <cstdio>
#include <cstring>

static unsigned int lcs(const char *x, const char *y) {
  auto m = static_cast<int>(std::strlen(x));
  auto n = static_cast<int>(std::strlen(y));
  unsigned int c[m + 1][n + 1]; // extend -1

  for (auto i = 0; i < m + 1; ++i)
    c[i][0] = 0u;
  for (auto j = 1; j < n + 1; ++j)
    c[0][j] = 0u;

  for (auto i = 0; i < m; ++i) {
    for (auto j = 0; j < n; ++j) {
      c[i + 1][j + 1] =
          (x[i] == y[j]) ? c[i][j] + 1 : std::max(c[i + 1][j], c[i][j + 1]);
    }
  }
  return c[m][n];
}

int main() {
  unsigned int q;
  std::scanf("%u", &q);
  const unsigned int MAX_LEN = 1000;
  char x[MAX_LEN + 1], y[MAX_LEN + 1];
  for (auto i = 0u; i < q; ++i) {
    std::scanf("%1000s", x);
    std::scanf("%1000s", y);
    auto l = lcs(x, y);
    std::printf("%u\\n", l);
  }
}

// eof

Error: 0

Fixed submission:

// -*- mode:c++; coding:utf-8; c-basic-offset:2; -*-
// ALDS1_10_C: Longest Common Subsequence

#include <algorithm>
#include <cstdio>
#include <cstring>

static unsigned int lcs(const char *x, const char *y) {
  auto m = static_cast<int>(std::strlen(x));
  auto n = static_cast<int>(std::strlen(y));
  unsigned int c0[m + 1][n + 1]; // extend -1
  auto const c = (c0 + 1) + 1;

  for (auto i = -1; i < m; ++i)
    c[i][-1] = 0;
  for (auto j = 0; j < n; ++j)
    c[-1][j] = 0;

  for (auto i = 0; i < m; ++i) {
    for (auto j = 0; j < n; ++j) {
      c[i][j] = (x[i] == y[j]) ? c[i - 1][j - 1] + 1
                               : std::max(c[i][j - 1], c[i - 1][j]);
    }
  }
  return c[m - 1][n - 1];
}

int main() {
  unsigned int q;
  std::scanf("%u", &q);
  const unsigned int MAX_LEN = 1000;
  char x[MAX_LEN + 1], y[MAX_LEN + 1];
  for (auto i = 0u; i < q; ++i) {
    std::scanf("%1000s", x);
    std::scanf("%1000s", y);
    std::printf("%u\\n", lcs(x, y));
  }
}

// eof
Problem p02235 (C++) — buggy submission, verdict: 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[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;
}

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


Error: 0

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

#include <iostream>
#include <string>

/*int LCS(std::string str1, std::string str2, int i, int j){
  if(str1[i] == str2[j]){
    return 1;
  }
  else{
    return 0;
  }
 }
*/

int max(int a, int b, int c) {
  int m = a;
  if (b > m) {
    m = b;
  }
  if (c > m) {
    m = c;
  }
  return m;
}

int main() {
  int q;
  std::cin >> q;
  // int ans[q];
  std::string str1, str2;
  for (int z = 0; z < q; z++) {
    std::cin >> str1 >> str2;
    int l1 = str1.size();
    int l2 = str2.size();
    int DP[l1 + 1][l2 + 1];

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

    for (int i = 1; i <= l1; i++) {
      for (int j = 1; j <= l2; j++) {
        if (str1[i - 1] == str2[j - 1]) {
          DP[i][j] = max(DP[i - 1][j], DP[i][j - 1], DP[i - 1][j - 1] + 1);
        } else {
          DP[i][j] = max(DP[i - 1][j], DP[i][j - 1], DP[i - 1][j - 1]);
        }
      }
    }
    // ans[z] = DP[l1][l2];
    std::cout << DP[l1][l2] << std::endl;
  }

  /*for(int z=0; z<q; z++){
    std::cout << ans[z] << std::endl;
    }*/
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <string>

int LCS(std::string str1, std::string str2, int i, int j) {
  if (str1[i] == str2[j]) {
    return 1;
  } else {
    return 0;
  }
}

int max(int a, int b, int c) {
  int m = a;
  if (b > m) {
    m = b;
  }
  if (c > m) {
    m = c;
  }
  return m;
}

int main() {
  int q;
  std::cin >> q;
  // int ans[q];
  std::string str1, str2;
  for (int z = 0; z < q; z++) {
    std::cin >> str1 >> str2;
    int l1 = str1.size();
    int l2 = str2.size();
    int DP[l1 + 1][l2 + 1];

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

    for (int i = 1; i <= l1; i++) {
      for (int j = 1; j <= l2; j++) {
        DP[i][j] = max(DP[i - 1][j], DP[i][j - 1],
                       DP[i - 1][j - 1] + LCS(str1, str2, i - 1, j - 1));
      }
    }
    // ans[z] = DP[l1][l2];
    std::cout << DP[l1][l2] << std::endl;
  }

  /*for(int z=0; z<q; z++){
    std::cout << ans[z] << std::endl;
    }*/
  return 0;
}