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 ...
随机推荐
- javaScript 内置对象-Array数组
Array 对象方法 方法 描述 concat() 连接两个或更多的数组,并返回结果. join() 把数组的所有元素放入一个字符串.元素通过指定的分隔符进行分隔. pop() 删除并返回数组的最后一 ...
- _event_phase
EventId 事件ID Phase 阶段ID,从1开始 StopGUID 击杀生物或摧毁物体当前阶段结束,,正数为生物,负数为物体
- 《spring boot 实战》读书笔记
前言:虽然已经用spring boot开发过一套系统,但是之前都是拿来主义,没有系统的,全面的了解过这套框架.现在通过学习<spring boot实战>这本书,希望温故知新.顺便实现自己的 ...
- zzulioj 1206 字符串的修改 (字符串修改)
不难,理解一下直接过,代码如下: #include<stdio.h> #include<string.h> #include<math.h> #include< ...
- 《剑指offer》第五十五题(二叉树的深度)
// 面试题55(一):二叉树的深度 // 题目:输入一棵二叉树的根结点,求该树的深度.从根结点到叶结点依次经过的 // 结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度. //如果左右 ...
- VUE项目实现页面跳转
打开一个VUE项目,目录结构是这样的: 如现在有两个页面aaa和HelloWorld,路由配置在index.js中: import Vue from 'vue' import Router from ...
- Android Studio NDK开发-JNI调用Java方法
相对于NDK来说SDK里面有更多API可以调用,有时候我们在做NDK开发的时候,需要在JNI直接Java中的方法和变量,比如callback,系统信息等.... 如何在JNI中调用Java方法呢?就需 ...
- jfinal集成cas单点认证实践
本示例jfinal集成cas单点认证,采用获取到登录用户session信息后,在本地站点备份一份session信息,主要做以下几个步骤: 1.站点引入响应jar包: 2.在web.xml中配置对应过滤 ...
- SPOJ 刷题记录
按点赞数降序 297 二分 #include<bits/stdc++.h> using namespace std; #define fi first #define se second ...
- 原生JS操作iframe里的dom
转:http://www.css88.com/archives/2343 一.父级窗口操作iframe里的dom JS操作iframe里的dom可是使用contentWindow属性,contentW ...