Lexicography

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

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.

 #include<stdio.h>
#include<string.h>
#include<algorithm>
typedef long long ll ;
char a[] ;
int alp[] ;
char map[] ;
bool vis[] ;
ll k ;
int n ; ll fact (int n)
{
ll sum = ;
for (int i = ; i <= n ; i ++) {
sum *= 1ll * i ;
}
return sum ;
} ll calc (int x)
{
int cnt = ;
for (int i = ; i < n ; i++) {
if (!vis[i]) cnt ++ ;
}
ll sum = ;
for (int i = ; i < ; i++) {
if (i + 'A' == a[x]) {
if ( alp[i] - > ) sum *= fact (alp[i] - ) ;
}
else {
if (alp[i] > ) sum *= fact (alp[i]) ;
}
}
// printf ("cnt=%d, sum=%lld\n" , cnt , sum ) ;
return fact (cnt - ) / sum ;
} void dfs (int deep)
{
if (deep == n) return ;
for (int i = ; i < n; i ++) {
// printf ("deep=%d,%c\n" , deep , a[i]) ;
if (!vis[i] ) {
if (k - calc (i) > ) {
// printf ("%c , k=%lld , fact=%lld\n" , a[i] , k , calc (i)) ;
k -= calc ( i ) ;
}
else {
if (k == ) { }
vis[i] = ;
alp[a[i] - 'A'] -- ;
map[deep] = a[i] ;
// printf ("k=%d\n" , k ) ;
// printf ("deep=%d , %c\n" , deep , map[deep]) ;
dfs (deep + ) ;
return ;
}
while (a[i + ] == a[i]) i ++ ;
}
}
} int main ()
{
// freopen ("a.txt" , "r" , stdin ) ;
while (scanf ("%s" , a) != NULL) {
scanf ("%lld" , &k) ;
if (a[] == '#' && k == ) break ;
memset (alp , , sizeof(alp)) ;
for (int i = ; a[i] != '\0' ; i ++) {
alp[a[i] - 'A'] ++ ;
}
n = strlen (a) ;
std::sort (a , a + n ) ;
/* for (int i = 0 ; i < n ; i++) {
printf ("%c" , a[i]) ;
} puts ("") ;*/
dfs () ;
memset (vis , , sizeof(vis)) ;
for (int i = ; i < n ; i ++) printf ("%c" , map[i]) ; puts ("") ;
}
return ;
}

Lexicography(数学推论>>求按字典序排第k个排列)的更多相关文章

  1. LeetCode 386——字典序排数

    1. 题目 2. 解答 2.1 方法一 假设返回 118 以内数的字典顺序,则为 1,10,100,101,102,...,109,11,110,111,112,...,118,12,13,....根 ...

  2. Java实现 LeetCode 386 字典序排数

    386. 字典序排数 给定一个整数 n, 返回从 1 到 n 的字典顺序. 例如, 给定 n =1 3,返回 [1,10,11,12,13,2,3,4,5,6,7,8,9] . 请尽可能的优化算法的时 ...

  3. LeetCode 386——字典序的第 K 小数字

    1. 题目 2. 解答 字典序排数可以看做是第一层节点分别为 1-9 的十叉树,然后我们在树上找到第 K 小的数字即可.因此,我们需要分别统计以 1-9 为根节点的每个树的节点个数.如果 K 小于当前 ...

  4. 求一个数组的最大k个数(java)

    问题描写叙述:求一个数组的最大k个数.如,{1,5,8,9,11,2,3}的最大三个数应该是,8,9,11 问题分析: 1.解法一:最直观的做法是将数组从大到小排序,然后选出当中最大的K个数.可是这种 ...

  5. 字典序的第K小数字

    今天zyb参加一场面试,面试官听说zyb是ACMer之后立马抛出了一道算法题给zyb:有一个序列,是1到n的一种排列,排列的顺序是字典序小的在前,那么第k个数字是什么?例如n=15,k=7, 排列顺序 ...

  6. php实现求链表中倒数第k个节点

    php实现求链表中倒数第k个节点 一.总结 $head = $head->next; //1.将$head节点next域里面的记录的那个地址值($head节点的下一个节点的地址)给$head,$ ...

  7. POJ 1146 ID Codes 用字典序思想生成下一个排列组合

    ID Codes Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7644   Accepted: 4509 Descript ...

  8. 三道习题(1、将单词表中由相同字母组成的单词归成一类,每类单词按照单词的首字母排序,并按 #每类中第一个单词字典序由大到小排列输出各个类别。 #输入格式:按字典序由小到大输入若干个单词,每个单词占一行,以end结束输入。)

    #coding=gbk ''' 1.将单词表中由相同字母组成的单词归成一类,每类单词按照单词的首字母排序,并按 #每类中第一个单词字典序由大到小排列输出各个类别. #输入格式:按字典序由小到大输入若干 ...

  9. Java实现 LeetCode 440 字典序的第K小数字

    440. 字典序的第K小数字 给定整数 n 和 k,找到 1 到 n 中字典序第 k 小的数字. 注意:1 ≤ k ≤ n ≤ 109. 示例 : 输入: n: 13 k: 2 输出: 10 解释: ...

随机推荐

  1. Myeclipse的show in breadcrumb

    m如何取消eclipse的show in breadcrumb 不小心点了show in breadcrumb,在编辑器界面上面多一层路径条,多余碍事,不晓得怎么取消,搞了半天终于弄好,方法如下: 点 ...

  2. tmux/screen里面如何用鼠标滚轮来卷动窗口内容

    tmux里面用鼠标滚轮来卷动窗口内容 在 tmux里面,因为每个窗口(tmux window)的历史内容已经被tmux接管了,所以原来console/terminal提供的Shift+PgUp/PgD ...

  3. UVA5135 Mining Your Own Business ( 无向图双连通分量)

    题目链接 题意:n条隧道由一些点连接而成,其中每条隧道链接两个连接点.任意两个连接点之间最多只有一条隧道.任务就是在这些连接点中,安装尽量少的太平井和逃生装置,使得不管哪个连接点倒塌,工人都能从其他太 ...

  4. HD1556Color the ball(树状数组)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  5. web项目中的跨域问题解决方法

    一种是JSONP 一种是 CORS. 在客户端Javascript调用服务端接口的时候,如果需要支持跨域的话,需要服务端支持. JSONP的方式就是服务端对返回的值进行回调函数包装,他的优点是支持众多 ...

  6. wpf 窗体内容旋转效果 网摘

    <Window x:Class="simplewpf.chuangtixuanzzhuan"        xmlns="http://schemas.micros ...

  7. 【原创】一段简短的读取libglade的UI文件的Python代码

    准备写一个将Glade/GtkBuilder等格式的UI文件转换成C++代码的python程序 首先完成的是将LIBGlade格式读取至内存中 #!/usr/bin/env python # -*- ...

  8. UIView UIwindow

    UI:用户界面,用户能看到的各种各样的页面元素 UIview :代表屏幕上的一个矩形区域,管理界面上的内容       创建UIview 1.开辟空间并初始化视图(初始化时,给出视图位置和大小 2.对 ...

  9. Linux下开发常用 模拟 Http get和post请求

    1.get请求 curl "http://www.baidu.com"      如果这里的URL指向的是一个文件或者一幅图都可以直接下载到本地 curl -i "htt ...

  10. PHP----遇到的Session问题

    使用SESSION,当跨页面使用时,会提示错误Cannot modify header information - headers already sent by..., 背景:使用session_s ...