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. Create and format Word documents using R software and Reporters package

    http://www.sthda.com/english/wiki/create-and-format-word-documents-using-r-software-and-reporters-pa ...

  2. React生命周期执行顺序详解

    文章内容转载于https://www.cnblogs.com/faith3/p/9216165.html 一.组件生命周期的执行次数是什么样子的??? 只执行一次: constructor.compo ...

  3. burp suite 的intruder 四种攻击方式

    一:sniper[狙击手] 这种攻击基于原始的请求内容,需要一个字典,每次用字典里的一个值去代替一个待攻击的原始值. 攻击次数=参数个数X字典内元素个数 例如:原始请求中 name=aa , pass ...

  4. Unity Shaderlab: Object Outlines 转

    转 https://willweissman.wordpress.com/tutorials/shaders/unity-shaderlab-object-outlines/ Unity Shader ...

  5. mongdb使用技巧

    进入shell的方法:mongo 命令   # 使用系统服务启动 mongodb /etc/init.d/mongod # 或 service mongod start # 或 service mon ...

  6. Spotlight 监控Linux服务器的性能

    Spotlight功能:详细的进程跟踪功能远程连接在线的Unix/Linux的调优指南事件日志跟踪配置警报 详细的进程跟踪功能:Spotlight对具体的Unix / Linux的进程长达24小时的历 ...

  7. vim的简单使用

    vim的学习曲线相当的大(参看各种文本编辑器的学习曲线),所以,如果你一开始看到的是一大堆VIM的命令分类,你一定会对这个编辑器失去兴趣的.下面的文章翻译自<Learn Vim Progress ...

  8. java与js交互,相互调用传参

    随着前端技术的发展与H5的广泛使用,移动端采用native+h5的方式越来越多了,对于Android来说就涉及到java与js的交互,相互调用传参等.下面就来看一下java与js交互的简单demo. ...

  9. nRF52832的SAADC

    SAADC部分思维导图 1ADC原理 1.1主要特点 1)8/10/12分辨率,使用过采样可达到14位分辨率 2)多达8个通道 单端输入时使用1个通道,2个通道可组成差分输入 单端和差分输入时均可配置 ...

  10. html5实现获取地理位置信息并定位

    这里主要讲h5实现获取地理位置信息并定位功能,本文讲解了原生h5,百度地图,谷歌地图等三种获取地理信息并定位的方法,需要的朋友可以参考下: h5提供了地理位置功能(Geolocation API),能 ...