我排第几个
时间限制:
1000 ms | 内存限制:
65535 KB
难度:
3
-
描述
-
现在有"abcdefghijkl”12个字符,将其所有的排列中按字典序排列,给出任意一种排列,说出这个排列在所有的排列中是第几小的?
-
输入
-
第一行有一个整数n(0<n<=10000);
随后有n行,每行是一个排列;
输出
- 输出一个整数m,占一行,m表示排列是第几位; 样例输入
-
3 abcdefghijkl hgebkflacdji gfkedhjblcia
样例输出
-
1 302715242 260726926
康拓展开模板题~~~
#include <stdio.h>
char str[13];
int Katuo[12];
//取得1~14的阶乘
void init() {
Katuo[0] = 1;
for( int i=1 ; i<12 ; i++ ) {
Katuo[i] = Katuo[i-1] * i;
}
}
//计算pos位置的字符在包括该字符以后的字串中是第几大的
int judge( char *str , int pos ) {
int count = 0;
for( int j=pos+1 ; str[j] ; j++ ) {
if( str[pos]>str[j] )
count++;
}
return count;
}
void Kangtor( char *s ) {
int count = 0;
for( int i=0 ; s[i] ; i++ )
count += Katuo[11-i] * judge( s,i );
printf( "%d\n",++count );
}
int main() {
init();
// freopen( "input.txt","r",stdin );
int t;
long long count;
scanf( "%d",&t );
while( t-- ) {
scanf( "%s",&str );
Kangtor( str ) ;
}
}