csu 1563 Lexicography

1563: Lexicography

            Time Limit: 1 Sec       Memory Limit: 128 Mb       Submitted: 469       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 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.

Source


这道题就是问一个字符串按照全排列的顺序,输出第几个排列就可以了。首先对字符串按字典序排序,然后按照全排列的规则找规律。




#include<map>
#include<set>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
	char a[111];
	long long int  m;
    long long int jc[22];
	jc[0]=1;
	for(int i=1;i<=17;i++)
	jc[i]=i*jc[i-1];
	while(cin>>a>>m)
	{
		if(a[0]=='#'&&m==0)break;
		sort(a,a+strlen(a));
		int vis[27];
		char ans[111];  
		memset(vis,0,sizeof(vis));
		int i,j;
		for(i=0;i<strlen(a);i++)
		vis[a[i]-'A']++;
		for(i=0;i<strlen(a);i++)
		{
			long long  cnt=0;
			long long  sss=0;
			for(j=0;j<26;j++)
			{
				if(vis[j])
				{
				   cnt=jc[strlen(a)-1-i];
				   for(int k=0;k<26;k++)
				   {
				   	if(k==j)cnt/=jc[vis[k]-1];
				   	else cnt/=jc[vis[k]];
				   }
				   if(cnt+sss>=m)
				   {
				   	ans[i]=j+'A';
				   	vis[j]--;
				   	m-=sss;
				   	break;
				   }
				   else sss+=cnt;
				}
				
			}
		}
		for(int s=0;s<strlen(a);s++)
		cout<<ans[s];
		cout<<endl;
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值