首页 > 其他 > 详细

Palindromes Building Gym - 101532K (计算回文串数目)

时间:2020-04-16 17:19:45      阅读:95      评论:0      收藏:0      [点我收藏+]

An anagram is a word or phrase formed by rearranging the letters of another word or phrase, using all the original letters exactly once, such as "post", "stop", and "spot".

You are given a string s consisting of lowercase English letters. Your task is to count how many distinct anagrams of the string s are palindromes.

palindrome is a word, phrase, number, or other sequence of characters which reads the same backward as forward, such as "madam" or "racecar".

For example, "aabb" has 6 distinct anagrams, which are: "aabb", "abab", "abba", "baab", "baba", "bbaa". Two of the previous anagrams are palindromes, which are: "abba", "baab".

Input

The first line contains an integer T, where T is the number of test cases.

The first line of each test case contains an integer n (1 ≤ n ≤ 20), where n is the length of the string s.

The second line of each test contains a string s of length n consisting of lowercase English letters.

Output

For each test case, print a single line containing how many distinct anagrams of the string s are palindromes.

Example

Input
5
4
aabb
6
ababbc
6
abaaba
12
babacbcbcaca
14
aaaabbcaaaabbc
Output
2
0
3
90
105

技术分享图片

 

 代码:

#include <iostream>
#include <algorithm>
#include <string.h>
#include <cstdio>
#include <string>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
//#include <unordered_map>
#define Fbo friend bool operator < (node a, node b)
#define mem(a, b) memset(a, b, sizeof(a))
#define FOR(a, b, c) for (int a = b; a <= c; a++)
#define RFOR(a, b, c) for (int a = b; a >= c; a--)
#define off ios::sync_with_stdio(0)
#define sc(a) scanf("%d",&a)
#define pr(a) printf("%d\n",a);
#define SC(n,m) scanf("%d%d",&n,&m)
bool check1(int a) { return (a & (a - 1)) == 0 ? true : false; }

using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
const int INF = 0x3f3f3f3f;//1e10
const int mod = 1e9 + 7;
const int Maxn = 1e5 + 5;
const int N = 2e5 + 5;
const double pi = acos(-1.0);
const double eps = 1e-8;

ll f[20];
ll a[30], b[30];

void factorial(){//阶乘
    f[0] = 1;
    for (int i = 1; i <= 11; i++) {
        f[i] = f[i - 1] * i;
    }
}

int main(){
    factorial();
    int t;
    cin >> t;
    while (t--) {
        mem(a, 0);
        mem(b, 0);
        int n, k = 0;
        string s;
        cin >> n >> s;
        for (int i = 0; i < s.size(); i++) {
            a[s[i] - a]++;
        }
        ll odd = 0, cnt = 0;
        ll ans = 0;
        FOR(i, 0, 26) {
            if (a[i]&1) odd++;
            if (a[i]) b[k++] = a[i] / 2;
            ans += a[i] / 2;
        }
        if (n & 1 && odd == 1 || (n % 2 == 0 && odd == 0)) {
            cnt = f[ans];
            for (int i = 0; i < k; i++) {
                if (b[i]) {
                    cnt /= f[b[i]];
                }
            }
        }
        cout << cnt << endl;
    }
}

 



Palindromes Building Gym - 101532K (计算回文串数目)

原文:https://www.cnblogs.com/AlexLINS/p/12713957.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!