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

#include <bits/stdc++.h>
using namespace std;
int n;
int memo[10001];
bool isPrime(int x) {
  if (memo[x] >= 0)
    return memo[x];
  for (int i = 2; i * i <= n; i++) {
    if (x % i == 0)
      return memo[x] = 0;
  }
  return memo[x] = 1;
}

int main() {
  fill(memo, memo + 10000, -1);
  while (cin >> n && n) {
    int mx = 5;
    for (int i = 2; i <= n - 2; i++) {
      if ((isPrime(i) & isPrime(i + 2)) == 1) {
        mx = i;
      }
    }
    cout << mx << ' ' << mx + 2 << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n;
bool isPrime(int x) {
  for (int i = 2; i * i <= n; i++) {
    if (x % i == 0)
      return 0;
  }
  return 1;
}

int main() {
  while (cin >> n && n) {
    int mx = 5;
    for (int i = 2; i <= n - 2; i++) {
      if ((isPrime(i) & isPrime(i + 2)) == 1) {
        mx = i;
      }
    }
    cout << mx << ' ' << mx + 2 << endl;
  }

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
int lis[10001];
int main() {
  for (int i = 2; i <= 10000; i++) {
    int flg = 1;
    for (int j = 2; j <= 10000; j++) {
      if (i % j == 0 && i != j)
        flg = 0;
    }
    lis[i] = flg;
  }
  while (1) {
    int a;
    cin >> a;
    if (a == 0)
      break;
    for (int i = a;; a--) {
      if (lis[a] && lis[a - 2]) {
        cout << a - 2 << ' ' << a << endl;
        break;
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
int lis[10001];
int main() {
  for (int i = 2; i <= 10000; i++) {
    int flg = 1;
    for (int j = 2; j <= 10000; j++) {
      if (i % j == 0 && i != j)
        flg = 0;
    }
    lis[i] = flg;
  }
  while (1) {
    int a;
    cin >> a;
    for (int i = a;; a--) {
      if (lis[a] && lis[a - 2]) {
        cout << a - 2 << ' ' << a << endl;
        break;
      }
    }
  }
  return 0;
}
Problem p00150 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

#define EPS (1e-10)
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))

typedef complex<double> P;
typedef long long ll;

const int MAX_SIZE = 10000;
const int MAX_PRIME = 10001;

int prime[MAX_PRIME + 1];
bool isPrime[MAX_PRIME + 1];
int p;

void erats() {
  // primeツδ環スツトツづーツ渉可甘コツ可サ
  fill(isPrime, isPrime + MAX_PRIME, true);
  p = 0;
  isPrime[0] = isPrime[1] = false;

  for (int i = 2; i <= MAX_PRIME; i++) {
    if (isPrime[i]) {
      prime[p++] = i;
    } else
      continue;

    // ツづ督づゥツつ「ツづ可つゥツつッツづゥ
    for (int j = 2 * i; j <= MAX_PRIME; j += i) {
      isPrime[j] = false;
    }
    cout << flush;
  }
}

int main() {

  erats();

  int n;
  vector<pair<int, int>> twin;
  bool f = false;
  for (int i = 0; i < p; i++) {
    for (int j = i + 1; j < p; j++) {
      if (prime[j] - prime[i] == 2) {
        twin.push_back(make_pair(prime[i], prime[j]));
        //					cout<<prime[j]<<"
        //"<<prime[i]<<endl;
      }
    }
  }

  while (cin >> n && n != 0) {
    for (int i = twin.size() - 1;; i--) {
      if (twin[i].first <= n && twin[i].second <= n) {
        cout << twin[i].first << " " << twin[i].second << endl;
        break;
      }
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

#define EPS (1e-10)
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))

typedef complex<double> P;
typedef long long ll;

const int MAX_SIZE = 10000;
const int MAX_PRIME = 10001;

int prime[MAX_PRIME + 1];
bool isPrime[MAX_PRIME + 1];
int p;

void erats() {
  // primeツδ環スツトツづーツ渉可甘コツ可サ
  fill(isPrime, isPrime + MAX_PRIME, true);
  p = 0;
  isPrime[0] = isPrime[1] = false;

  for (int i = 2; i <= MAX_PRIME; i++) {
    if (isPrime[i]) {
      prime[p++] = i;
    } else
      continue;

    // ツづ督づゥツつ「ツづ可つゥツつッツづゥ
    for (int j = 2 * i; j <= MAX_PRIME; j += i) {
      isPrime[j] = false;
    }
    cout << flush;
  }
}

int main() {

  erats();

  int n;
  while (cin >> n && n != 0) {
    bool f = false;
    for (int i = p - 1; i >= 0; i--) {
      for (int j = i - 1; j >= 0; j--) {
        if (prime[i] - prime[j] == 2 && prime[i] <= n) {
          cout << prime[j] << " " << prime[i] << endl;
          f = true;
          break;
        }
      }
      if (f)
        break;
    }
  }

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

#include <stdio.h>

int isprime(int n) {
  int i;
  for (i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      return 0;
    }
  }
  return 1;
}

int main() {
  int n, i;
  int sousi[2];
  int prime[10100][2];
  for (i = 0; i < 10100; i++) {
    prime[i][0] = i;
    prime[i][1] = isprime(i);
  }
  while (1) {
    sousi[0] = 0;
    sousi[1] = 0;
    scanf("%d", &n);
    if (n == 0) {
      break;
    }
    for (i = n; i > 2; i--) {
      if (prime[i][1] == 1 && prime[i - 2][1] == 1) {
        sousi[0] = prime[i - 2][0];
        sousi[1] = prime[i][0];
        break;
      }
    }
    printf("%d %d\\n", sousi[0], sousi[1]);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>

int isprime(int n) {
  int i;
  for (i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      return 0;
    }
  }
  return 1;
}

int main() {
  int n, i;
  int sousi[2];
  int prime[10100][2];
  for (i = 0; i < 10100; i++) {
    prime[i][0] = i;
  }
  while (1) {
    sousi[0] = 0;
    sousi[1] = 0;
    scanf("%d", &n);
    for (i = 0; i < n; i++) {
      prime[i][1] = 0;
    }
    if (n == 0) {
      break;
    }
    for (i = 0; i <= n; i++) {
      prime[i][1] = isprime(prime[i][0]);
    }
    for (i = n; i > 2; i--) {
      if (prime[i][1] == 1 && prime[i - 2][1] == 1) {
        sousi[0] = prime[i - 2][0];
        sousi[1] = prime[i][0];
        break;
      }
    }
    printf("%d %d\\n", sousi[0], sousi[1]);
  }
  return 0;
}
Problem p00150 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repc(i, s, e) for (int i = (s); i < (e); i++)
#define pb(n) push_back((n))
#define mp(n, m) make_pair((n), (m))
#define all(r) r.begin(), r.end()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int main() {
  vi v(100100, 1);
  v[0] = 0;
  v[1] = 1;
  for (int i = 2; i < 100100; i++) {
    if (v[i] == 1) {
      for (int j = 2; j <= 100100 / i; j++) {
        v[i * j] = 0;
      }
    }
  }
  int n;
  while (cin >> n && n > 0) {
    int ans = 5;
    for (int i = n; i > 1; i--) {
      if (v[i] && v[i - 2]) {
        ans = i;
        break;
      }
    }
    cout << ans - 2 << " " << ans << endl;
  }
}

Error: -6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)


Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repc(i, s, e) for (int i = (s); i < (e); i++)
#define pb(n) push_back((n))
#define mp(n, m) make_pair((n), (m))
#define all(r) r.begin(), r.end()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int main() {
  vi v(10010, 1);
  v[0] = 0;
  v[1] = 1;
  for (int i = 2; i < 10010; i++) {
    if (v[i] == 1) {
      for (int j = 2; j <= 10010 / i; j++) {
        v[i * j] = 0;
      }
    }
  }
  int n;
  while (cin >> n && n > 0) {
    int ans = 5;
    for (int i = n; i > 1; i--) {
      if (v[i] && v[i - 2]) {
        ans = i;
        break;
      }
    }
    cout << ans - 2 << " " << ans << endl;
  }
}
Problem p00150 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int maxsize(0);
  vector<int> input;

  while (1) {
    int tmp;

    cin >> tmp;

    if (!tmp) {
      break;
    }

    input.push_back(tmp);

    (maxsize < tmp) ? (maxsize = tmp) : (0);
  }

  vector<int> prime, ans;

  for (int i(2); i <= maxsize; ++i) {
    for (vector<int>::iterator i2(prime.begin()), n2(prime.end()); i2 != n2;
         ++i2) {
      if ((i % *i2) == 0) {
        goto nx;
      }
    }

    prime.push_back(i);

    if (i != *prime.begin() &&
        binary_search(prime.begin(), prime.end(), i - 2)) {
      ans.push_back(i);
    }
  nx:;
  }

  for (vector<int>::iterator i(input.begin()), n(input.end()); i != n; ++i) {
    int out(*(--upper_bound(ans.begin(), ans.end(), *i)));

    cout << out - 2 << ' ' << out << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
int main() {
  int maxsize(0);
  vector<int> input;

  while (1) {
    int tmp;

    cin >> tmp;

    (maxsize < tmp) ? (maxsize = tmp) : (0);
  }

  vector<int> prime, ans;

  for (int i(1); i <= maxsize; ++i) {
    for (vector<int>::iterator i2(prime.begin()), n2(prime.end()); i2 != n2;
         ++i2) {
      if (i % *i2 == 0) {
        prime.push_back(i);
        if (i2 != prime.begin()) {
          if (*(--i2) == i) {
            ans.push_back(i);
          }
        }
        break;
      }
    }
  }

  for (vector<int>::iterator i(input.begin()), n(input.end()); i != n; ++i) {
    int out(*i);

    for (vector<int>::reverse_iterator i2(ans.rbegin()), n2(ans.rend());
         i2 != n2; ++i2) {
      if (*i2 <= out) {
        cout << (*i2 - 2) << ' ' << (*i2) << endl;
        break;
      }
    }
  }

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

#include <stdio.h>
const int MAX_V = 10000; // 1????????§????´???°??¨?????????

int main() {
  int i, k, n, p, q;
  int prime[MAX_V + 1]; // 1????´???°???0????´???°??§?????????
  for (i = 2; i <= MAX_V; i++) {
    prime[i] = 1; // 2??\\???????´???°??¨??????
  }
  for (i = 2; i * i <= MAX_V; i++) {
    if (prime[i]) {
      for (k = 2 * i; k <= MAX_V; k += i) {
        prime[k] = 0; // ?´???°???2?????\\???????????°????´???°??§?????????
      }
    }
  }
  //?´???°??¨fin
  // while(scanf("%d",&n) != 0){
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      break;
    for (i = n; i >= 3; i--) { //???????????¨????´???°????????????
      if (prime[i] == 1 && prime[i - 2] == 1) {
        p = i;
        q = p - 2;
        break;
      }
    }
    // 	ans[a]=i; a++;
    // 	ans[a]=i-2; a++;
    // }
    // for(i=0;i<=a;i=i+2){
    // 	printf("%d %d\\n",ans[i],ans[i+1]);
    // }
    printf("%d %d\\n", q, p);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
const int MAX_V = 10000; // 1????????§????´???°??¨?????????

int main() {
  int i, k, n;
  int prime[MAX_V + 1]; // 1????´???°???0????´???°??§?????????
  for (i = 2; i <= MAX_V; i++) {
    prime[i] = 1; // 2??\\???????´???°??¨??????
  }
  for (i = 2; i * i <= MAX_V; i++) {
    if (prime[i]) {
      for (k = 2 * i; k <= MAX_V; k += i) {
        prime[k] = 0; // ?´???°???2?????\\???????????°????´???°??§?????????
      }
    }
  }
  //?´???°??¨fin
  while (scanf("%d", &n) != 0) {
    for (i = n; i >= 3; i--) { //???????????¨????´???°????????????
      if (prime[i] == 1 && prime[i - 2] == 1) {
        break;
      }
    }
    // 	ans[a]=i; a++;
    // 	ans[a]=i-2; a++;
    // }
    // for(i=0;i<=a;i=i+2){
    // 	printf("%d %d\\n",ans[i],ans[i+1]);
    // }
    printf("%d %d\\n", i, i - 2);
  }
  return 0;
}
Problem p00150 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>

int isprime(int n) {
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return 0;
  }
  return 1;
}

int main() {
  int n, i, min, max;
  int number[10000 + 1] = {0};
  for (i = 2; i < 10000; i++) {
    number[i] = isprime(i);
  }
  while (scanf("%d", &n)) {
    if (n == 0)
      break;
    max = 0;
    min = 0;
    for (i = 2; i <= n; i++) {
      if (number[i] && number[i - 2]) {
        min = i - 2;
        max = i;
        // printf("%d %d\\n",i-2,i);
      }
    }
    printf("%d %d\\n", min, max);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>

int isprime(int n) {
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return 0;
  }
  return 1;
}

int main() {
  int n, i, min, max;
  int number[10000 + 1];
  while (scanf("%d", &n)) {
    if (n == 0)
      break;
    max = 0;
    min = 0;
    for (i = 2; i < n; i++) {
      number[i] = isprime(i);
      if (number[i] && number[i - 2]) {
        min = i - 2;
        max = i;
        // printf("%d %d\\n",i-2,i);
      }
    }
    printf("%d %d\\n", min, max);
  }
  return 0;
}
Problem p00150 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
const int MAX_V = 10000;
int prime[MAX_V + 1];
int main() {
  int i, k, n, p, q, v;
  for (i = 2; i <= MAX_V; i++) {
    prime[i] = 1;
  }
  for (i = 2; i * i <= MAX_V; i++) {
    if (prime[i]) {
      for (k = 2 * i; k <= MAX_V; k += i) {
        prime[k] = 0;
      }
    }
  }
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      break;
    for (v = n - 2; v >= 2; v--) {
      int isprime = prime[v] * prime[v + 2];
      if (isprime) {
        printf("%d %d\\n", v, v + 2);
        break;
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
int isprime(int n) {
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return 0;
  }
  return 1;
}
int main() {
  int k, n, p, q;
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      break;
    for (k = 2; k <= n - 2; k++) {
      if (isprime(k) && isprime(k + 2)) {
        p = k;
        q = k + 2;
      }
    }
    printf("%d %d\\n", p, q);
  }
  return 0;
}
Problem p00151 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
int main() {
  int n;
  while (scanf("%d", &n), n) {
    int m = 0, v[4][2] = {{0, -1}, {-1, 0}, {1, -1}, {-1, -1}},
        map[257][257] = {{0}};
    for (int y = 1; y <= n; y++)
      for (int x = 1; x <= n; x++)
        scanf("%1d", &map[x][y]);

    for (int i = 0; i < 4; i++) {
      int dp[257][257] = {{0}};
      for (int k = 1; k <= n; k++)
        for (int j = 1; j <= n; j++)
          m = std::max(
              m, dp[j][k] =
                     map[j][k] ? map[j][k] + dp[j + v[i][0]][k + v[i][1]] : 0);
    }
    printf("%d\\n", m);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
int main() {
  int n;
  while (scanf("%d", &n), n) {
    int m = 0, v[4][2] = {{0, -1}, {-1, 0}, {1, -1}, {-1, -1}},
        map[257][257] = {{0}};
    for (int y = 1; y <= n; y++)
      for (int x = 1; x <= n; x++)
        scanf("%1d", &map[x][y]);

    for (int i = 0; i < 4; i--) {
      int dp[257][257] = {{0}};
      for (int k = 1; k <= n; k++)
        for (int j = 1; j <= n; j++)
          m = std::max(
              m, dp[j][k] =
                     map[j][k] ? map[j][k] + dp[j + v[i][0]][k + v[i][1]] : 0);
    }
    printf("%d\\n", m);
  }
}
Problem p00151 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
using namespace std;
int main(void) {
  int i, j, k, n, max, cnt, x, y, dx[] = {1, 1, 0, -1}, dy[] = {0, 1, 1, 1};
  char xy[255][256];
  while (1) {
    max = 0;
    cin >> n;
    if (n == 0)
      break;
    for (i = 0; i < n; i++)
      cin >> xy[i];
    for (i = 0; i < n; i++) {
      for (j = 0; j < n; j++) {
        if (xy[i][j] == '1') {
          for (k = 0; k < 4; k++) {
            cnt = 1;
            y = i;
            x = j;
            while (1) {
              y = y + dy[k];
              x = x + dx[k];
              if (y >= n || x >= n || y < 0 || x < 0)
                break;
              if (xy[y][x] != '1')
                break;
              cnt++;
            }
            if (max < cnt)
              max = cnt;
          }
        }
      }
    }
    cout << max << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;
int main(void) {
  int i, j, k, n, max, cnt, x, y, dx[] = {1, 1, 0, -1}, dy[] = {0, 1, 1, 1};
  char xy[225][226];
  while (1) {
    max = 0;
    cin >> n;
    if (n == 0)
      break;
    for (i = 0; i < n; i++)
      cin >> xy[i];
    for (i = 0; i < n; i++) {
      for (j = 0; j < n; j++) {
        if (xy[i][j] == '1') {
          for (k = 0; k < 4; k++) {
            cnt = 1;
            y = i;
            x = j;
            while (1) {
              y = y + dy[k];
              x = x + dx[k];
              if (y >= n || x >= n || y < 0 || x < 0)
                break;
              if (xy[y][x] != '1')
                break;
              cnt++;
            }
            if (max < cnt)
              max = cnt;
          }
        }
      }
    }
    cout << max << endl;
  }
  return 0;
}
Problem p00151 (C++) — buggy submission, verdict: Runtime Error

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

string field[256];

void rotate(int n) {
  string tmp[256];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      tmp[i] += field[n - j - 1][i];
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++)
      field[i][j] = tmp[i][j];
  }
}

int main() {
  int n;
  while (cin >> n, n) {
    int ma = 0;
    for (int i = 0; i < n; i++)
      cin >> field[i];
    for (int i = 0; i < n; i++) {
      int cnt = 0;
      for (int j = 0; j < n; j++) {
        if (field[i][j] == '1') {
          cnt++;
          ma = max(ma, cnt);
        } else
          cnt = 0;
      }
      cnt = 0;
      for (int j = 0; j < n; j++) {
        if (field[j][i] == '1') {
          cnt++;
          ma = max(ma, cnt);
        } else
          cnt = 0;
      }
    }
    for (int k = 0; k < 2; k++) {
      if (k)
        rotate(n);
      for (int i = 0; i < 2 * n - 1; i++) {
        int cnt = 0;
        for (int j = 0; j < n; j++) {
          if (i - j >= n || i - j < 0)
            continue;
          if (field[j][i - j] == '1') {
            cnt++;
            ma = max(ma, cnt);
          } else
            cnt = 0;
        }
      }
    }
    cout << ma << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

string field[256];

void rotate(int n) {
  string tmp[256];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      tmp[i][j] = field[n - j - 1][i];
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++)
      field[i][j] = tmp[i][j];
  }
}

int main() {
  int n;
  while (cin >> n, n) {
    int ma = 0;
    for (int i = 0; i < n; i++)
      cin >> field[i];
    for (int i = 0; i < n; i++) {
      int cnt = 0;
      for (int j = 0; j < n; j++) {
        if (field[i][j] == '1') {
          cnt++;
          ma = max(ma, cnt);
        } else
          cnt = 0;
      }
      cnt = 0;
      for (int j = 0; j < n; j++) {
        if (field[j][i] == '1') {
          cnt++;
          ma = max(ma, cnt);
        } else
          cnt = 0;
      }
    }
    for (int k = 0; k < 2; k++) {
      if (k)
        rotate(n);
      for (int i = 0; i < 2 * n - 1; i++) {
        int cnt = 0;
        for (int j = 0; j < n; j++) {
          if (i - j >= n || i - j < 0)
            continue;
          if (field[j][i - j] == '1') {
            cnt++;
            ma = max(ma, cnt);
          } else
            cnt = 0;
        }
      }
    }
    cout << ma << endl;
  }
  return 0;
}
Problem p00151 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

char map[255][255];

int dire4[][2] = {{1, 0},   {-1, 1}, {0, 1},  {1, 1},
                  {-1, -1}, {0, -1}, {1, -1}, {-1, 0}};

int main() {
  int n;
  while (1) {
    cin >> n;
    if (n == 0)
      break;

    for (int y = 0; y < n; y++) {
      for (int x = 0; x < n; x++) {
        cin >> map[y][x];
      }
    }

    int ans = 0;
    for (int y = 0; y < n; y++) {
      for (int x = 0; x < n; x++) {
        for (int z = 0; z < 8; z++) {
          int dx = dire4[z][0], dy = dire4[z][1];
          int cx = x, cy = y;
          int c = 0;
          for (;
               map[cy][cx] == '1' && 0 <= cx && cx < n && 0 <= cy && cy < n;) {
            cx += dx;
            cy += dy;
            c++;
          }
          ans = max(ans, c);
        }
      }
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

char map[255][255];

int main() {
  int n;
  while (1) {
    cin >> n;
    if (n == 0)
      break;

    for (int y = 0; y < n; y++) {
      for (int x = 0; x < n; x++) {
        cin >> map[y][x];
      }
    }

    int ans = 0;
    for (int y = 0; y < n; y++) {
      for (int x = 0; x < n; x++) {
        int dire4[][2] = {{1, 0},   {-1, 1}, {0, 1},  {1, 1},
                          {-1, -1}, {0, -1}, {1, -1}, {-1, 0}};
        for (int z = 0; z < 8; z++) {
          int dx = dire4[z][0], dy = dire4[z][1];
          int cx = x, cy = y;
          int c = 0;
          for (; map[cy][cx] == '1' && cx < n && cy < n;) {
            cx += dx;
            cy += dy;
            c++;
          }
          ans = max(ans, c);
        }
      }
    }
    cout << ans << endl;
  }
}
Problem p00151 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1}, dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};
int ans;
int n;
char ma[300][300];

void cheak(int px, int dx, int py, int dy) {
  int cnt = 1;

  while (1) {
    px += dx;
    py += dy;
    if (!(0 <= px && px < n && 0 <= py && py < n) || ma[py][px] != '1')
      break;
    cnt++;
  }
  ans = max(ans, cnt);
}

int main() {
  while (cin >> n, n) {

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        cin >> ma[i][j];
      }
    }

    ans = 0;

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {

        if (ma[i][j] == '1') {
          for (int l = 0; l < 8; l++) {
            cheak(j, dx[l], i, dy[l]);
          }
        }
      }
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1}, dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};
int ans;
int n;
char ma[100][100];

void cheak(int px, int dx, int py, int dy) {
  int cnt = 1;

  while (1) {
    px += dx;
    py += dy;
    if (!(0 <= px && px < n && 0 <= py && py < n) || ma[py][px] != '1')
      break;
    cnt++;
  }
  ans = max(ans, cnt);
}

int main() {
  while (cin >> n, n) {

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        cin >> ma[i][j];
      }
    }

    ans = 0;

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {

        if (ma[i][j] == '1') {
          for (int l = 0; l < 8; l++) {
            cheak(j, dx[l], i, dy[l]);
          }
        }
      }
    }
    cout << ans << endl;
  }
}