CSU

来源:互联网 发布:人民子弟兵 知乎 编辑:程序博客网 时间:2024/06/14 05:26

1563: Lexicography

        Time Limit:1 Sec     Memory Limit: 128 Mb     Submitted:468     Solved:150    

Description

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 5ICPC 12REGION 274# 0

Sample Output

MACPICCIGNORE

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.

题意:求一个字符串的全排列按字典序从小到大排之后的第K个

就是排列组合的一些问题。N个不同元素的全排列为N!,若N个元素中有M中每种有ai个则其全排列为N! / a1! / a2! /... / am-1! / am!

可从前往后依次放应该放在该位置的字符,如ICPC,得到的是CCIP,第一个位置可以放'C'、'I'、'P'三种字符,第一个位置为'C'时有6种组合,'I'和'P'各有3种组合,若K在1-6之间选C,7-9之间选I,10-12之间选P,然后K各减去0、6、9,然后再按此操作往下一个位置找,直到K=0。

#include<iostream>#include<stdio.h>#include<string.h>#include<algorithm>#define ll long longusing namespace std;char s[20];ll k,len,fact[20];ll A(char *str)//求字符串str的全排列{    ll l = strlen(str);    sort(str,str+l);    ll ans = fact[l];    ll cnt = 1;    for(int i=1;i<l;i++)    {        ans /= cnt;        if(str[i]==str[i-1])            cnt++;        else            cnt = 1;    }    ans /= cnt;    return ans ;}bool cmp(char a,char b){    return a>b;}void dfs(int x,ll k){    if(k==0) return;    ll cost = A(s+x+1);    if(cost==k)    {        sort(s+x+1,s+len,cmp);        return;    }    else    if(cost>k)        dfs(x+1,k);    else    {        int co = 1;        while(cost<k)        {            int cnt = 0;            for(int i=x;i<len;i++)            {                if(s[i]!=s[i-1]&&s[i]!=s[x])                    cnt++;                if(cnt==co)                {                    co++;                    swap(s[x],s[i]);                    sort(s+x+1,s+len);                    k-=cost;                    break;                }            }            cost = A(s+x+1);        }        dfs(x,k);    }}int main(){    fact[0] = fact[1] = 1;    for(int i=2;i<=16;i++)        fact[i] = fact[i-1] * i;    while(~scanf("%s%lld",s,&k))    {        len = strlen(s);        if(s[0]=='#'&&len==1&&k==0)            break;        sort(s,s+len);        dfs(0,k);        printf("%s\n",s);    }    return 0;}


原创粉丝点击