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

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;
}
时间: 2024-11-05 16:30:53

CSU 1563: Lexicography (数学计数问题)的相关文章

Lexicography(数学推论&gt;&gt;求按字典序排第k个排列)

Lexicography Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format:%lld & %llu Submit Status Practice CSU 1563 Description An anagram of a string is any string that can be formed using the same letters as the original. (We consider the orig

CSU1563:Lexicography(数学)

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 alphabet

数学建模论坛

数学建模论坛: http://bbs.agoil.cn/thread-htm-fid-402.html http://bbs.pinggu.org/biaoqian/shuxuejianmo/ http://tieba.baidu.com/f?kw=%E6%95%B0%E5%AD%A6%E5%BB%BA%E6%A8%A1&ie=utf-8 http://www.madio.net/forum.php http://www.matlabsky.com/topic-contest-mcm.html

CSU1563 组合数学

C - Lexicography Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format:%lld & %llu Submit Status Practice CSU 1563 Description An anagram of a string is any string that can be formed using the same letters as the original. (We consider the

CSU 1290 DP解决数学期望问题

题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1290 题目大意: 给定k个数,每次可以生成0-N-1中的任何一个数,k个数中出现不同的整数的个数的数学期望 1 #include <cstdio> 2 #include <cstring> 3 using namespace std; 4 #define N 1005 5 double dp[N]; 6 int main() 7 { 8 int T,k,n; 9 scan

【数学】CSU 1810 Reverse (2016湖南省第十二届大学生计算机程序设计竞赛)

题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1810 题目大意: 一个长度为N的十进制数,R(i,j)表示将第i位到第j位翻转过来后的数字,求mod 109+7 题目思路: [数学] 这题换一种思路,看每个数字能够对答案的贡献情况.(可以手推01,10,001,010,100.....,也可以像我一样写个暴力打个10以内的表看看规律) 现在先考虑位置为i的数字为1的情况(最后乘上这个数字就行).可以发现贡献是对称的(第i位的1和第

【模拟】【数学】CSU 1803 2016 (2016湖南省第十二届大学生计算机程序设计竞赛)

题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1803 题目大意: 给定n,m(n,m<=109)1<=i<=n,1<=j<=m,求i*j%2016=0的方案数. 题目思路: [模拟][数学] 按照%2016的余数分类.每增加一个2016就又多一种方案.统计是2016的几倍,根据余数分类.最后枚举i,j的余数即可求解. 1 // 2 //by coolxxx 3 //#include<bits/stdc++

CSU 1505: 酷酷的单词(数学啊)

题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1505 Description 输入一些仅由小写字母组成的单词.你的任务是统计有多少个单词是"酷"的,即每种字母出现的次数都不同. 比如ada是酷的,因为a出现2次,d出现1次,而1和2不同.再比如,banana也是酷的,因为a出现3次,n出现2次,b出现1次.但是,bbacccd不是酷的,因为a和d出现的次数相同(均为1次). Input 输入包含不超过30组数据.每组数据第

CSU 1507: 超大型LED显示屏(数学啊)

题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1507 Description Input 输入包含不超过100组数据.每组数据第一行为"START hh:mm:ss",表示比赛开始时刻为hh:mm:ss.最后一行为"END hh:mm:ss",即比赛结束时刻.二者之间至少会有一个SCORE信息,格式为"SCORE hh:mm:ss team score",其中team要么是"