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. easyui使用datagrid时列名包含特殊字符导致表头与数据错位的问题

    做一个用easyui的datagrid显示数据的功能时发现表格的列头与数据错位了,而且这个现象不总是能重现,一直没搞清楚原因.后来偶然在控制台看出了一点端倪: 推测表头或者单元格的class名应该是用 ...

  2. HDU 1231 最大连续子序列(水题)

    题目链接: 传送门 最大连续子序列 Time Limit: 1000MS     Memory Limit: 32768 K Description 给定K个整数的序列{ N1, N2, ..., N ...

  3. 从Paxos到ZooKeeper-二、ZooKeeper和Paxos

    ZooKeeper为分布式应用提供了高效且可靠的分布式协调服务,提供了诸如tong'yi统一命名服务.配置管理和分布式锁等分布式的基础服务.在解决分布式数据一致性方面,ZooKeeper并没有直接采用 ...

  4. 从Paxos到ZooKeeper-一、分布式架构

    本系列为本人读<从Paxos到ZooKeeper>一书的一些读书笔记,仅供学习使用,谢谢. 一.从集中式到分布式 1.1 分布式的定义: 分布式系统是一个硬件或软件组件分布在不同的网络计算 ...

  5. HTTPS背后的加密算法

    当你在浏览器的地址栏上输入https开头的网址后,浏览器和服务器之间会在接下来的几百毫秒内进行大量的通信.InfoQ的这篇文章对此有非常详细的描述.这些复杂的步骤的第一步,就是浏览器与服务器之间协商一 ...

  6. DataReader和DataSet的异同

    DataReader:使用时始终占用SqlConnection,在线操作数据库:每次只在内存中加载一条数据,所以占用的内存是很小的:是只进的. 只读的. DataSet:则是将数据一次性加载在内存中. ...

  7. mysql 字符串

    mysql中一个字符串,既可以用两个单引号表示,也可以用两个双引号表示. 比如字符串 wangxiaowei,用单引号表示 'wangxiaowei',双引号表示"wangxiaowei&q ...

  8. Visual Studio的调试技巧

    Visual Studio的调试技巧 [原文地址] Debugging Tips with Visual Studio 2010 [原文发表日期] 2010/8/19 10:48 AM 这是我写的关于 ...

  9. os模块之popen

    想查看当前目录下有哪些东西,可以使用os.popen()方法,代码如下: t = (os.popen("dir")) print(t.read()) #运行结果 C:\python ...

  10. winform的扩展的带有截图功能picturebox

    using System; using System.ComponentModel; using System.Diagnostics; using System.Drawing; using Sys ...