码迷,mamicode.com
首页 > 其他好文 > 详细

CSU 1563: Lexicography (数学计数问题)

时间:2015-05-04 22:18:39      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:数学


1563: Lexicography

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

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

Sample Output

MAC
PICC
IGNORE

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.



//排列组合计数问题,n个数全排列为n!,如果x有t个,那么全排列为n!/(t!)
//然后枚举当前位是i的情况,然后一直减去为i的时候的情况,如果<i的情况,
//那么这一位就是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-8
typedef 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 17

ll 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)   //这一位是i时有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;
}





CSU 1563: Lexicography (数学计数问题)

标签:数学

原文地址:http://blog.csdn.net/u014737310/article/details/45484981

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!