UVALive6814 Lexicography

An anagram of a string is any string that can be formedusing the same letters as the original. (We consider the original string ananagram of itself as well.) For example, the string
ACM has thefollowing 6 anagrams, as given in alphabetical order:

 
ACM
AMC
CAM
CMA
MAC
MCA

As another example, the string ICPC has the following 12anagrams (in alphabetical order):

 
CCIP
CCPI
CICP
CIPC
CPCI
CPIC
ICCP
ICPC
IPCC
PCCI
PCIC
PICC

Given a string and a rank K, you are to determinethe Kth such anagram according to alphabetical order.

Input: Each test case willbe designated on a single line containing the original word followed by thedesired rank
K. Words will use uppercase letters (i.e., A through Z) and willhave length at most 16. The value of
K will be in the range from 1 tothe number of distinct anagrams of the given word. A line of the form "# 0"designates the end of the input.

Warning: The value of Kcould be almost 245 in the largest tests, so you should use type
long in Java,or type long long in C++ to store K.

Output: For each test,display the Kth anagram of the original string.


Example Input:


Example Output:


ACM 5

ICPC 12

REGION 274

# 0


MAC

PICC

IGNORE

#include <stdio.h>
#include <iostream>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#define ll long long
#define N 50

using namespace std;

char str[N];
ll k;
int num[N];
ll f[N];

ll fun(int x)
{
    ll ans=f[x];
    for(int i=0;i<26;i++)
    ans/=f[num[i]];//除以相同个数的阶乘

    return ans;
}

void solve(int len)
{

    for(int i=0;i<len;i++)
    {
        for(int j=0;j<26;j++)
        {
            if(num[j])
            {
                num[j]--;//当前被占用了一个
                ll a=fun(len-i-1);

                num[j]++;
                if(a>=k)
                {
                    num[j]--;
                    printf("%c",'A'+j);
                    break;
                }
                else
                k-=a;
            }
        }
    }
    cout<<endl;

}

int main()
{
    f[0]=1;
    for(int i=1;i<=17;i++)//阶乘
    f[i]=f[i-1]*i;

    while(~scanf("%s %lld",str,&k))
    {
        if(k==0) break;

        int len=strlen(str);
        memset(num,0,sizeof num);

        for(int i=0;i<len;i++)
        {
            num[str[i]-'A']++;
        }
        solve(len);
    }
    return 0;
}
时间: 2024-08-28 03:44:02

UVALive6814 Lexicography的相关文章

CSU 1563: Lexicography (数学计数问题)

1563: Lexicography Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 342  Solved: 111 [Submit][Status][Web Board] Description An anagram of a string is any string that can be formed using the same letters as the original. (We consider the original stri

Lexicography (UVALive 6814)

Lexicography Time Limit: 1000MS   Memory Limit: 131072KB   64bit IO Format: %lld & %llu Submit Status Description An anagram of a string is any string that can be formed using the same letters as the original. (We consider the original string an anag

Lexicography(数学推论&gt;&gt;求按字典序排第k个排列)

Lexicography Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format:%lld & %llu Submit Status Practice CSU 1563 Description An anagram of a string is any string that can be formed using the same letters as the original. (We consider the orig

SDUT 3185 Lexicography(排列问题(不会))

Lexicography Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 An anagram of a string is any string that can be formed using the same letters as the original. (We consider the original string an anagram of itself as well.) For example, the s

CSUOJ--1563: Lexicography

这个题目就是求一个序列的第k个字典序排序,因为k比较大所以我们不能穷举,而是用其它的方法求. 代码如下: #include<iostream> #include<cstdio> #include<cstring> using namespace std; #define ll long long char str[20]; int cc[30]; ll fac[20],k; ll GetNum(int cnt) { ll ans=fac[cnt]; for(int i=

Lexicography

An anagram of a string is any string that can be formed using the same letters as the original. (We consider the original string an anagram of itself as well.) For example, the string ACM has the following 6 anagrams, as given in alphabetical order:

CSU1563:Lexicography(数学)

Description An anagram of a string is any string that can be formed using the same letters as the original. (We consider the original string an anagram of itself as well.) For example, the string ACM has the following 6 anagrams, as given in alphabet

csu1563: Lexicography以及找出多重集的第k个串的讲解

Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 162  Solved: 52 [Submit][Status][Web Board] Description An anagram of a string is any string that can be formed using the same letters as the original. (We consider the original string an anagram of its

根据76大细分词性对单词进行归组(二)

词性的重要性不言而喻,尤其是对于自然语言处理来说,哪怕就是记单词,根据词性对单词进行归组也是非常有帮助的. superword是一个Java实现的英文单词分析软件,主要研究英语单词音近形似转化规律.前缀后缀规律.词之间的相似性规律等等. 各大词性及其包括的词: 32.N-COUNT-COLL(可数集合名词) (词数:50) 1 aristocracy army array audience band 2 cast chapter command commission committee 3 co