Problem p03364 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>
#define rep(i, s, n) for (int i = (s); (n) > i; i++)
#define REP(i, n) rep(i, 0, n)
#define RANGE(x, a, b) ((a) <= (x) && (x) < (b))
#define DUPLE(a, b, c, d)                                                      \
  (RANGE(a, c, d) || RANGE(b, c, d) || RANGE(c, a, b) || RANGE(d, a, b))
#define INCLU(a, b, c, d) (RANGE(a, c, d) && (b, c, d))
#define PW(x) ((x) * (x))
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define MODU 1000000007LL
#define bitcheck(a, b) ((a >> b) & 1)
#define bitset(a, b) (a |= (1 << b))
#define bitunset(a, b) (a &= ~(1 << b))
#define MP(a, b) make_pair((a), (b))
#define Manh(a, b) (abs((a).first-(b).first) + abs((a).second - ((b).second))
#define pritnf printf
#define scnaf scanf
#define itn int
#define PI 3.141592653589

#define izryt bool
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}
pll Dir[8] = { // 移動
    {0, 1}, {-1, 0}, {1, 0}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};

//[a, b)
#define Getsum(ar, a, b) (ar[b] - ar[a])
#define INF 10000000000000000LL

struct Edge {
  int from, to;
  ll w;
};
typedef vector<vector<Edge>> Graph;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll m, ll n) { return m * n / gcd(m, n); }

ll gettugibaisu(ll from, ll k) {

  ll ret = ((from + k - 1) / k) * k;

  return ret;
}

ll getmaebaisu(ll to, ll k) {

  ll ret = (to / k) * k;

  return ret;
}
class Loli {
  char str[601];
  int hash[601], pw[601];
  int mod = 1000000007, base = 1007;
  int len;

public:
  Loli(){};

  void setstr(char *str2, int n) {
    strcpy(str, str2);
    len = n;
    hash[0] = 0;
    pw[0] = 1;
    rep(i, 1, len + 1) {
      pw[i] = ((ll)pw[i - 1] * base) % mod;
      hash[i] = ((ll)hash[i - 1] * base + (int)str[i - 1]) % mod;
    }
  }

  inline int gethash(int l, int r) { // 半開
    return (hash[r] - (ll)hash[l] * pw[r - l] % mod + mod) % mod;
  }
};

signed main(void) {

  int n;
  cin >> n;
  int l = 2 * n - 1;
  char s[599][599];
  Loli lsh[599], lsv[599];

  REP(i, n) {
    char st[301];
    scanf("%s", st);
    strcpy(s[i], st);
    strcpy(s[i] + n, st);
    if (i != n - 1)
      strcpy(s[i + n], s[i]);
  }
  REP(i, l) { lsh[i].setstr(s[i], l); }
  REP(i, l) {
    char buf[600];
    REP(j, l) { buf[j] = s[j][i]; }
    lsv[i].setstr(buf, l);
  }

  int cou = 0;
  REP(x, l - n + 1) {
    REP(y, l - n + 1) {
      bool f = true;
      REP(i, n) {
        if (lsh[y + i].gethash(x + 1 + i, x + n) !=
            lsv[x + i].gethash(y + 1 + i, y + n)) {
          f = false;
          break;
        }
      }
      cou += f;
    }
  }

  printf("%d\n", cou);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>
#define rep(i, s, n) for (int i = (s); (n) > i; i++)
#define REP(i, n) rep(i, 0, n)
#define RANGE(x, a, b) ((a) <= (x) && (x) < (b))
#define DUPLE(a, b, c, d)                                                      \
  (RANGE(a, c, d) || RANGE(b, c, d) || RANGE(c, a, b) || RANGE(d, a, b))
#define INCLU(a, b, c, d) (RANGE(a, c, d) && (b, c, d))
#define PW(x) ((x) * (x))
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define MODU 1000000007LL
#define bitcheck(a, b) ((a >> b) & 1)
#define bitset(a, b) (a |= (1 << b))
#define bitunset(a, b) (a &= ~(1 << b))
#define MP(a, b) make_pair((a), (b))
#define Manh(a, b) (abs((a).first-(b).first) + abs((a).second - ((b).second))
#define pritnf printf
#define scnaf scanf
#define itn int
#define PI 3.141592653589

#define izryt bool
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}
pll Dir[8] = { // 移動
    {0, 1}, {-1, 0}, {1, 0}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};

//[a, b)
#define Getsum(ar, a, b) (ar[b] - ar[a])
#define INF 10000000000000000LL

struct Edge {
  int from, to;
  ll w;
};
typedef vector<vector<Edge>> Graph;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll m, ll n) { return m * n / gcd(m, n); }

ll gettugibaisu(ll from, ll k) {

  ll ret = ((from + k - 1) / k) * k;

  return ret;
}

ll getmaebaisu(ll to, ll k) {

  ll ret = (to / k) * k;

  return ret;
}
class Loli {
  char str[601];
  int hash[601], pw[601];
  int mod = 1000000007, base = 1007;
  int len;

public:
  Loli(){};

  void setstr(char *str2, int n) {
    strcpy(str, str2);
    len = n;
    hash[0] = 0;
    pw[0] = 1;
    rep(i, 1, len + 1) {
      pw[i] = ((ll)pw[i - 1] * base) % mod;
      hash[i] = ((ll)hash[i - 1] * base + (int)str[i - 1]) % mod;
    }
  }

  inline int gethash(int l, int r) { // 半開
    return (hash[r] - (ll)hash[l] * pw[r - l] % mod + mod) % mod;
  }
};

signed main(void) {

  int n;
  cin >> n;
  int l = 2 * n - 1;
  char s[601][601];
  Loli lsh[601], lsv[601];

  REP(i, n) {
    char st[301];
    scanf("%s", st);
    strcpy(s[i], st);
    strcpy(s[i] + n, st);
    if (i != n - 1)
      strcpy(s[i + n], s[i]);
  }
  REP(i, l) { lsh[i].setstr(s[i], l); }
  REP(i, l) {
    char buf[600];
    REP(j, l) { buf[j] = s[j][i]; }
    lsv[i].setstr(buf, l);
  }

  int cou = 0;
  REP(x, l - n + 1) {
    REP(y, l - n + 1) {
      bool f = true;
      REP(i, n) {
        if (lsh[y + i].gethash(x + 1 + i, x + n) !=
            lsv[x + i].gethash(y + 1 + i, y + n)) {
          f = false;
          break;
        }
      }
      cou += f;
    }
  }

  printf("%d\n", cou);
  return 0;
}