程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> UVALive6814 Lexicography

UVALive6814 Lexicography

編輯:關於C++

 

An anagram of a string is any string that can be formedusing the same letters as the original. (We consider the original string ananagram of itself as well.) For example, the string ACM has thefollowing 6 anagrams, as given in alphabetical order:

 
ACM
AMC
CAM
CMA
MAC
MCA

As another example, the string ICPC has the following 12anagrams (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 determinethe Kth such anagram according to alphabetical order.

Input: Each test case willbe designated on a single line containing the original word followed by thedesired rank K. Words will use uppercase letters (i.e., A through Z) and willhave length at most 16. The value of K will be in the range from 1 tothe number of distinct anagrams of the given word. A line of the form "# 0"designates the end of the input.

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

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

Example Input:

Example Output:

ACM 5
ICPC 12
REGION 274
# 0

MAC
PICC
IGNORE

 

 

 

#include 
#include 
#include 
#include 
#include 
#include 
#define ll long long
#define N 50

using namespace std;

char str[N];
ll k;
int num[N];
ll f[N];

ll fun(int x)
{
    ll ans=f[x];
    for(int i=0;i<26;i++)
    ans/=f[num[i]];//除以相同個數的階乘

    return ans;
}

void solve(int len)
{

    for(int i=0;i=k)
                {
                    num[j]--;
                    printf("%c",'A'+j);
                    break;
                }
                else
                k-=a;
            }
        }
    }
    cout<

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved