alexjercan/bugnet huggingface dataset:
Problem p00451 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string.h>
using namespace std;
short dp[4001][4001];
int main() {
  string s, t;

  while (cin >> s >> t) {
    memset(dp, 0, sizeof(dp));
    dp[0][0] = 0;
    int ans = 0;
    for (int i = 0; i < s.size(); i++) {
      for (int j = 0; j < t.size(); j++) {
        if (s[i] == t[j]) {
          dp[i + 1][j + 1] = dp[i][j] + 1;
          if (dp[i + 1][j + 1] > ans)
            ans = dp[i + 1][j + 1];
        } else {
          dp[i + 1][j + 1] = 0;
        }
      }
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;
int main() {
  string str, str2;
  while (cin >> str >> str2) {
    if (cin.eof())
      break;
    int ans = 0, t = 1;
    for (int i = 0; i < str.size() - 1; i++) {
      for (int j = t; i + j < str.size(); j++) {
        if (str2.find(str.substr(i, j)) != string::npos) {
          if (ans < j) {
            ans = j;
            t = ans;
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00451 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

int main(void) {
  int a, b;
  string str;
  string str2;
  while (cin >> str >> str2) {
    int ans = 0;
    for (a = 0; a < str.size(); a++) {
      int res = 0;
      string tmp = str.substr(a, str.size() - a);
      for (b = 0; b < min(tmp.size(), str2.size()); b++) {
        if (tmp[b] == str2[b])
          res++;
        else
          ans = max(res, ans), res = 0;
        if (min(tmp.size(), str2.size()) - 1 == b)
          ans = max(res, ans);
      }
    }
    for (a = 0; a < str2.size(); a++) {
      int res = 0;
      string tmp = str2.substr(a, str2.size() - a);
      for (b = 0; b < min(tmp.size(), str.size()); b++) {
        if (tmp[b] == str[b])
          res++;
        else
          ans = max(res, ans), res = 0;
        if (min(tmp.size(), str.size()) - 1 == b)
          ans = max(res, ans);
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

int main(void) {
  int a, b;
  while (1) {
    string str;
    string str2;
    int ans = 0;
    cin >> str;
    if (str == "EOF")
      break;
    cin >> str2;
    for (a = 0; a < str.size(); a++) {
      int res = 0;
      string tmp = str.substr(a, str.size() - a);
      for (b = 0; b < min(tmp.size(), str2.size()); b++) {
        if (tmp[b] == str2[b])
          res++;
        else
          ans = max(res, ans), res = 0;
        if (min(tmp.size(), str2.size()) - 1 == b)
          ans = max(res, ans);
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00451 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <stdio.h>
#include <string.h>
int main() {
  char a[4001], b[4001];
  while (scanf("%s%s", a, b) > 0) {
    static int x[2][4001], i, j, s, t, r, c;
    s = strlen(a), t = strlen(b);
    for (r = j = 0; j <= t; ++j)
      x[0][j] = 0;
    for (c = i = 0; i < s; ++i, c = !c)
      for (j = 0; j < t; ++j)
        r = (r > (x[!c][j + 1] = (a[i] == b[j] ? x[c][j] + 1 : 0))
                 ? r
                 : x[!c][j + 1]);
    printf("%d\\n", r);
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <stdio.h>
#include <string.h>
int main() {
  char a[4001], b[4001];
  while (scanf("%s%s", a, b) > 0) {
    static int x[4001][4001] = {0}, i, j, s, t, r;
    s = strlen(a), t = strlen(b);
    for (r = j = 0; j <= t; ++j)
      x[0][j] = 0;
    for (i = 0; i <= s; ++i)
      x[i][0] = 0;
    for (i = 0; i < s; ++i)
      for (j = 0; j < t; ++j)
        if (a[i] == b[j])
          r = (r > (x[i + 1][j + 1] = x[i][j] + 1) ? r : x[i + 1][j + 1]);
    printf("%d\\n", r);
  }
  return 0;
}
Problem p00451 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
string s[2];
int search(int id) {
  int id2 = (id + 1) % 2;
  int ret = 0;
  int a[40004];
  for (int j = (int)s[id2].size() - 1; j > -(int)s[id].size(); j--) {
    memset(a, 0, sizeof(a));
    for (int i = 0; i < (int)s[id].size(); i++) {
      if (i + j < 0)
        continue;
      if (i + j >= (int)s[id2].size())
        continue;
      if (s[id][i] == s[id2][i + j])
        a[i] = 1;
    }
    int co = 0;
    for (int i = 0; i < (int)s[id].size() + 1; i++) {
      if (a[i])
        co++;
      else {
        ret = max(ret, co);
        co = 0;
      }
    }
  }
  return ret;
}
int main() {
  while (cin >> s[0]) {
    cin >> s[1];
    printf("%d\\n", search(0));
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
string s[2];
int search(int id) {
  int id2 = (id + 1) % 2;
  int ret = 0;
  int a[40004];
  for (int j = (int)s[id2].size() - 1; j > -(int)s[id].size(); j--) {
    memset(a, 0, sizeof(a));
    for (int i = 0; i < (int)s[id].size(); i++) {
      if (i + j < 0)
        continue;
      if (i + j >= (int)s[id2].size())
        continue;
      if (s[id][i] == s[id2][i + j])
        a[i] = 1;
    }
    int co = 0;
    for (int i = 0; i < (int)s[id].size() + 1; i++) {
      if (a[i])
        co++;
      else {
        ret = max(ret, co);
        co = 0;
      }
    }
  }
  return ret;
}
int main() {
  while (cin >> s[0]) {
    cin >> s[1];
    printf("%d\\n", max(search(0), search(1)));
  }
}
Problem p00451 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  string str1, str2;
  while (cin >> str1 >> str2) {

    // int count = 0;
    // for(int i = 0; i < str1.size(); i++){
    // 	for(int j = 0; j < str2.size(); j++){
    // 		int k = 0;
    // 		while(str1[i+k] == str2[j+k] && i+k < str1.size() && j+k <
    // str2.size()){
    // 			//if(i + k > str1.size()-1 || j + k > str2.size()-1)
    // break; 			k++; 			count = max(count, k);
    // 			//cout << str2.substr(i, k) << endl;
    // 		}
    // 	}
    // }
    // cout << count << endl;

    vector<vector<int>> dp(str1.size() + 1, vector<int>(str2.size() + 1));
    for (int i = 0; i < str1.size() + 1; i++) {
      for (int j = 0; j < str2.size() + 1; j++) {
        dp[i][j] = 0;
      }
    }
    int ans = 0;
    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;
          ans = max(ans, dp[i + 1][j + 1]);
        }
      }
    }
    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  string str1, str2;
  while (cin >> str1) {
    cin >> str2;
    int count = 0;
    for (int i = 0; i < str1.size(); i++) {
      for (int j = 0; j < str2.size(); j++) {
        int k = 0;
        while (str1[i + k] == str2[j + k] && i + k < str1.size() &&
               j + k < str2.size()) {
          // if(i + k > str1.size()-1 || j + k > str2.size()-1) break;
          k++;
          count = max(count, k);
          // cout << str2.substr(i, k) << endl;
        }
      }
    }
    cout << count << endl;
  }
  return 0;
}
Problem p00451 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

using namespace std;

string A, B;
int lenA, lenB;

short dp[4096][4096];

int main() {
  while (cin >> A >> B) {
    lenA = A.size(), lenB = B.size();
    memset(dp, 0, sizeof(dp));
    for (int i = 0; i < lenA; i++) {
      for (int j = 0; j < lenB; j++) {
        if (A[i] == B[j])
          dp[i + 1][j + 1] = dp[i][j] + 1;
      }
    }
    short res = 0;
    for (int i = 0; i <= lenA; i++)
      for (int j = 0; j <= lenB; j++)
        res = max(res, dp[i][j]);
    cout << res << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

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

using namespace std;

string A, B;
int lenA, lenB;

int dp[4096][4096];

int main() {
  while (cin >> A >> B) {
    lenA = A.size(), lenB = B.size();
    memset(dp, 0, sizeof(dp));
    for (int i = 0; i < lenA; i++) {
      for (int j = 0; j < lenB; j++) {
        if (A[i] == B[j])
          dp[i + 1][j + 1] = dp[i][j] + 1;
      }
    }
    int res = 0;
    for (int i = 0; i <= lenA; i++)
      for (int j = 0; j <= lenB; j++)
        res = max(res, dp[i][j]);
    cout << res << endl;
  }
  return 0;
}
Problem p00451 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#include <string.h>

int main(void) {
  char str1[5001] = {0}, str2[5001] = {0};
  str1[0] = '0';
  str2[0] = '0';

  while (scanf("%s", &str1[1]) != EOF) {
    int dp[2][5002] = {0};
    int i, j, st1, st2, max = 0, f = 0;

    scanf("%s", &str2[1]);

    st1 = strlen(str1);
    st2 = strlen(str2);

    for (i = 1; i < st1; i++) {
      for (j = 1; j < st2; j++) {
        if (str1[i] == str2[j]) {
          dp[f][j] = dp[1 - f][j - 1] + 1;
        } else
          dp[f][j] = 0;

        if (dp[f][j] > max)
          max = dp[f][j];
      }
      if (f)
        f = 0;
      else
        f = 1;
    }
    printf("%d\\n", max);
  }

  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>
#include <string.h>

int main(void) {
  char str1[4001] = {0}, str2[4001] = {0};
  str1[0] = '0';
  str2[0] = '0';

  while (scanf("%s", &str1[1]) != EOF) {
    int dp[2][4002] = {0};
    int i, j, st1, st2, max = 0, f = 0;

    scanf("%s", &str2[1]);

    st1 = strlen(str1);
    st2 = strlen(str2);

    for (i = 1; i < st1; i++) {
      for (j = 1; j < st2; j++) {
        if (str1[i] == str2[j]) {
          dp[f][j] = dp[1 - f][j - 1] + 1;
        } else
          dp[f][j] = 0;

        if (dp[f][j] > max)
          max = dp[f][j];
      }
      if (f)
        f = 0;
      else
        f = 1;
    }
    printf("%d\\n", max);
  }

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

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\\D_Download\\Visual Studio
///2015\\Projects\\programing_contest_c++\\Debug\\a.txt" > "D:\\D_Download\\Visual
///Studio 2015\\Projects\\programing_contest_c++\\Debug\\b.answer"

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

  return 0;
}

Error: -11

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\\D_Download\\Visual Studio
///2015\\Projects\\programing_contest_c++\\Debug\\a.txt" > "D:\\D_Download\\Visual
///Studio 2015\\Projects\\programing_contest_c++\\Debug\\b.answer"
int main() {
  string a, b;
  while (cin >> a >> b) {
    int dp[4001][4001];
    for (int i = 0; i < 4001; ++i) {
      for (int j = 0; j < 4001; ++j) {
        dp[i][j] = -1e9;
      }
    }
    dp[0][0] = 0;
    int ans = 0;
    for (int i = 0; i < a.size(); ++i) {
      for (int j = 0; j < a.size(); ++j) {
        if (a[i] == b[j]) {
          if (i > 0 && j > 0) {
            dp[i][j] = dp[i - 1][j - 1] + 1;
          } else {
            dp[i][j] = 1;
          }
        }
        ans = max(ans, dp[i][j]);
      }
    }
    cout << ans << endl;
  }

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

#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(a) (a).begin(), (a).end()
int INF = 1e9;
int MOD = 1000000007;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
double EPS = 1e-10;
const int mod = 1e9 + 7;
int dp[4000][4000];
int main() {
  string s, t;
  while (cin >> s >> t) {
    int ans = 0;
    memset(dp, 0, sizeof(dp));
    REP(i, s.size()) {
      REP(j, t.size()) {
        if (s[i] == t[j]) {
          if (i == 0 || j == 0)
            dp[i][j] = 1;
          else
            dp[i][j] = dp[i - 1][j - 1] + 1;
        }
        ans = max(ans, dp[i][j]);
      }
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(a) (a).begin(), (a).end()
#define INF (1 << 29)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ull B = 100000007;
typedef pair<int, int> P;
bool contain(string a, string b) {
  int al = a.length(), bl = b.length();
  if (al > bl)
    return false;
  ull t = 1;
  REP(i, al) t *= B;
  ull ah = 0, bh = 0;
  REP(i, al) ah = ah * B + a[i];
  REP(i, al) bh = bh * B + b[i];
  for (int i = 0; i + al <= bl; i++) {
    if (ah == bh)
      return true;
    if (i + al < bl)
      bh = bh * B + b[i + al] - b[i] * t;
  }
  return false;
}
int main() {
  string a, b;
  while (cin >> a >> b) {
    int ans = 0;
    if (a.length() > b.length())
      a.swap(b);
    string tmp;
    for (int i = 0; i < a.size(); i++) {
      for (int j = 1; i + j < a.size(); j++) {
        tmp.assign(a, i, j);
        if (contain(tmp, b))
          ans = max(ans, j);
      }
    }
    cout << ans << endl;
  }
}
Problem p00451 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define FOR(i, n) for (int i = 0; i < (int)(n); i++)
#define FORR(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define pb(a) push_back(a)
#define ZERO(a) memset(a, 0, sizeof(a))
#define len(a) sizeof(a)
#define ll long long
#define INF 1 << 29
#define MAX 4002
using namespace std;

int dp[MAX][MAX];

void solve() {
  string s, t;
  while (cin >> s >> t) {
    ZERO(dp);
    FOR(i, s.size()) {
      FOR(j, t.size()) {
        if (s[i] == t[j]) {
          dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + 1);
        }
      }
    }
    int ma = 0;
    FOR(i, s.size() + 1) {
      FOR(j, t.size() + 1) { ma = max(ma, dp[i][j]); }
    }
    printf("%d\\n", ma);
  }
}

int main() {
  solve();
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define FOR(i, n) for (int i = 0; i < (int)(n); i++)
#define FORR(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define pb(a) push_back(a)
#define ZERO(a) memset(a, 0, sizeof(a))
#define len(a) sizeof(a)
#define ll long long
#define INF 1 << 29
#define MAX 4002
using namespace std;

int dp[MAX][MAX];

void solve() {
  string s, t;
  while (~scanf("%s", &s)) {
    scanf("%s", &t);
    ZERO(dp);
    FOR(i, s.size()) {
      FOR(j, t.size()) {
        if (s[i] == t[j]) {
          dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + 1);
        }
      }
    }
    int ma = 0;
    FOR(i, s.size() + 1) {
      FOR(j, t.size() + 1) { ma = max(ma, dp[i][j]); }
    }
    printf("%d", ma);
  }
}

int main() {
  solve();
  return 0;
}
Problem p00451 (C++) — buggy submission, verdict: Runtime Error

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

#define MAX 4001

int dp[MAX][MAX];

int main(void) {
  string s, t;
  while (cin >> s >> t) {
    for (int i = 0; i < s.size(); i++) {
      for (int j = 0; j < t.size(); j++) {
        if (s[i] == t[j]) {
          dp[i][j] = 1;
        } else {
          dp[i][j] = 0;
        }
      }
    }

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

    int ans = 0;
    for (int i = 0; i < s.size(); i++) {
      for (int j = 0; j < t.size(); j++) {
        ans = max(ans, dp[i][j]);
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  string s, t;
  while (cin >> s >> t) {
    int dp[s.size()][t.size()];
    for (int i = 0; i < s.size(); i++) {
      for (int j = 0; j < t.size(); j++) {
        if (s[i] == t[j]) {
          dp[i][j] = 1;
        } else {
          dp[i][j] = 0;
        }
      }
    }

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

    int ans = 0;
    for (int i = 0; i < s.size(); i++) {
      for (int j = 0; j < t.size(); j++) {
        ans = max(ans, dp[i][j]);
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00451 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

int solve(const string &s1, const string &s2, int l1, int l2) {
  int tmp = 0;
  int res = 0;
  int len = min(s2.length() - l2, s1.length() - l1);
  for (int i = 0; i < len; i++) {
    if (s1[l1 + i] != s2[l2 + i]) {
      res = max(res, tmp);
      tmp = 0;
    } else {
      tmp++;
    }
  }
  return max(tmp, res);
}

int main() {

  string s1, s2;
  int l1, l2, ans;
  int i = 0;
  while (1) {
    cin >> s1;
    cin >> s2;
    l1 = s1.length();
    l2 = 0;
    ans = 0;
    while (l1 != 0 || l2 <= s2.length()) {

      if (l1 != 0) {
        l1--;
      } else {
        l2++;
      }
      ans = max(ans, solve(s1, s2, l1, l2));
    }
    cout << ans << endl;
    if (i == 9) {
      break;
    }
    i++;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  string s[10000];
  int i = 0;
  while (1) {
    cin >> s[i];
    i++;
    if (cin.eof()) {
      break;
    }
  }
  int max = 0;
  int tmp = 0;
  for (int j = 0; j < i / 2; j++) {
    string s1 = s[j * 2];
    string s2 = s[j * 2 + 1];
    if (s1.length() > s2.length()) {
      string ss = s1;
      s1 = s2;
      s2 = ss;
    }
    max = 0;
    for (int k = 0; k < s1.length(); k++) {
      for (int l = 0; l < s2.length(); l++) {
        tmp = 0;
        if (s1.at(k) == s2.at(l)) {
          tmp++;
          for (int m = k + 1; m < s1.length(); m++) {
            if (s2.length() <= l + m - k || s1.length() <= m) {
              break;
            }
            if (s1.at(m) == s2.at(l + m - k)) {
              tmp++;
            } else {
              break;
            }
          }

          if (max < tmp) {
            max = tmp;
          }
        }
      }
    }
    cout << max << endl;
  }
}
Problem p00451 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define vi vector<int>
#define VS vector<string>
#define all(x) x.begin(), x.end()
#define mp make_pair
#define pb push_back

using namespace std;

int main() {
  string n, m;
  while (cin >> n >> m) {
    int ans = 0;
    if (n.size() > m.size())
      swap(n, m);
    for (int i = 0; i < n.size(); i++) {
      for (int j = i + 1; j <= n.size(); j++) {
        if (j - i <= ans)
          continue;
        string s = n.substr(i, j - i);
        if (m.find(s) != string::npos) {
          ans = max(ans, (int)s.size());
        } else
          j = 40000;
      }
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define vi vector<int>
#define VS vector<string>
#define all(x) x.begin(), x.end()
#define mp make_pair
#define pb push_back

using namespace std;

int main() {
  string n, m;
  while (cin >> n >> m) {
    int ans = 0;
    if (n.size() > m.size())
      swap(n, m);
    for (int i = 0; i < n.size(); i++) {
      for (int j = i + 1; j < n.size(); j++) {
        if (j - i <= ans)
          continue;
        string s = n.substr(i, j - i);
        if (m.find(s) != string::npos) {
          ans = max(ans, (int)s.size());
        }
      }
    }
    cout << ans << endl;
  }
}
Problem p00451 (C++) — buggy submission, verdict: Runtime Error

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

int dp[4002][4002];

int main() {
  string s;
  string t;
  int mx;

  while (getline(cin, s)) {
    fill(dp[0], dp[4000] + 4000, 0);
    getline(cin, t);
    int 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] = 0;
        }
      }
    }

    mx = 0;
    for (int i = 0; i <= n + 1; i++)
      for (int j = 0; j <= m + 1; j++)
        mx = max(mx, dp[i][j]);

    cout << mx << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

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

int dp[4001][4001];

int main() {
  string s;
  string t;
  int mx;

  while (getline(cin, s)) {
    fill(dp[0], dp[4000] + 4000, 0);
    getline(cin, t);
    int 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] = 0;
        }
      }
    }

    mx = 0;
    for (int i = 0; i <= n + 1; i++)
      for (int j = 0; j <= m + 1; j++)
        mx = max(mx, dp[i][j]);

    cout << mx << endl;
  }

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

#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;

int main() {
  while (true) {
    char s[4001], t[4001];
    int l, r, ans = 0;
    static int dp[4001][4001];
    if (scanf("%s %s", s, t) == EOF)
      return 0;
    l = strlen(s), r = strlen(t);
    for (int i = 0; i <= l; i++) {
      for (int j = 0; j <= r; j++) {
        if (i == 0 || j == 0)
          dp[i][j] = 0;
        else if (s[i - 1] == t[j - 1]) {
          dp[i][j] = dp[i - 1][j - 1] + 1;
          ans = max(ans, dp[i][j]);
        } else {
          dp[i][j] = 0;
        }
      }
    }
    printf("%d\\n", ans);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;

char s[4001], t[4001];
int l, r;
bool C(int x) {
  for (int i = 0; i + x <= l; i++) {
    if (search(t, t + r, s + i, s + i + x) != t + r) {
      return true;
    }
  }
  return false;
}

int main() {
  while (true) {
    if (scanf("%s %s", s, t) == EOF)
      return 0;
    l = strlen(s), r = strlen(t);
    int lb = 0, ub = min(l, r) + 1;
    while (ub - lb > 1) {
      int mid = (ub + lb) / 2;
      if (C(mid))
        lb = mid;
      else
        ub = mid;
    }
    printf("%d\\n", lb);
  }
}