Problem p03298 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> II;
typedef pair<long long, int> LLI;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<II> VII;

#define For(i, a, b) for (int i = a; i <= b; i++)
#define Rep(i, a, b) for (int i = a; i >= b; i--)
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, f) for (auto i : f)
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define BUG(x) (cerr << #x << " = " << x << "\n")
#define ARRAY(f)                                                               \
  {                                                                            \
    cerr << #f << " = ";                                                       \
    FOR(i, f) cout << i << ' ';                                                \
    cout << "\n";                                                              \
  }
#define sz(s) int(s.size())
#define reset(f, x) memset(f, x, sizeof(f))
#define all(x) x.begin(), x.end()
#define two(x) (1LL << x)
#define bit(x, i) ((x >> (i)) & 1LL)
#define onbit(x, i) (x | (1LL << (i)))
#define offbit(x, i) (x & ~(1 << (i)))

const int N = 18;
const int p = 37;

int n;
string s;
map<pair<LL, LL>, int> cnt;
LL res = 0;

int main() {
  ios_base::sync_with_stdio(false);
  srand(time(NULL));
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  // freopen("","w",stdout);
#endif
  cin >> n;
  cin >> s;
  string s1 = s.substr(0, n), s2 = s.substr(n, n);
  // BUG(s1); BUG(s2);
  REP(tt, two(n)) {
    LL h1 = 0, h2 = 0;
    REP(i, n)
    if (!bit(tt, i))
      h1 = h1 * p + s1[i] - 'a' + 1;
    Rep(i, n - 1, 0) if (bit(tt, i)) h2 = h2 * p + s1[i] - 'a' + 1;
    cnt[make_pair(h1, h2)]++;
  }
  REP(tt, two(n)) {
    LL h1 = 0, h2 = 0;
    Rep(i, n - 1, 0) if (bit(tt, i)) h1 = h1 * p + s2[i] - 'a' + 1;
    REP(i, n)
    if (!bit(tt, i))
      h2 = h2 * p + s2[i] - 'a' + 1;
    if (cnt.find(mp(h1, h2)) != cnt.end()) {
      // cerr << tt << ' ' << h1 << ' ' << h2 << ' ' << cnt[mp(h1, h2)] << "\n";
      res += cnt[mp(h1, h2)];
    }
  }
  cout << res << "\n";
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> II;
typedef pair<long long, int> LLI;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<II> VII;

#define For(i, a, b) for (int i = a; i <= b; i++)
#define Rep(i, a, b) for (int i = a; i >= b; i--)
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, f) for (auto i : f)
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define BUG(x) (cerr << #x << " = " << x << "\n")
#define ARRAY(f)                                                               \
  {                                                                            \
    cerr << #f << " = ";                                                       \
    FOR(i, f) cout << i << ' ';                                                \
    cout << "\n";                                                              \
  }
#define sz(s) int(s.size())
#define reset(f, x) memset(f, x, sizeof(f))
#define all(x) x.begin(), x.end()
#define two(x) (1LL << x)
#define bit(x, i) ((x >> (i)) & 1LL)
#define onbit(x, i) (x | (1LL << (i)))
#define offbit(x, i) (x & ~(1 << (i)))

const int N = 18;
const int p = 37;

int n;
string s;
map<pair<LL, LL>, int> cnt;
LL res = 0;

int main() {
  ios_base::sync_with_stdio(false);
  srand(time(NULL));
#ifndef ONLINE_JUDGE
  // freopen("in.txt","r",stdin);
  // freopen("","w",stdout);
#endif
  cin >> n;
  cin >> s;
  string s1 = s.substr(0, n), s2 = s.substr(n, n);
  // BUG(s1); BUG(s2);
  REP(tt, two(n)) {
    LL h1 = 0, h2 = 0;
    REP(i, n)
    if (!bit(tt, i))
      h1 = h1 * p + s1[i] - 'a' + 1;
    Rep(i, n - 1, 0) if (bit(tt, i)) h2 = h2 * p + s1[i] - 'a' + 1;
    cnt[make_pair(h1, h2)]++;
  }
  REP(tt, two(n)) {
    LL h1 = 0, h2 = 0;
    Rep(i, n - 1, 0) if (bit(tt, i)) h1 = h1 * p + s2[i] - 'a' + 1;
    REP(i, n)
    if (!bit(tt, i))
      h2 = h2 * p + s2[i] - 'a' + 1;
    if (cnt.find(mp(h1, h2)) != cnt.end()) {
      // cerr << tt << ' ' << h1 << ' ' << h2 << ' ' << cnt[mp(h1, h2)] << "\n";
      res += cnt[mp(h1, h2)];
    }
  }
  cout << res << "\n";
}