ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2020-04-16 17:00:09  阅读:275  来源: 互联网

标签:Building Palindromes const string int ++ 101532K include define


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.

A 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;
    }
}

 



标签:Building,Palindromes,const,string,int,++,101532K,include,define
来源: https://www.cnblogs.com/AlexLINS/p/12713957.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有