Problem p03364 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
using namespace std;

#define LL long long

char ma[603][603];
int mb[603][603];

int cd(int a, int b, int n) {
  int ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i; j < n; j++) {
      if (ma[a + i][b + j] != ma[a + j][b + i])
        ans++;
    }
  }
  return ans;
}

int next(int a, int b, int n) {
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (ma[a - 1 + i][b] != ma[a][b - 1 + i])
      ans--;
  }
  for (int i = 0; i < n; i++) {
    if (ma[a + i][b + n] != ma[a + n][b + i])
      ans++;
  }
  return ans;
}

int main() {
  int n;
  while (cin >> n) {
    for (int i = 0; i < n; i++) {
      cin >> ma[i];
    }
    for (int i = 0; i < n; i++) {
      for (int j = n; j < 2 * n; j++)
        ma[i][j] = ma[i][j - n];
    }
    for (int i = n; i < 2 * n; i++) {
      for (int j = 0; j < 2 * n; j++)
        ma[i][j] = ma[i - n][j];
    }
    //
    //                for(int i=0;i<2*n;i++){
    //                        for(int j=0;j<2*n;j++){
    //                                cout<<ma[i][j];
    //                        }
    //                        cout<<endl;
    //                }
    int ans = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        mb[i][j] = cd(i, j, n);
        for (int k = 1; k + i < n && k + j < n; k++) {
          mb[i + k][j + k] = mb[i + k - 1][j + k - 1] + next(i + k, j + k, n);
        }
      }
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        // cout<<mb[i][j]<<endl;
        if (mb[i][j] == 0)
          ans++;
      }
      // cout<<endl;
    }
    cout << ans << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
using namespace std;

#define LL long long

char ma[603][603];
int mb[603][603];

int cd(int a, int b, int n) {
  int ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i; j < n; j++) {
      if (ma[a + i][b + j] != ma[a + j][b + i])
        ans++;
    }
  }
  return ans;
}

int next(int a, int b, int n) {
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (ma[a - 1 + i][b] != ma[a][b - 1 + i])
      ans--;
  }
  for (int i = 0; i < n; i++) {
    if (ma[a + i][b + n] != ma[a + n][b + i])
      ans++;
  }
  return ans;
}

int main() {
  int n;
  while (cin >> n) {
    for (int i = 0; i < n; i++) {
      cin >> ma[i];
    }
    for (int i = 0; i < n; i++) {
      for (int j = n; j < 2 * n; j++)
        ma[i][j] = ma[i][j - n];
    }
    for (int i = n; i < 2 * n; i++) {
      for (int j = 0; j < 2 * n; j++)
        ma[i][j] = ma[i - n][j];
    }
    //
    //                for(int i=0;i<2*n;i++){
    //                        for(int j=0;j<2*n;j++){
    //                                cout<<ma[i][j];
    //                        }
    //                        cout<<endl;
    //                }
    int ans = 0;
    for (int i = 0; i < n; i++) {
      mb[i][0] = cd(i, 0, n);
      for (int k = 1; k + i < n && k + 0 < n; k++) {
        mb[i + k][0 + k] = mb[i + k - 1][0 + k - 1] + next(i + k, 0 + k, n);
      }
    }
    for (int j = 1; j < n; j++) {
      mb[0][j] = cd(0, j, n);
      for (int k = 1; k + 0 < n && k + j < n; k++) {
        mb[0 + k][j + k] = mb[0 + k - 1][j + k - 1] + next(0 + k, j + k, n);
      }
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        // cout<<mb[i][j]<<endl;
        if (mb[i][j] == 0)
          ans++;
      }
      // cout<<endl;
    }
    cout << ans << endl;
  }
  return 0;
}