CSU 1563: Lexicography (mathematical counting problem), csulexicography

Source: Internet
Author: User

CSU 1563: Lexicography (mathematical counting problem), csulexicography


1563: LexicographyTime Limit: 1 Sec Memory Limit: 128 MB
Submit: 342 Solved: 111
[Submit] [Status] [Web Board] Description

An ansible of a string is any string that can be formed using the same letters as the original. (We consider the original string an anal 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 anw.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 anal of the original string.

Sample Input
ACM 5ICPC 12REGION 274# 0
Sample Output
MACPICCIGNORE
HINT

The value of K cocould be almost 245 in the largest tests, so you shoshould use type long in Java, or type long in C ++ to store K.



// Sorting and combination counting problem. The n count column is n !, If x has t, all columns are listed as n! /(T !) // Then enumerate the case where the current BIT is I, and then always subtract it from the case where I is, If <I, // then this bit is I # include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <cmath> # include <queue> # include <stack> # include <vector> # include <set> # include <map> # define L (x) (x <1) # define R (x) (x <1 | 1) # define MID (x, y) (x + y)> 1) # define eps 1e-8typedef long ll; # define fre (I, a, B) for (I = a; I <B; I ++) # define free (I, B, a) for (I = B; I> = a; I --) # define mem (t, v) memset (t), v, sizeof (t )) # define ssf (n) scanf ("% s", n) # define sf (n) scanf ("% d", & n) # define sff (a, B) scanf ("% d", & a, & B) # define sfff (a, B, c) scanf ("% d", &, & B, & c) # define pf printf # define bug pf ("Hi \ n") using namespace std; # define INF 0x3f3f3f3f # define N 17ll dp [N]; ll k; int len; int a [30]; char c [N]; int ans [N]; void inint () {int I, j; dp [0] = 1; dp [1] = 1; fre (I, 2, N) dp [I] = dp [I-1] * I ;} void dfs (int pos, ll k) {if (pos = len) return; // printf ("% lld \ n", k); int I, j; int le = len-pos; fre (I,) if (a [I]> 0) {ll ss = dp [le-1]; fre (j) if (a [j]) {if (j = I) {ss/= dp [a [I]-1];} else ss/= dp [a [j];} if (ss> = k) // This is the case when I have ss {ans [pos] = I; a [I] --; dfs (pos + 1, k); return;} else k-= ss;} int main () {int I, j; inint (); while (scanf ("% s % lld", c, & k) {if (c [0] = '#' & k = 0) break; mem (a, 0); len = strlen (c); fre (I, 0, len) a [c [I]-'a'] ++; sort (c, c + len); dfs (0, k); fre (I, 0, len) pf ("% c", ans [I] + 'A '); puts ("");} return 0 ;}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.