Lexicography
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.
假设字符共有n类,个数分别为m1,m2……,mn,
那么这个多重集的全排列个数为(m1+m2+……+mn)!/m1!/m2!/……/mn!
然后呢,我们来举个例子,要知道字符集"ICPC"的第12个字符串是多少,
对第一位做出假设
C的话,那么有3!=6种串,
I的话,有3!/2!=3种串,
P的话,有3!/2!=3种串,
可知6+3+3=12,那么第一位就是P,从剩下的字符集里找出第12-6-3=3个字符串
现在字符集变成"ICC",
对第二位做出假设
C的话,那么有2!=2种串,
I的话,有2!/2!=1种串,
可知2+1=3,所以,第二位就是I,从剩下的字符集里找出第3-2=1个字符串
现在字符集变成"CC",
对第三位做出架设
C的话,那么有1!=1种串,
可知1=1,于是,第三位就是C,从剩下的字符集里找出第1-1=0个字符串,
现在字符集变成”C",
这个串并不存在,那么就是直接把剩下的那些字符,就是C补在最后就行了,
很显然,此时剩下的字符集一定都是由某个字符重复组成。
#include <iostream>
#include <algorithm>
#include <string.h>
#include<stdio.h>
using namespace std;
int main()
{
char a[];
long long int n;
long long int f[];
f[]=;
for(int i=;i<;i++)
f[i]=f[i-]*i; while(cin>>a>>n)
{ if(a[]=='#'&&n==)
break;
int t=strlen(a);
int zimu[];
memset(zimu,,sizeof zimu);
for(int i=;i<t;i++)
{
zimu[a[i]-'A']++; }
for(int i=;i<t;i++)
{
long long int tot=;
for(int j=;j<;j++)
{
if(zimu[j])
{
long long int ppp= f[t-i-];
for(int k=;k<;k++)
{
if(k==j)
ppp=ppp/f[zimu[k]-];
else
ppp=ppp/f[zimu[k]];
}
if(tot+ppp>=n)
{
a[i]=j+'A';
n=n-tot;
zimu[j]--;
break;
}
else
tot=ppp+tot;
} } }
cout<<a<<endl; }
return ;
}
Lexicography的更多相关文章
- Lexicography(数学推论>>求按字典序排第k个排列)
Lexicography Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%lld & %llu Submit ...
- 组合数学(全排列)+DFS CSU 1563 Lexicography
题目传送门 /* 题意:求第K个全排列 组合数学:首先,使用next_permutation 函数会超时,思路应该转变, 摘抄网上的解法如下: 假设第一位是a,不论a是什么数,axxxxxxxx一共有 ...
- csu 1563 Lexicography
题意:给出一堆字母 问这些字母组成的字符串中第k大的 排列组合,具体看代码 //寒假集训被何柱大大踩好惨(>_<) #include<cstdio> #include<i ...
- Basic Tutorials of Redis(5) - Sorted Set
The last post is mainly about the unsorted set,in this post I will show you the sorted set playing a ...
- hihoCoder 1385 : A Simple Job(简单工作)
hihoCoder #1385 : A Simple Job(简单工作) 时间限制:1000ms 单点时限:1000ms 内存限制:256MB Description - 题目描述 Institute ...
- ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛 A Simple Job
描述 Institute of Computational Linguistics (ICL), Peking University is an interdisciplinary institute ...
- hihoCoder 1385 A Simple Job
#1385 : A Simple Job 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 Institute of Computational Linguistics (I ...
- UESTC_Ferris Wheel String 2015 UESTC Training for Search Algorithm & String<Problem L>
L - Ferris Wheel String Time Limit: 3000/1000MS (Java/Others) Memory Limit: 43000/43000KB (Java/ ...
- zoj3204 connect them 最小生成树 暴力
Connect them Time Limit: 1 Second Memory Limit:32768 KB You have n computers numbered from 1 to ...
随机推荐
- 搭建 FTP 文件服务
1.安装并启动 FTP 服务 2.配置 FTP 权限 3.准备域名和证书 4.访问 FTP 安装 VSFTPD 使用 yum 安装 vsftpd: yum install vsftpd -y vsft ...
- Spring-JDBC依赖
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</a ...
- HttpDns原理
什么是 DNS DNS(Domain Name System,域名系统),DNS 服务用于在网络请求时,将域名转为 IP 地址.能够使用户更方便的访问互联网,而不用去记住能够被机器直接读取的 IP 数 ...
- TCGA收官之作—27篇重磅文献绘制“泛癌图谱”
TCGA的关键数字:图片来源<细胞> 由美国政府发起的癌症和肿瘤基因图谱(Cancer Genome Atlas,TCGA)计划于2006年联合启动,目前已经收录了来自1万多例病人的33种 ...
- CSS垂直居中查询宝典
一.垂直居中的用处 设计稿需求 当我们抱怨设计反复不定的时候,试着理解一下.每一位开发者也会是一位用户,请多多用'用户'的角色去开发.就比如下面这图,你会更稀饭哪种格式呢? 如果我们使用一个webap ...
- js_提示是否删除小案例
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- MySQL的正则表达式的LIKE和REGEXP区别
LIKE匹配整个列.如果被匹配的文本在列值 中出现,LIKE将不会找到它,相应的行也不被返回(除非使用 通配符).而REGEXP在列值内进行匹配,如果被匹配的文本在 列值中出现,REGEXP将会找到它 ...
- 定时任务redis锁+自定义lambda优化提取冗余代码
功能介绍: 我系统中需要跑三个定时任务,由于是多节点部署,为了防止多个节点的定时任务重复执行.所以在定时任务执行时加个锁,抢到锁的节点才能执行定时任务,没有抢到锁的节点就不执行.从而避免了定时任务重复 ...
- AtCoder Regular Contest 102 D - All Your Paths are Different Lengths
D - All Your Paths are Different Lengths 思路: 二进制构造 首先找到最大的t,使得2^t <= l 然后我们就能构造一种方法使得正好存在 0 到 2^t ...
- Codeforces 551 E - GukiZ and GukiZiana
E - GukiZ and GukiZiana 思路:分块, 块内二分 代码: #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC ...