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 Scala (2.11.7) (judge verdict: Accepted)

import java.io._
import java.util.StringTokenizer
import scala.util.control.Breaks.{breakable,break}
import scala.collection.mutable.Map

object Main extends App {
  val in = new InputReader(System.in)
  val pw = new PrintWriter(System.out)

  private[this] val N = in.next().toInt
  private[this] val S = in.next()

  private[this] val left = S.substring(0, N)
  private[this] val right = S.substring(N, 2*N).reverse

  private[this] val dp1: Map[(String, String), Long] = Map()
  private[this] val dp2: Map[(String, String), Long] = Map()

  for(k <- 0 until Math.pow(2, N).round.toInt) {
    val tmp = k.toBinaryString
    val cond: String = "0"*(N - tmp.length) + tmp

    val l1 = new StringBuilder()
    val l2 = new StringBuilder()
    val r1 = new StringBuilder()
    val r2 = new StringBuilder()
    for(i <- 0 until N) {
      if(cond(i) == '0') {
        r1.append(left(i))
        r2.append(right(i))
      }
      else {
        l1.append(left(i))
        l2.append(right(i))
      }
    }

    val key1 = (l1.toString, r1.toString)
    val key2 = (l2.toString, r2.toString)
    if(dp1.contains(key1)) {
      dp1(key1) += 1
    }
    else {
      dp1 += ((key1, 1))
    }

    if(dp2.contains(key2)) {
      dp2(key2) += 1
    }
    else {
      dp2 += ((key2, 1))
    }
  }

  // val _ = ???

  var ans = 0L
  for((key, v1) <- dp1) {
    if(dp2.contains(key)) {
      ans += v1 * dp2(key)
    }
  }
  println(ans)
}


class InputReader(stream: InputStream) {
  private val reader = new BufferedReader(new InputStreamReader(stream))
  private var tokenizer: StringTokenizer = new StringTokenizer(reader.readLine())
  def next(): String = {
    while (!tokenizer.hasMoreTokens()) {
      tokenizer = new StringTokenizer(reader.readLine())
    }
    tokenizer.nextToken()
  }
}