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


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

#define MOD 1000000007

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

char a[4010], b[4010];
int dp[4010][4010];

int main() {
  while (cin >> a) {
    cin >> b;
    int len_a = strlen(a);
    int len_b = strlen(b);
    for (int i = 0; i <= len_a; i++)
      for (int j = 0; j <= len_b; j++)
        dp[i][j] = 0;
    for (int i = 0; i < len_a; i++) {
      for (int j = 0; j < len_b; j++) {
        if (a[i] == b[j])
          dp[i + 1][j + 1] = dp[i][j] + 1;
      }
    }
    int ans = 0;
    for (int i = 0; i <= len_a; i++)
      for (int j = 0; j <= len_b; j++)
        ans = max(ans, dp[i][j]);
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:



#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define MOD 1000000007

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

string s1, s2;

int main() {
  while (cin >> s1 >> s2) {
    set<string> s;
    string s3;

    for (int i = 0; i < s1.size(); i++) {
      for (int j = i; j < s1.size(); j++) {
        for (int k = i; k < j + 1; k++) {
          s3 += s1[k];
        }
        s.insert(s3);
        s3 = "";
      }
    }
    int ans = 0;
    for (set<string>::iterator itr = s.begin(); itr != s.end(); ++itr) {
      int pos = s2.find(*itr);
      string s3 = *itr;
      int si = s3.size();
      if (pos != -1)
        ans = max(ans, si);
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00451 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define reps(i, n) for (int i = 1; i <= int(n); i++)
#define sint(i) scanf("%d", &i);
#define sintt(i, j) scanf("%d%d", &i, &j);
#define sinttt(i, j, k) scanf("%d%d%d", &i, &j, &k);
#define sintttt(i, j, k, m) scanf("%d%d%d%d", &i, &j, &k, &m);

#define INF 1010000000
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

short dp[4001][4001];

int main() {
  while (1) {
    string a, b;
    cin >> a >> b;

    if (b.size() == 0) {
      break;
    }

    rep(i, 4001) rep(j, 4001) dp[i][j] = 0;

    short maxi = 0;
    reps(i, a.size()) {
      reps(j, b.size()) {
        if (a[i - 1] == b[j - 1]) {
          dp[i][j] = dp[i - 1][j - 1] + 1;
        }
        maxi = max(maxi, dp[i][j]);
      }
    }

    printf("%d\\n", maxi);
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define reps(i, n) for (int i = 1; i <= int(n); i++)
#define sint(i) scanf("%d", &i);
#define sintt(i, j) scanf("%d%d", &i, &j);
#define sinttt(i, j, k) scanf("%d%d%d", &i, &j, &k);
#define sintttt(i, j, k, m) scanf("%d%d%d%d", &i, &j, &k, &m);

#define INF 1010000000
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

int dp[4001][4001];

int main() {
  while (1) {
    string a, b;
    cin >> a >> b;

    if (b.size() == 0) {
      break;
    }

    rep(i, 4001) rep(j, 4001) dp[i][j] = 0;

    int maxi = 0;
    reps(i, a.size()) {
      reps(j, b.size()) {
        if (a[i - 1] == b[j - 1]) {
          dp[i][j] = dp[i - 1][j - 1] + 1;
        }
        maxi = max(maxi, dp[i][j]);
      }
    }

    printf("%d\\n", maxi);
  }
}
Problem p00451 (C++) — buggy submission, verdict: Memory Limit Exceeded

#define _USE_MATH_DEFINES
#define INF 100000000
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

static const double EPS = 1e-8;

short dp[4001][4001];

int main() {
  string str1;
  while (cin >> str1) {
    memset(dp, 0, sizeof(dp));

    string str2;
    cin >> str2;

    for (int i = 0; i < str1.size(); i++) {
      for (int j = 0; j < str2.size(); j++) {
        if (str1[i] == str2[j]) {
          dp[i + 1][j + 1] = dp[i][j] + 1;
        } else {
          dp[i + 1][j + 1] = 0;
        }
      }
    }

    short res = 0;
    for (int i = 0; i <= str1.size(); i++) {
      for (int j = 0; j <= str2.size(); j++) {
        res = max(res, dp[i][j]);
      }
    }

    printf("%d\\n", res);
  }
}

Error: MLE

Fixed submission:

#define _USE_MATH_DEFINES
#define INF 100000000
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

static const double EPS = 1e-8;

int dp[4001][4001];

int main() {
  string str1;
  while (cin >> str1) {
    memset(dp, 0, sizeof(dp));

    string str2;
    cin >> str2;

    for (int i = 0; i < str1.size(); i++) {
      for (int j = 0; j < str2.size(); j++) {
        if (str1[i] == str2[j]) {
          dp[i + 1][j + 1] = dp[i][j] + 1;
        } else {
          dp[i + 1][j + 1] = 0;
        }
      }
    }

    int res = 0;
    for (int i = 0; i < str1.size(); i++) {
      for (int j = 0; j < str2.size(); j++) {
        res = max(res, dp[i][j]);
      }
    }

    printf("%d\\n", res);
  }
}
Problem p00451 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

int main() {
  int dp[4001];
  string a, b;
  while (cin >> a >> b) {
    int ans = 0;
    memset(dp, 0, sizeof(dp));
    for (int i = 0; i < a.size(); i++) {
      for (int j = b.size() - 1; j >= 0; j--) {
        if (a[i] == b[j]) {
          ans = max(ans, dp[j + 1] = dp[j] + 1);
        }
        dp[j] = 0;
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
#define INF (1 << 29)
#define rep(i, n) for (int i = 0; i < (n); i++)

int dp[4001][4001];
int main() {
  string s, t;
  while (cin >> s >> t) {
    int ans = 0;
    rep(i, s.size() + 1) rep(j, t.size() + 1) dp[i][j] = 0;
    for (int i = 1; i <= s.size(); i++) {
      for (int j = 1; j <= t.size(); j++) {
        if (s[i - 1] == t[j - 1]) {
          dp[i][j] = dp[i - 1][j - 1] + 1;
          ans = max(ans, dp[i][j]);
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00451 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

int main() {

  string s1, s2;
  while (cin >> s1 >> s2) {
    int maxLength = 0;
    // ツづづェツつセツつッツつクツづァツつキツつゥ
    for (int i = -(int)s1.size(); i <= (int)s2.size(); i++) {
      // ツ債。ツづ個つクツづァツつオツつスツ湘ウツ妥板づ個淞催オツづーツ渉堕つッツづゥ
      int cnt = 0;
      for (int j = 0; j < s1.size(); j++) {
        if (i + j >= 0 && i + j < s2.size()) {
          if (s1[j] == s2[i + j])
            cnt++;
          else {
            maxLength = max(maxLength, cnt);
            cnt = 0;
          }
        }
      }
      maxLength = max(maxLength, cnt);
    }

    // for(int i = 0; i < str[0].size(); i++){
    //	for(int j = 0; j < str[1].size(); j++){
    //		if(str[0][i] == str[1][j]){
    //			int length = 1;
    //			for(int k = 1; k + i < str[0].size() && k + j < str[1].size();
    //k++){ 				if(str[0][k+i] == str[1][k+j]) 					length++; 				else 					break;
    //			}
    //			maxLength = max(maxLength,length);
    //		}
    //	}
    // }
    cout << maxLength << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

int main() {

  string str[2];
  while (cin >> str[0] >> str[1]) {
    int maxLength = 0;
    for (int i = 0; i < str[0].size(); i++) {
      for (int j = 0; j < str[1].size(); j++) {
        if (str[0][i] == str[1][j]) {
          int length = 1;
          for (int k = 1; k + i < str[0].size() && k + j < str[1].size(); k++) {
            if (str[0][k + i] == str[1][k + j])
              length++;
            else
              break;
          }
          maxLength = max(maxLength, length);
        }
      }
    }
    cout << maxLength << endl;
  }

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

#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++)

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};
string str1;
string str2;
int main() {
  while (cin >> str1 >> str2) {
    int ans = 0;
    for (int i = 0; i < str1.length(); i++) {
      for (int j = 0; j < str1.length(); j++) {
        if (i > 0 && j > 0 && str1[i - 1] == str2[j - 1])
          continue;
        int tmp = 0;
        for (int k = 0; k + i < str1.length() && k + j < str2.length(); k++) {
          if (str1[i + k] != str2[j + k]) {
            break;
          }
          tmp++;
        }
        ans = max(ans, tmp);
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

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++)

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};
string str1;
string str2;
int main() {
  while (cin >> str1 >> str2) {
    int ans = 0;
    for (int i = 0; i < str1.length(); i++) {
      for (int j = 0; j < str1.length(); j++) {
        int tmp = 0;
        for (int k = 0; k + i < str1.length() && k + j < str2.length(); k++) {
          if (str1[i + k] != str2[j + k]) {
            break;
          }
          tmp++;
        }
        ans = max(ans, tmp);
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00451 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

int main() {
  string s, t;
  while (cin >> s >> t) {
    short int ans = 0;
    vector<vector<short int>> dp(s.size() + 1,
                                 vector<short int>(t.size() + 1, 0));
    for (unsigned int i = 0; i < s.size(); ++i) {
      for (unsigned int j = 0; j < t.size(); ++j) {
        if (s[i] == t[j]) {
          dp[i + 1][j + 1] = dp[i][j] + 1;
          ans = max(ans, dp[i + 1][j + 1]);
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

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

int main() {
  string s, t;
  while (cin >> s >> t) {
    int ans = 0;
    vector<vector<int>> dp(s.size() + 1, vector<int>(t.size() + 1, 0));
    for (unsigned int i = 0; i < s.size(); ++i) {
      for (unsigned int j = 0; j < t.size(); ++j) {
        if (s[i] == t[j]) {
          dp[i + 1][j + 1] = dp[i][j] + 1;
          ans = max(ans, dp[i + 1][j + 1]);
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00451 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>

using namespace std;

short dp[4001][4001];

int main() {
  for (;;) {
    string s, t;

    if (!(cin >> s >> t)) {
      break;
    }

    for (int i = 0; i <= s.size(); i++) {
      dp[i][0] = 0;
    }

    for (int j = 0; j <= t.size(); j++) {
      dp[0][j] = 0;
    }

    for (int i = 1; i <= s.size(); i++) {
      for (int j = 1; j <= t.size(); j++) {
        if (s[i - 1] == t[j - 1]) {
          dp[i][j] = dp[i - 1][j - 1] + 1;
        } else {
          dp[i][j] = 0;
        }
      }
    }
    int ans = 0;

    for (int i = 0; i <= s.size(); i++) {
      for (int j = 0; j <= t.size(); j++) {
        if (dp[i][j] > ans) {
          ans = dp[i][j];
        }
      }
    }

    cout << ans << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#include <string>

using namespace std;

short dp[4000][4000];

int main() {
  for (;;) {
    string s, t;

    if (!(cin >> s >> t)) {
      break;
    }

    for (int i = 0; i <= s.size(); i++) {
      dp[i][0] = 0;
    }

    for (int j = 0; j <= t.size(); j++) {
      dp[0][j] = 0;
    }

    for (int i = 1; i <= s.size(); i++) {
      for (int j = 1; j <= t.size(); j++) {
        if (s[i - 1] == t[j - 1]) {
          dp[i][j] = dp[i - 1][j - 1] + 1;
        } else {
          dp[i][j] = 0;
        }
      }
    }
    int ans = 0;

    for (int i = 0; i <= s.size(); i++) {
      for (int j = 0; j <= t.size(); j++) {
        if (dp[i][j] > ans) {
          ans = dp[i][j];
        }
      }
    }

    cout << ans << endl;
  }
  return 0;
}
Problem p00451 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  while (true) {
    string s, t;
    cin >> s;
    if (!s.size())
      break;
    cin >> t;
    int dp1[4001] = {0}, dp2[4001] = {0}, mx = 0;
    for (int i = 0; i < s.size(); i++) {
      for (int j = 0; j < t.size(); j++) {
        if (s[i] == t[j]) {
          dp2[j + 1] = dp1[j] + 1;
          mx = max(mx, dp2[j + 1]);
        }
      }
      for (int j = 0; j <= t.size(); j++) {
        dp1[j] = dp2[j];
        dp2[j] = 0;
      }
    }
    cout << mx << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

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

int main() {
  while (true) {
    string s, t;
    cin >> s;
    if (!s.size())
      break;
    cin >> t;
    int dp[4001][4001] = {0}, mx = 0;
    for (int i = 1; i <= s.size(); i++) {
      for (int j = 1; j <= t.size(); j++) {
        if (s[i] == t[j]) {
          dp[i][j] = dp[i - 1][j - 1] + 1;
          mx = max(mx, dp[i][j]);
        }
      }
    }
    cout << mx << endl;
  }
  return 0;
}
Problem p00451 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
#define MOD 1000000007
#define INF 11234567890
#define in std::cin
#define out std::cout
#define rep(i, N) for (int i = 0; i < N; ++i)
typedef long long int LL;

std::string s, t;
int dp[4123][4123];
int ans;

int main() {
  while (true) {
    s.clear();
    t.clear();
    memset(dp, 0, sizeof(dp));
    ans = 0;

    in >> s >> t;
    if (s.empty()) {
      break;
    }

    rep(i, s.length()) {
      rep(j, t.length()) {
        if (s[i] == t[j]) {
          dp[i + 1][j + 1] = dp[i][j] + 1;
        } else {
          dp[i + 1][j + 1] = 0;
        }
      }
    }
    rep(i, s.length() + 1) {
      rep(j, t.length() + 1) { ans = std::max(ans, dp[i][j]); }
    }

    out << ans << std::endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#define MOD 1000000007
#define INF 11234567890
#define in std::cin
#define out std::cout
#define rep(i, N) for (int i = 0; i < N; ++i)
typedef long long int LL;

std::string s, t;
int ans;

bool check(std::string strs) {
  rep(i, t.length() - strs.length() + 1) {
    const auto temp = t.substr(i, strs.length());
    if (strs == temp) {
      return true;
    }
  }
  return false;
}

int main() {
  while (true) {
    s.clear();
    t.clear();
    ans = 0;

    in >> s >> t;
    if (s.empty()) {
      break;
    }

    if (s.length() > t.length()) {
      std::swap(s, t);
    }
    rep(i, s.length()) {
      for (int j = 1; i + j < s.length(); ++j) {
        const auto temp = s.substr(i, j);
        if (check(temp)) {
          ans = std::max<int>(ans, temp.length());
        } else {
          break;
        }
      }
    }

    out << ans << std::endl;
  }
  return 0;
}
Problem p00451 (C++) — buggy submission, verdict: Runtime Error

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

std::string s1, s2;

int dp[4001][4001];

int main() {

  while (std::cin >> s1 >> s2) {

    std::memset(dp, 0, sizeof(dp));

    for (int i = 1; i <= s1.size(); ++i) {
      for (int j = 1; j <= s2.size(); ++j) {
        if (s1[i - 1] == s2[j - 1])
          dp[i][j] = dp[i - 1][j - 1] + 1;
      }
    }

    int ans = 0;

    for (int i = 1; i <= s1.size(); ++i) {
      for (int j = 1; j <= s2.size(); ++j) {
        ans = std::max(ans, dp[i][j]);
      }
    }

    std::cout << ans << std::endl;
  }

  return 0;
}

Error: WA

Fixed submission:

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

std::string s1, s2;

std::vector<std::string> dat;

int main() {

  while (std::cin >> s1 >> s2) {

    int ans = 0;

    for (int i = 0; i < s1.size(); ++i) {
      for (int j = i; j < s1.size(); ++j)
        dat.emplace_back(s1.substr(i, j - i + 1));
    }

    std::sort(dat.begin(), dat.end());

    for (int i = 0; i < s2.size(); ++i) {
      for (int j = i; j < s2.size(); ++j) {
        std::string tmp = s2.substr(i, j - i + 1);
        if (*(std::lower_bound(dat.begin(), dat.end(), tmp)) == tmp)
          ans = std::max(ans, j - i + 1);
      }
    }

    std::cout << ans << std::endl;
  }

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

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

int solve(const string &s1, const string &s2) {
  int max_idx1 = 0;
  int max_idx2 = 0;
  int max_len = 0;
  for (int i = -int(s1.size()); i <= int(s2.size()); i++) {
    int len = 0;
    for (int j = max(i, 0); j < int(min(s1.size() + i, s2.size())); j++) {
      if (s1[j - i] == s2[j]) {
        len++;
        if (len > max_len) {
          max_idx1 = j - i - len + 1;
          max_idx2 = j - len + 1;
          max_len = len;
        }
      } else {
        len = 0;
      }
    }
  }
  return max_len;
}

int main() {
  string s1, s2;
  while (cin >> s1 >> s2) {
    cout << solve(s1, s2) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

vector<int> make_kmp_table(const string &w) {
  vector<int> tbl(w.size());
  tbl[0] = -1;
  tbl[1] = 0;
  for (string::size_type i = 2, j = 0; i < w.size();) {
    if (w[i - 1] == w[j]) {
      tbl[i] = j + 1;
      i++;
      j++;
    } else if (j > 0) {
      j = tbl[j];
    } else {
      tbl[i] = 0;
      i++;
    }
  }
  return tbl;
}

int kmp(const string &s, const string &w) {
  const vector<int> tbl = make_kmp_table(w);
  for (string::size_type m = 0, i = 0; m + i < s.size();) {
    if (w[i] == s[m + i]) {
      i++;
      if (i == w.size()) {
        return m;
      }
    } else {
      m = m + i - tbl[i];
      if (i > 0) {
        i = tbl[i];
      }
    }
  }
  return -1;
}

int main() {
  string s1, s2;
  while (cin >> s1 >> s2) {
    int l = 0, r = min(s1.size(), s2.size()) + 1;
    while (l + 1 < r) {
      const int m = (l + r) / 2;
      for (int i = 0; i <= s1.size() - m; i++) {
        if (kmp(s2, s1.substr(i, m)) != -1) {
          l = m;
          goto NEXT;
        }
      }
      r = m;
    NEXT:;
    }
    cout << l << endl;
  }
  return 0;
}
Problem p00451 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <string>
#define N 4001
using namespace std;
int n, m, dp[N][N], ans;
int main() {
  string A, B;
  while (cin >> A >> B) {
    n = A.size();
    m = B.size();
    for (int i = 0; i < N; i++)
      for (int j = 0; j < N; j++)
        dp[i][j] = 0;
    ans = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (A[i] == B[j])
          dp[i + 1][j + 1] = dp[i][j] + 1;
        ans = max(ans, dp[i + 1][j + 1]);
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#define N 4000
using namespace std;
int n, m, dp[N][N], ans;
int main() {
  string A, B;
  while (cin >> A >> B) {
    n = A.size();
    m = B.size();
    for (int i = 0; i < N; i++)
      for (int j = 0; j < N; j++)
        dp[i][j] = 0;
    ans = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (A[i] == B[j])
          dp[i + 1][j + 1] = dp[i][j] + 1;
        ans = max(ans, dp[i + 1][j + 1]);
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00451 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cstring>
#include <iostream>
#include <string>
int dp[4001][4001];
using namespace std;
int main() {
  string s, str;
  int ans;
  while (cin >> s >> str) {
    ans = 0;
    memset(dp, 0, sizeof(dp));
    for (int i = 0, l = s.size(); i < l; i++) {
      for (int j = 0, ll = str.size(); j < ll; j++) {
        if (s[i] == str[j]) {
          dp[i + 1][j + 1] = dp[i][j] + 1;
          ans = max(ans, dp[i + 1][j + 1]);
        } else
          dp[i + 1][j + 1] = 0;
      }
    }
    cout << ans << endl;
  }
}

Error: MLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <string>
int dp[5000][5000];
using namespace std;
int main() {
  string s, str;
  int ans = 0;
  while (cin >> s >> str) {
    memset(dp, 0, sizeof(dp));
    for (int i = 0, l = s.size(); i < l; i++) {
      for (int j = 0, ll = str.size(); j < ll; j++) {
        if (s[i] == str[j]) {
          dp[i + 1][j + 1] = dp[i][j] + 1;
          ans = max(ans, dp[i + 1][j + 1]);
        } else
          dp[i + 1][j + 1] = 0;
      }
    }
    cout << ans << endl;
  }
}
Problem p00451 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 100000005
#define MAX 5001
using namespace std;
//__gcd(a,b), __builtin_popcount(a);

int dp[4005][4005];
int main() {
  string s1, s2;
  while (cin >> s1 >> s2) {
    s1 = " " + s1;
    s2 = " " + s2;
    fill((int *)dp, (int *)(dp + 4005), 0);
    int ans = 0;
    for (int i = 1; i < s1.size(); i++) {
      for (int j = 1; j < s2.size(); j++) {
        if (s1[i] == s2[j])
          dp[i][j] = dp[i - 1][j - 1] + 1;
        ans = max(ans, dp[i][j]);
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 100000005
#define MAX 5001
using namespace std;
//__gcd(a,b), __builtin_popcount(a);

int dp[4005][4005];
int main() {
  string s1, s2;
  while (cin >> s1 >> s2, s1 != "") {
    s1 = " " + s1;
    s2 = " " + s2;
    fill((int *)dp, (int *)(dp + 4005), 0);
    int ans = 0;
    for (int i = 1; i <= s1.size(); i++) {
      for (int j = 1; j <= s2.size(); j++) {
        if (s1[i] == s2[j])
          dp[i][j] = dp[i - 1][j - 1] + 1;
        ans = max(ans, dp[i][j]);
      }
    }
    cout << ans << endl;
  }
  return 0;
}