CSU1563 Lexicography

本文介绍了一种算法,用于解决给定字符串后找出其所有不同字母组合中第K个排列的问题。通过逐步枚举并计算剩余字符的排列数量来确定每个位置上的字符。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 alphabetical order:

ACM
AMC
CAM
CMA
MAC
MCA
As another example, the string ICPC has the following 12 anagrams (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 determine the Kth such anagram according to alphabetical order.

Input

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

Output

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

Sample Input

ACM 5
ICPC 12
REGION 274
# 0

Sample Output

MAC
PICC
IGNORE

Hint

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

——————————————————————————

题目的意思是将一个字符串进行全排列去重,输出第k个字符串

思路:依次枚举每一位,每次计算剩下的数构成的排列个数,与k比较,若比k大则该位就是这个字符,否则继续枚举。带重复的全排列个数为n/(每个数字个数的阶乘之积)

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define LL long long
const LL mod=1e9+7;
const int INF=0x3f3f3f3f;
#define MAXN 100005

char s[20];
char ss[20];
LL f[18];
struct node
{
    char s;
    int t;
} p[20];
LL k;
int n;

int main()
{
    f[0]=1;
    for(int i=1; i<17; i++)
    {
        f[i]=f[i-1]*i;
    }
    while(~scanf("%s%lld",s,&k))
    {
        if(strcmp(s,"#")==0&&k==0)
            break;
        int n=strlen(s);
        sort(s,s+n);
        int  cnt=0;
        p[0].s=s[0];
        p[0].t=1;
        for(int i=1; i<n; i++)
        {
            if(s[i]==s[i-1])
            {
                p[cnt].t++;
            }
            else
            {
                p[++cnt].s=s[i];
                p[cnt].t=1;
            }
        }
        for(int i=0; i<n; i++)
        {
            LL ans=0;
            for(int j=0; j<=cnt; j++)
            {
                if(p[j].t==0)
                    continue;
                p[j].t--;
                LL ct=f[n-i-1];
                for(int kk=0; kk<=cnt; kk++)
                {
                    ct/=f[p[kk].t];
                }
                ans+=ct;
                if(ans>=k)
                {
                    ans-=ct;
                    k-=ans;
                    ss[i]=p[j].s;
                    break;
                }
                else
                    p[j].t++;
            }
        }
        for(int i=0; i<n; i++)
            printf("%c",ss[i]);
        printf("\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值