Problem:
Score : 600 points

Problem Statement

You are given a string S of length 2N consisting of lowercase English letters.

There are 2^{2N} ways to color each character in S red or blue. Among these ways, how many satisfy the following condition?

The string obtained by reading the characters painted red from left to right is equal to the string obtained by reading the characters painted blue from right to left.

Constraints

1 \leq N \leq 18

The length of S is 2N.

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the number of ways to paint the string that satisfy the condition.

Sample Input 1

4
cabaacba

Sample Output 1

4

There are four ways to paint the string, as follows:

cabaacba

cabaacba

cabaacba

cabaacba

Sample Input 2

11
mippiisssisssiipsspiim

Sample Output 2

504

Sample Input 3

4
abcdefgh

Sample Output 3

0

Sample Input 4

18
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

Sample Output 4

9075135300

The answer may not be representable as a 32-bit integer.

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <string>
#include <algorithm>
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <cmath>
#include <cassert>
#include <queue>
#include <bitset>
#include <map>
#include <set>
#include <unordered_map>

#define pb push_back
#define mp make_pair
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(),(v).end()

using namespace std;

typedef long long int64;
typedef pair<int, int> ii;
typedef vector<int> vi;

const int64 X = 2184057LL;

int main() {
  int n;
  string s;
  cin >> n >> s;
  unordered_map<int64, int> left;
  vector<int64> p(2 * n + 2);
  p[0] = 1;
  for (int i = 1; i < sz(p); ++i)
    p[i] = p[i - 1] * X;
  for (int mask = 0; mask < (1 << n); ++mask) {
    int64 h1 = 0, h2 = 0;
    int cnt = 0;
    for (int i = 0; i < n; ++i) {
      if ((mask >> i) % 2)
        h1 = h1 * X + s[i];
      else
        h2 += p[cnt++] * s[i];
    }
    int64 h = h1 * 98729321 + h2;
    left[h]++;
  }

  int64 res = 0;
  for (int mask = 0; mask < (1 << n); ++mask) {
    int64 h1 = 0, h2 = 0;
    int cnt = 0;
    for (int i = 0; i < n; ++i) {
      if ((mask >> i) % 2)
        h2 = h2 * X + s[n + i];
      else
        h1 += p[cnt++] * s[n + i];
    }
    int64 h = h1 * 98729321 + h2;
    if (left.count(h))
      res += left[h];
  }
  cout << res << "\n";
  return 0;
}