CSU 1563:lexicography (mathematical counting problem)

Source: Internet
Author: User

1563:lexicography time limit: 1 Sec Memory Limit: MB
Submit: 342 Solved: 111
[Submit] [Status] [Web Board] Description

An anagram of a string was 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 have the following 6 anagrams, as given in alphabetical order:

As another example, the string ICPC has the and the following anagrams (in alphabetical order):

Given a string and a rank K, you is to determine the Kth such anagram according to alphabetical order.


Each test case is designated on a single line containing the original word followed by the desired rank K. Words would Use uppercase letters (i.e., A through Z) and would has length at the most 16. The value of K would 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.


For each test, display the Kth anagram of the original string.

Sample Input
Sample Output

The value of K could is almost 245 in the largest tests, so you should use type Long in Java, or type long long in C + + to Store K.

Permutation combination count problem, n number is all arranged as n!, if X has T, then the whole arrangement is n!/(t!) Then enumerate the current bit is the case of I, and then always minus the case of I, if the case of <i,//Then this one 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 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%d ", &a, &b) #define SFFF (a,b,c) scanf ("%d%d%d ", &a, &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,0,27) if (a[i]>0) {ll ss=dp[le-1]; Fre (j,0,27) if (A[j]) {if (j==i) {ss/=dp[a[i]-1];} else ss/=dp[a[  J]];  } if (ss>=k)//This bit is I when there is a SS case {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);d FS (0,k), Fre (I,0,len) pf ("%c", ans[i]+ ' A ');  Puts ("");} return 0;}

CSU 1563:lexicography (mathematical counting problem)

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.