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 Rust (1.15.1) (judge verdict: Accepted)

#[allow(unused_imports)]
use std::cmp::{min,max};
#[allow(unused_imports)]
use std::collections::BTreeMap;
#[allow(unused_imports)]
use std::ops::*;
#[allow(unused_imports)]
use std::collections::BinaryHeap;

fn main() {
    let n = read_i64() as usize;
    let ss: Vec<u8> = read_line().bytes().collect();
    let mut res = 0;
    let mut xs = vec![];
    let mut ys = vec![];
    let mut dp = [[0;19];19];
    //let mut dp = [[[0;19];19];20];
    for i in 0..1<<n {
        xs.clear();
        ys.clear();
        //for k in 0..20 {
            for ky in 0..19 {
                for kx in 0..19 {
                    //dp[k][ky][kx] = 0i64;
                    dp[ky][kx] = 0i64;
                }
            }
        //}
        for k in 0..n {
            if i & (1 << k) == 0 {
                xs.push(ss[k]);
            }
            else {
                ys.push(ss[k]);
            }
        }
        //eprintln!("{:?} {:?}", xs, ys);
        //dp[0][ys.len()][xs.len()] = 1;
        dp[ys.len()][xs.len()] = 1;
        for k in 0..n {
            for ky in 0..ys.len()+1 {
                for kx in 0..xs.len()+1 {
                    if kx >= 1 && xs[kx-1] == ss[n+k] {
                        //dp[k+1][ky][kx-1] += dp[k][ky][kx];
                        dp[ky][kx-1] += dp[ky][kx];
                    }
                    if ky >= 1 && ys[ky-1] == ss[n+k] {
                        //dp[k+1][ky-1][kx] += dp[k][ky][kx];
                        dp[ky-1][kx] += dp[ky][kx];
                    }
                    //eprintln!("k={}, ky={}, kx={}, dp={}", k, ky, kx, dp[k][ky][kx]);
                }
            }
        }
        res += dp[0][0];
    }
    println!("{}", res);
}

#[allow(dead_code)]
fn read_line() -> String {
    let mut ret = String::new();
    std::io::stdin().read_line(&mut ret).ok();
    ret.pop();
    return ret;
}

#[allow(dead_code)]
fn read_i64() -> i64 {
    let ss = read_line();
    return ss.parse::<i64>().unwrap();
}

#[allow(dead_code)]
fn read_vec_i64() -> Vec<i64> {
    let mut res = vec![];
    let ss = read_line();
    for ts in ss.split_whitespace() {
        let x = ts.parse::<i64>().unwrap();
        res.push(x);
    }
    return res;
}

use std::fmt::Display;
#[allow(dead_code)]
fn write_vec<T: Display>(xs: &Vec<T>) {
    if xs.len() == 0 {
        println!("");
        return;
    }
    print!("{}", xs[0]);
    for i in 1..xs.len() {
        print!(" {}", xs[i]);
    }
    println!("");
}