现在有"abcdefghijkl”12个字符,将其所有的排列中按字典序排列,给出任意一种排列,说出这个排列在所有的排列中是第几小的?
输入描述:
第一行有一个整数n(0<n<=10000); 随后有n行,每行是一个排列;
输出描述:
输出一个整数m,占一行,m表示排列是第几位;
样例输入:
复制
3 abcdefghijkl hgebkflacdji gfkedhjblcia
思路:
康拓展开
所谓康拓展开是指把一个整数X展开成如下形式:
X=a[n]*(n-1)!+a[n-1]*(n-2)!+...+a[i]*(i-1)!+...+a[2]*1!+a[1]*0!
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<math.h>
#include<map>
#include<vector>
#include<stack>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N=10005;
int cl[11]={1,2,6,24,120,720,5040,40320,362880,3628800,39916800};
//打表,1到11的阶乘
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
char a[13];
int i,j,ans=0,temp;
scanf("%s",a);
for(i=0;i<11;i++)
{
temp=0;
for(j=i+1;j<12;j++)
if(a[i]>a[j])
temp++;
ans+=temp*cl[10-i];
}
printf("%d\n",ans+1);
}
return 0;
}