2013-04-17 10:16:34Morris

[UVA][數學] 11027 - Palindromic Permutation

Problem A

Palindromic Permutation

Time limit: 1 second

 

Given a string of characters, we can permute the individual characters to make new strings. We can then order these strings into alphabetical order.

 

For example the string ‘abba’ gives rise to the following 6 distinct permutations in alphabetical order.

 

aabb 1

abab 2

abba 3

baab 4

baba 5

bbaa 6

 

Of these 6 permutations, only 2 are palindromes (A string that reads the same when read backwards). These are ‘abba’ and ‘baab’.

 

Given a string, you have to find out the nth palindrome in the sorted list of all permutations. For the above case ‘abba’ is the 1st and ‘baab’ is the 2nd palindrome.

 

Input

 

The first line of input gives the number of test cases. Each case contains a string, consisting of lowercase letters only, followed by a space separated positive integer n (n < 2^31). The length of the string will be at most 30.

 

Output

 

For each case, output the case number followed by the nth palindrome, but if the total number of palindromes is less than n output ‘XXX’ without the quotes. Follow the sample for exact format.

 

Sample Input

Output for Sample Input

3

abba 1

abba 2

abba 3

Case 1: abba

Case 2: baab

Case 3: XXX

 

 

 

ProblemSetter: Sohel Hafiz

Alternate Solution: Jane Alam Jan

說要求回文,事實上只要切一半找字典順序第 k 大的排列就好。

奇數長度要特別考慮,而奇數個數字元最多只能有一個。


#include <stdio.h>
#include <string.h>
int main() {
    int t, cases = 0;
    long long pk;
    char s[105];
    scanf("%d", &t);
    long long f[30] = {1};
    int i, j, k;
    for(i = 1; i < 20; i++)
        f[i] = f[i-1]*i;
    while(t--) {
        scanf("%s %lld", s, &pk);
        printf("Case %d: ", ++cases);
        int len = strlen(s);
        int cntA[26] = {};
        int odd = 0, oddi;
        for(i = 0; i < len; i++)
            cntA[s[i]-'a']++;
        for(i = 0; i < 26; i++) {
            if(cntA[i]%2)   odd++, oddi = i;
            cntA[i] /= 2;
        }
        if(odd > 1) {
            puts("XXX");
            continue;
        }
        len /= 2;
        char res[105];
        int pre;
        long long comb = f[len];
        for(i = 0; i < 26; i++)
            comb /= f[cntA[i]];
        if(comb < pk) {
            puts("XXX");
            continue;
        }
        for(i = 0; i < len; i++) {
            for(j = 0, pre = 0; j < 26; j++) {
                if(cntA[j]) {
                    long long comb = f[len-i-1];
                    cntA[j]--;
                    for(k = 0; k < 26; k++)
                        comb /= f[cntA[k]];
                    cntA[j]++;
                    pre = j;
                    pk -= comb;
                    if(pk <= 0) {
                        pk += comb;
                        break;
                    }
                }
            }
            while(cntA[pre] == 0)
                pre++;
            res[i] = pre+'a';
            cntA[pre]--;
        }
        if(odd) {
            res[len] = oddi+'a';
            for(i = 0; i < len; i++)
                res[len+i+1] = res[len-i-1];
            res[2*len+1] = '\0';
        } else {
            for(i = 0; i < len; i++)
                res[2*len-i-1] = res[i];
            res[2*len] = '\0';
        }
        puts(res);
    }
    return 0;
}