Problem p03364 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long int LL;

template <typename T> ostream &operator<<(ostream &out, const vector<T> &vec) {
  out << "[";
  for (int x : vec)
    out << x << ", ";
  out << "]";
  return out;
}

#if DEBUG && !ONLINE_JUDGE
ifstream input_from_file("input.txt");
#define cin input_from_file
#define dbg_var(x) cerr << #x << ": " << x << endl;
#endif

int n;
char g[700][700];
int s[700][700];

int calc(int x, int y) {
  for (int i = x - 1; i < 2 * n; i++)
    for (int j = y - 1; j < 2 * n; j++)
      s[i][j] = 0;

  for (int i = x; i < 2 * n; i++)
    for (int j = y; j < 2 * n; j++) {
      s[i][j] = (g[i][j] == g[j - y + x][i - x + y]) + s[i - 1][j] +
                s[i][j - 1] - s[i - 1][j - 1];
    }

  //~ cout << x << ' ' << y << endl;
  //~ for(int i=x; i<2*n; i++) {for(int j=y; j<2*n; j++) cout << s[i][j] << ' ';
  //cout << endl;} ~ cout << endl;

  int ans = 0;
  for (int i = n - 1; x + i < 2 * n && y + i < 2 * n; i++) {
    if (s[x + i][y + i] - s[x + i - n][y + i] - s[x + i][y + i - n] +
            s[x + i - n][y + i - n] ==
        n * n)
      ans++;
  }
  return ans;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> n;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      cin >> g[i][j];
  for (int i = 1; i < 2 * n; i++)
    for (int j = n + 1; j < 2 * n; j++)
      g[i][j] = g[i][j - n];
  for (int i = n + 1; i < 2 * n; i++)
    for (int j = 1; j < 2 * n; j++)
      g[i][j] = g[i - n][j];

  int ans = calc(1, 1);
  for (int x = 2; x <= n; x++)
    ans += calc(x, 1);
  for (int y = 2; y <= n; y++)
    ans += calc(1, y);
  cout << ans << '\n';
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int LL;

template <typename T> ostream &operator<<(ostream &out, const vector<T> &vec) {
  out << "[";
  for (int x : vec)
    out << x << ", ";
  out << "]";
  return out;
}

#if DEBUG && !ONLINE_JUDGE
ifstream input_from_file("input.txt");
#define cin input_from_file
#define dbg_var(x) cerr << #x << ": " << x << endl;
#endif

int n;
char g[700][700];
int s[700][700];

int calc(int x, int y) {
  for (int i = x - 1; i < 2 * n; i++)
    for (int j = y - 1; j < 2 * n; j++)
      s[i][j] = 0;

  for (int i = x; i < 2 * n && i < 2 * n + x - y; i++)
    for (int j = y; j < 2 * n && j < 2 * n + y - x; j++) {
      s[i][j] = (g[i][j] == g[j - y + x][i - x + y]) + s[i - 1][j] +
                s[i][j - 1] - s[i - 1][j - 1];
    }

  //~ cout << x << ' ' << y << endl;
  //~ for(int i=x; i<2*n; i++) {for(int j=y; j<2*n; j++) cout << s[i][j] << ' ';
  //cout << endl;} ~ cout << endl;

  int ans = 0;
  for (int i = n - 1; x + i < 2 * n && y + i < 2 * n; i++) {
    if (s[x + i][y + i] - s[x + i - n][y + i] - s[x + i][y + i - n] +
            s[x + i - n][y + i - n] ==
        n * n)
      ans++;
  }
  return ans;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> n;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      cin >> g[i][j];
  for (int i = 1; i < 2 * n; i++)
    for (int j = n + 1; j < 2 * n; j++)
      g[i][j] = g[i][j - n];
  for (int i = n + 1; i < 2 * n; i++)
    for (int j = 1; j < 2 * n; j++)
      g[i][j] = g[i - n][j];

  int ans = calc(1, 1);
  for (int x = 2; x <= n; x++)
    ans += calc(x, 1);
  for (int y = 2; y <= n; y++)
    ans += calc(1, y);
  cout << ans << '\n';
  return 0;
}