Lexicography (uvalive 6814)

Source: Internet
Author: User

Lexicography
Time Limit: 1000MS Memory Limit: 131072KB 64bit IO Format: %lld &%llu

Submit Status

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:

Acm
Amc
CAM
Cma
Mac
Mca
As another example, the string ICPC has the and the following anagrams (in alphabetical order):

CCIP
Ccpi
Cicp
CIPC
CPCI
CPIC
ICCP
ICPC
IPCC
PCCI
Pcic
Picc
Given a string and a rank K, you is to determine the Kth such anagram according to alphabetical order.

Input

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.

Output

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

Sample Input

ACM 5ICPC 12REGION 274# 0

Sample Output

Macpiccignore

Hint

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.



Test instructions: Given a string and K, the sequence of the string dictionary sequence K is obtained.

Code

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < cmath> #include <string> #include <map> #include <stack> #include <vector> #include <set > #include <queue> #pragma comment (linker, "/stack:102400000,102400000") #define MAXN 1005#define MAXN 2005# Define mod 1000000009#define INF 0x3f3f3f3f#define pi ACOs ( -1.0) #define EPS 1e-6#define Lson rt<<1,l,mid#define RSO  N rt<<1|1,mid+1,r#define FRE (i,a,b) for (i = A, I <= b; i++) #define FREE (i,a,b) for (i = A; I >= b; i--) #define FRL (i,a,b) for (i = A; I < b; i++) #define FRLL (i,a,b) for (i = A; i > b; i--) #define MEM (T, v) memset ((t), V, si Zeof (t)) #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 DBG pf ("hi\n") typedef long Long ll;using Nam Espace std;ll Fac[20],k;char str[30];int num[30];ll gEtnum (int l) {ll s=fac[l];    for (int i=0;i<26;i++) s/=fac[num[i]]; return s;}            void solve (int len) {for (int. i=0;i<len;i++) {for (int j=0;j<26;j++) {if (Num[j])                {num[j]--;                ll X=getnum (len-i-1);                num[j]++;                    if (x>=k) {num[j]--;                    printf ("%c", j+ ' A ');                Break            } else k-=x; }}} printf ("\ n");}    int main () {//Freopen ("C:/users/asus1/desktop/in.txt", "R", stdin);    int i,j;    Fac[0]=1;    for (i=1;i<=17;i++) fac[i]=fac[i-1]*i;        while (scanf ("%s%lld", str,&k)} {if (k==0) break;        int Len=strlen (str);        MEM (num,0);        for (i=0;i<len;i++) num[str[i]-' A ']++;    Solve (len); } return 0;}



Lexicography (uvalive 6814)

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.