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.

假设字符共有n类,个数分别为m1,m2……,mn,
    那么这个多重集的全排列个数为(m1+m2+……+mn)!/m1!/m2!/……/mn!
    
    然后呢,我们来举个例子,要知道字符集"ICPC"的第12个字符串是多少,
    
    对第一位做出假设
    
    C的话,那么有3!=6种串,
    I的话,有3!/2!=3种串,
    P的话,有3!/2!=3种串,
    
    可知6+3+3=12,那么第一位就是P,从剩下的字符集里找出第12-6-3=3个字符串
    
    现在字符集变成"ICC",
    
    对第二位做出假设
    
    C的话,那么有2!=2种串,
    I的话,有2!/2!=1种串,
    
    可知2+1=3,所以,第二位就是I,从剩下的字符集里找出第3-2=1个字符串
    
    现在字符集变成"CC",
    
    对第三位做出架设
    
    C的话,那么有1!=1种串,
    
    可知1=1,于是,第三位就是C,从剩下的字符集里找出第1-1=0个字符串,
    
    现在字符集变成”C",
    
    这个串并不存在,那么就是直接把剩下的那些字符,就是C补在最后就行了,
    
    很显然,此时剩下的字符集一定都是由某个字符重复组成。

#include <iostream>
#include <algorithm>
#include <string.h>
#include<stdio.h>
using namespace std;
int main()
{
char a[];
long long int n;
long long int f[];
f[]=;
for(int i=;i<;i++)
f[i]=f[i-]*i; while(cin>>a>>n)
{ if(a[]=='#'&&n==)
break;
int t=strlen(a);
int zimu[];
memset(zimu,,sizeof zimu);
for(int i=;i<t;i++)
{
zimu[a[i]-'A']++; }
for(int i=;i<t;i++)
{
long long int tot=;
for(int j=;j<;j++)
{
if(zimu[j])
{
long long int ppp= f[t-i-];
for(int k=;k<;k++)
{
if(k==j)
ppp=ppp/f[zimu[k]-];
else
ppp=ppp/f[zimu[k]];
}
if(tot+ppp>=n)
{
a[i]=j+'A';
n=n-tot;
zimu[j]--;
break;
}
else
tot=ppp+tot;
} } }
cout<<a<<endl; }
return ;
}

Lexicography的更多相关文章

  1. Lexicography(数学推论>>求按字典序排第k个排列)

    Lexicography Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format:%lld & %llu Submit  ...

  2. 组合数学(全排列)+DFS CSU 1563 Lexicography

    题目传送门 /* 题意:求第K个全排列 组合数学:首先,使用next_permutation 函数会超时,思路应该转变, 摘抄网上的解法如下: 假设第一位是a,不论a是什么数,axxxxxxxx一共有 ...

  3. csu 1563 Lexicography

    题意:给出一堆字母 问这些字母组成的字符串中第k大的 排列组合,具体看代码 //寒假集训被何柱大大踩好惨(>_<) #include<cstdio> #include<i ...

  4. Basic Tutorials of Redis(5) - Sorted Set

    The last post is mainly about the unsorted set,in this post I will show you the sorted set playing a ...

  5. hihoCoder 1385 : A Simple Job(简单工作)

    hihoCoder #1385 : A Simple Job(简单工作) 时间限制:1000ms 单点时限:1000ms 内存限制:256MB Description - 题目描述 Institute ...

  6. ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛 A Simple Job

    描述 Institute of Computational Linguistics (ICL), Peking University is an interdisciplinary institute ...

  7. hihoCoder 1385 A Simple Job

    #1385 : A Simple Job 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 Institute of Computational Linguistics (I ...

  8. UESTC_Ferris Wheel String 2015 UESTC Training for Search Algorithm & String<Problem L>

    L - Ferris Wheel String Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 43000/43000KB (Java/ ...

  9. zoj3204 connect them 最小生成树 暴力

    Connect them Time Limit: 1 Second      Memory Limit:32768 KB You have n computers numbered from 1 to ...

随机推荐

  1. MRO和C3算法

    本节主要内容: 1.python多继承 2.python经典类的MRO 3.python新式类的MRO,C3算法 4.super() 一.python多继承 在python中类与类之间可以有继承关系, ...

  2. C#解析html文档类库HtmlAgilityPack下载地址

    新:http://html-agility-pack.net/?z=codeplex 原:http://htmlagilitypack.codeplex.com/

  3. Node内核基本自带模块fs 文件的读写

    在node的内核中存在一些内置的模块 这些是最基本的服务端所必要的 1:node全局环境:global类似于浏览器端的window 2:文件读取模块:fs fs模块同时提供了异步和同步的方法. 'us ...

  4. 【BZOJ】3143: [Hnoi2013]游走

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3143 显然如果一条边期望被走过的次数越多,我们就应该给它的编号越小. 所以问题变为如何求每 ...

  5. Ubuntu 下 Python自由切换

    sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 100 sudo update-alternati ...

  6. [转][smart3d]Smart3D之手动配置 S3C 索引加载全部的OSGB瓦片数据

    转自:https://blog.csdn.net/u013719339/article/details/77840728/ 一.须知: S3C是Smart3D内部格式,实质上是一个分块模型的索引,可以 ...

  7. 求1000以内的质数c语言

    之前在做求1000以内的质数的时候,我们一般能想到的就是从3~(根号)no,逐一和no除,如果存在某个i使得  i|no成立的话,说明no不是质数(“i|no”是i整除除no的意思): 在<明解 ...

  8. leecode第二十一题(合并两个有序链表)

    /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...

  9. 淘宝开源的H5移动开发UI框架genie-ui

    官网地址: https://npm.taobao.org/package/genie-ui

  10. (转+整理)C#中使用GUID

    GUID(全局统一标识符)是指在一台机器上生成的数字,它保证对在同一时空中的所有机器都是唯一的.通常平台会提供生成GUID的API.生成算法很有意思,用到了以太网卡地址.纳秒级时间.芯片ID码和许多可 ...