UVA11027 Palindromic Permutation回文

Posted 海岛Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA11027 Palindromic Permutation回文相关的知识,希望对你有一定的参考价值。

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 1-st and ‘baab’ is the 2-nd 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 < 231). 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
3
abba 1
abba 2
abba 3
Sample Output
Case 1: abba
Case 2: baab
Case 3: XXX

问题链接UVA11027 Palindromic Permutation
问题简述:给定字符串和正整数n,字符串中的字母排序后可以组成若干的字符串,其中一部分字符串为回文串。输出第n个回文串,若不存第n个回文串则输出“XXX”。
问题分析:看解题程序代码,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA11027 Palindromic Permutation */

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
const int N = 30 + 1;
const int L = 26;
char s[N], mid;
int book[L], ans[L], n, len;

LL fact(int n)
{
    LL ret = 1;
    for (int i = 2; i <= n; i++)
        ret *= i;
    return ret;
}

void solve()
{
    LL a = fact(len), b = 1;
    for (int i = 0; i < L; i++)
        if (book[i]) b *= fact(book[i]);
    if (n > a / b) printf("XXX\\n");
    else {
        int cur = 1;
        while (cur <= len) {
            for (int i = 0; i < L; i++)
                if (book[i]) {
                    book[i]--;
                    LL t = fact(len - cur);
                    for (int j = 0; j < L; j++)
                        if (book[j]) t /= fact(book[j]);
                    if (t < n) n -= t, book[i]++;
                    else {
                        ans[cur] = i;
                        break;
                    }
                }
            cur++;
        }

        for (int i = 1; i <= len; i++)
            printf("%c", ans[i] +'a');
        if (mid) printf("%c", mid);
        for (int i = len; i >= 1; i--)
            printf("%c", ans[i] + 'a');
        printf("\\n");
    }
}

int main()
{
    int t, caseno = 0;
    scanf("%d", &t);
    while (t--) {
        scanf("%s%d", s, &n);

        memset(book, 0, sizeof book);
        for (int i = 0; s[i]; i++)
            book[s[i] - 'a']++;

        int cnt = 0;
        mid = 0;
        for (int i = 0; i < L; i++) {
            if (book[i] & 1) mid = i + 'a', cnt++;
            book[i] >>= 1;
        }
        len = strlen(s) / 2;

        printf("Case %d: ", ++caseno);
        if (cnt > 1) printf("XXX\\n");
        else solve();
    }

    return 0;
}

以上是关于UVA11027 Palindromic Permutation回文的主要内容,如果未能解决你的问题,请参考以下文章

UVA 11404 Palindromic Subsequence[DP LCS 打印]

Longest Palindromic Substring & Longest Palindromic Subsequence

1.2.4 Palindromic Squares

Longest Palindromic Substring

全排列问题的递归算法(Perm)

LeetcodeLongest Palindromic Substring