Problem p03364 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); ++i)
#define rrep(i, n) for (int(i) = (n)-1; (i) >= 0; --i)
#define rep1(i, n) for (int(i) = 1; (i) <= (n); ++i)
#define rrep1(i, n) for (int(i) = (n); (i) >= 1; --i)
#define pb push_back
#define fr first
#define sc second
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> LP;
typedef double db;
using namespace std;

string s[601];
ll N = 0;
ll imos[601][601];
ll ans = 0;

bool valo(ll y, ll x) { return 0 <= y && 0 <= x && y < N && x < N; }

bool val(ll y, ll x) { return 0 <= y && 0 <= x && y < 2 * N && x < 2 * N; }

LP ok(ll y, ll x) {
  if (x < 0 || y < 0) {
    return LP(y - min(y, x), x - min(y, x));
  }
  return LP(y, x);
}

int main() {
  cin >> N;
  rep(i, N) { cin >> s[i]; }
  rep(i, N) rep(j, N) { s[i + N][j + N] = s[i + N][j] = s[i][j + N] = s[i][j]; }
  rep(i, 2 * N) rep(j, 2 * N) {
    rep1(k, N - 1) {
      if (!val(i - k, j + k))
        break;
      if (s[i][j] == s[i - k][j + k]) {
        imos[ok(i - N + 1, j + k - N + 1).fr]
            [ok(i - N + 1, j + k - N + 1).sc]++;
        imos[i - k + 1][j + 1]--;
      }
    }
  }
  rep(i, N) {
    rep(k, N) { imos[i + k + 1][k + 1] += imos[i + k][k]; }
  }
  rep1(i, N - 1) {
    rep(k, N) { imos[k + 1][i + k + 1] += imos[k][i + k]; }
  }
  rep(i, N) rep(j, N) {
    if (imos[i][j] == N * (N - 1) / 2)
      ans++;
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); ++i)
#define rrep(i, n) for (int(i) = (n)-1; (i) >= 0; --i)
#define rep1(i, n) for (int(i) = 1; (i) <= (n); ++i)
#define rrep1(i, n) for (int(i) = (n); (i) >= 1; --i)
#define pb push_back
#define fr first
#define sc second
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> LP;
typedef double db;
using namespace std;

string s[601];
ll N = 0;
ll imos[601][601];
ll ans = 0;

bool valo(ll y, ll x) { return 0 <= y && 0 <= x && y < N && x < N; }

bool val(ll y, ll x) { return 0 <= y && 0 <= x && y < 2 * N && x < 2 * N; }

LP ok(ll y, ll x) {
  if (x < 0 || y < 0) {
    return LP(y - min(y, x), x - min(y, x));
  }
  return LP(y, x);
}

int main() {
  cin >> N;
  rep(i, N) {
    cin >> s[i];
    s[i + N] = s[i];
  }
  rep(i, 2 * N) { s[i] += s[i]; }
  rep(i, 2 * N) rep(j, 2 * N) {
    rep1(k, N - 1) {
      if (!val(i - k, j + k))
        break;
      if (s[i][j] == s[i - k][j + k]) {
        imos[ok(i - N + 1, j + k - N + 1).fr]
            [ok(i - N + 1, j + k - N + 1).sc]++;
        imos[i - k + 1][j + 1]--;
      }
    }
  }
  rep(i, N) {
    rep(k, N) { imos[i + k + 1][k + 1] += imos[i + k][k]; }
  }
  rep1(i, N - 1) {
    rep(k, N) { imos[k + 1][i + k + 1] += imos[k][i + k]; }
  }
  rep(i, N) rep(j, N) {
    if (imos[i][j] == N * (N - 1) / 2)
      ans++;
  }
  cout << ans << endl;
}