Lexicography(数学推论>>求按字典序排第k个排列)
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个排列)的更多相关文章
- LeetCode 386——字典序排数
1. 题目 2. 解答 2.1 方法一 假设返回 118 以内数的字典顺序,则为 1,10,100,101,102,...,109,11,110,111,112,...,118,12,13,....根 ...
- Java实现 LeetCode 386 字典序排数
386. 字典序排数 给定一个整数 n, 返回从 1 到 n 的字典顺序. 例如, 给定 n =1 3,返回 [1,10,11,12,13,2,3,4,5,6,7,8,9] . 请尽可能的优化算法的时 ...
- LeetCode 386——字典序的第 K 小数字
1. 题目 2. 解答 字典序排数可以看做是第一层节点分别为 1-9 的十叉树,然后我们在树上找到第 K 小的数字即可.因此,我们需要分别统计以 1-9 为根节点的每个树的节点个数.如果 K 小于当前 ...
- 求一个数组的最大k个数(java)
问题描写叙述:求一个数组的最大k个数.如,{1,5,8,9,11,2,3}的最大三个数应该是,8,9,11 问题分析: 1.解法一:最直观的做法是将数组从大到小排序,然后选出当中最大的K个数.可是这种 ...
- 字典序的第K小数字
今天zyb参加一场面试,面试官听说zyb是ACMer之后立马抛出了一道算法题给zyb:有一个序列,是1到n的一种排列,排列的顺序是字典序小的在前,那么第k个数字是什么?例如n=15,k=7, 排列顺序 ...
- php实现求链表中倒数第k个节点
php实现求链表中倒数第k个节点 一.总结 $head = $head->next; //1.将$head节点next域里面的记录的那个地址值($head节点的下一个节点的地址)给$head,$ ...
- POJ 1146 ID Codes 用字典序思想生成下一个排列组合
ID Codes Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7644 Accepted: 4509 Descript ...
- 三道习题(1、将单词表中由相同字母组成的单词归成一类,每类单词按照单词的首字母排序,并按 #每类中第一个单词字典序由大到小排列输出各个类别。 #输入格式:按字典序由小到大输入若干个单词,每个单词占一行,以end结束输入。)
#coding=gbk ''' 1.将单词表中由相同字母组成的单词归成一类,每类单词按照单词的首字母排序,并按 #每类中第一个单词字典序由大到小排列输出各个类别. #输入格式:按字典序由小到大输入若干 ...
- Java实现 LeetCode 440 字典序的第K小数字
440. 字典序的第K小数字 给定整数 n 和 k,找到 1 到 n 中字典序第 k 小的数字. 注意:1 ≤ k ≤ n ≤ 109. 示例 : 输入: n: 13 k: 2 输出: 10 解释: ...
随机推荐
- 关于git不区分文件名大小写的处理
今天遇到了git不区分文件名大小写的问题,一开始着实郁闷了一把. 处理办法: windows下在git中修改文件的大小写 git mv --force myfile MyFile 或者 git mv ...
- Appium for IOS testing on Mac
一:环境 1.Mac OS X 10.9.1 2.Xcod 5.0.2 3.Appium 1.3.6 下载地址:https://bitbucket.org/appium/appium.app/down ...
- python标准模块(一)
本文会涉及到的模块: time datetime sys os random re hashlib 模块,用若干代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能 ...
- http+mysql结合keepalived做热备
preface 公司要求http+mysql+redis+二次开发的ldap要求做高可用,所以此处写写keepalived在这种 环境下的高可用.keepalived这个软件我就不啰嗦了,众所周知,基 ...
- Base64复习
http://www.cnblogs.com/chengxiaohui/articles/3951129.html
- Django 部署 uwsgi + nginx + supervisor
Django 部署 uwsgi + nginx + supervisor https://hacpai.com/article/1460607620615?p=1&m=0 zonghua • ...
- hibernate----(Hql)查询
package com.etc.test; import java.util.List;import java.util.Properties; import org.hibernate.Query; ...
- JS-流程控制汇集
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>流 ...
- OC description
description方法的作用是打印对象,对于一个类,如果没有重写description方法,NSLog(@“%@”,此处写类的对象), 输出的是该类的地址如下: -- :::] <Class ...
- split shell tools
split [-bl] file [prefix] 参数说明: -b, --bytes=SIZE:对file进行切分,每个小文件大小为SIZE.可以指定单位b,k,m. -l, --lines=NUM ...