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 ...
随机推荐
- 小程序之根据参数更改title
是这样的,今天呢在写中英文切换功能,哇 从psd图里面去复制英文在去对应,真的是太难受了 okok 切回正题 当用户选择英文的时候 我的title也要是英文怎么办呢 wx.setNaviga ...
- python web.py操作mysql数据库,实现对数据库的增删改查操作
使用web.py框架,实现对mysql数据库的增删改查操作: 该示例代码中连接的是本地数据库testdb,user表,表结构比较简单,只有两个字段:mobile和passwd,类型均为字符型 实际应用 ...
- 能改变this各种情况下的总结,还有没有总结到的,请留言!!
1.. 在函数参数中的,回调函数的this ,指向window 如: promise中的回调函数, 可以在方法外,转存this 2..构造函数中,this指向,实例对象 , 在全局中this是win ...
- BFC(块级格式化上下文)
转载自:https://www.cnblogs.com/asheng2016/p/7281784.html https://blog.csdn.net/jiaojsun/article/details ...
- AD绘制PCB时,贴片封装器件的焊盘间距小于10Mil,报错解决
Design->Rules->Manufacturing->Minimum Solder Mask Sliver
- SpringBoot 文件上传、下载、设置大小
本文使用SpringBoot的版本为2.0.3.RELEASE 1.上传单个文件 ①html对应的提交表单 <form action="uploadFile" method= ...
- (转)C# 之泛型详解
什么是泛型 我们在编写程序时,经常遇到两个模块的功能非常相似,只是一个是处理int数据,另一个是处理string数据,或者其他自定义的数据类型,但我们没有办法,只能分别写多个方法处理每个数据类型,因为 ...
- JavaScript 第十章总结:first class functions
前言 这一章的内容是 advanced knowledge and use of functions. 讲了关于 function 的使用的一些特殊的方面. function expression 的 ...
- 2017-2018-2 20165327 实验二 《Java面向对象程序设计》实验报告
20165327<Java程序设计>实验二 <Java面向对象程序设计>实验报告 实验二 <Java面向对象程序设计> 一.实验报告封面 课程:Java程序设计 班 ...
- asp.net网站服务器搭建之从零开始
asp.net网站服务器搭建之从零开始 一 IIS(Internet Information Services)安装: 1.选择"控制面板". 2.点"添加或删除程序 ...