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. iOS评分(给个好评)

    id 721996244 可以测试 NSString *str = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/app ...

  2. SVN Access to ‘/svn/Test/!svn/me’ forbidden,不能更新解决办法

    今天上班,使用公司配置的电脑进行项目的更新.SVN报如下错误, SVN Access to '/svn/Test/!svn/me' forbidden,不能更新解决办法 很有意思: 开始以为自己的SV ...

  3. asp.net中的窗口弹出实现,包括分支窗口 . ASP.NET返回上一页面实现方法总结 .

    返回上一页的这个东东在我们做项目的时候一般是用于填写完表单后确认的时候,有对原来输入的数据进行修改或者更新时用的,或者是因为网站为了方便浏览者而有心添加的一个东东,一般这种功能的实现在ASP.NET中 ...

  4. 最近在 OS-10.9下配置opencv, cgal, latex, qt, pillow

    其实我之前使用的Mac os的版本是10.8的雪豹,可是最近想体验一下Mac os10.9新版本,于是就开始更新Mac os,经过10多个小时的下载和成功安装后,发现之前的配置全乱了,首先是发现lat ...

  5. log4net 记录到数据库和本地文件

    1)配置代码 <?xml version="1.0" encoding="utf-8" ?> <configuration> <c ...

  6. vim的使用

    vim的定位 home:光标移到行首 end:光标移到行尾 pageup:屏幕上翻页 pagedow:屏幕下翻页 shift+g:定位行  如转至10行   10shift+g shift+gg 转至 ...

  7. 词性标注 parts of speech tagging

    In corpus linguistics, part-of-speech tagging (POS tagging or POST), also called grammatical tagging ...

  8. Cloudservie将LocalStroage中的内容通过WAD自动上传到BLOB中

    开发云服务程序,如果使用Local stroage存储我们临时生成的日志或者文件并将它们自动上传到BLOB中,可以通过WAD来实现,具体如下: 1.配置webrole,开启Local stroage功 ...

  9. UITableViewCell的separator分隔线设置失效

    // 处理separator -(void)viewDidLayoutSubviews { if ([self.tableView respondsToSelector:@selector(setSe ...

  10. SVN合并操作实践

    大家都知道,SVN是很多公司管理代码的版本控制工具,当分支越来越多,版本迭代越来越频繁的时候,经常会出现代码冲突的头疼事儿,这里讲一下鲨鱼遇到过关于代码版本控制的一些事,最后做个小例子,看图描述. 为 ...