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 解释: ...
随机推荐
- python面相对象进阶
1. 类的成员 python 类的成员有三种:字段.方法.属性 字段 字段包括:普通字段和静态字段,他们在定义和使用中有所区别,而最本质的区别是内存中保存的位置不同, 普通字段 属于对象,只有对象创建 ...
- Code笔记 之:注册页面验证码
文章内容包括: 1.验证码制作 -- 中文|字母|数字|…… 2.图文验证码 -- 图片防盗链(PHP而非JS) 3.JS防止右键点击图片 4.input表单输入框不记录输入过的信息 5.CSS+di ...
- Autofac的高级使用——Autofac.2.6.3.862
Autofac的高级使用——Autofac.2.6.3.862 目录(?)[-] 使用代码方式进行组件注册依赖服务类和组件类 使用配置文件进行组件注册不需要依赖 定义配置文件 读取config配置文件 ...
- Mysql初始化root密码和允许远程访问
mysql默认root用户没有密码,输入mysql –u root 进入mysql 1.初始化root密码 进入mysql数据库 1 mysql>update user set password ...
- linux中配置NTP基本操作
计算机的系统时间是由计算机内的石英晶体震荡电路以固定的震荡频率产生的date 查看当前时区vim /etc/sysconfig/clock 修改时区配置文件ZONE="America/Ne ...
- java学习总结
1 获得项目绝对路径 String path = request.getContextPath(); String basePath = request.getScheme() + ":// ...
- Java 开发技巧
一 读取配置文件 1 Properties读取配置文件 编写配置文件config.properties放在普通java工程的src目录(如果是maven工程就放在工程的src/main/resourc ...
- awk多模式匹配
awk -F ':' '{if(($1 ~/wlan/)||( $1 ~/Cell/)||($1 ~/Quality*/)) {print $0}}'
- MySQL数据库常用函数
一.数学函数 数学函数主要用于处理数字,包括整型.浮点数等. ABS(x) 返回x的绝对值 不区分大小写 SELECT ABS(-1) -- 返回1 CEIL(x),CEILING(x) 返回大于或等 ...
- yourtour的几种链接
php,html {:URL('User-Register/index')} 格式:http://www.xxx.com/index.php?g=User&m=User&a=in ...