現在有"abcdefghijkl”12個字符,將其所有的排列中按字典序排列,給出任意一種排列,說出這個排列在所有的排列中是第幾小的?
輸入
第一行有一個整數n(0<n<=10000); 隨後有n行,每行是一個排列;
|
樣例輸入
3 abcdefghijkl hgebkflacdji gfkedhjblcia
|
輸出
輸出一個整數m,佔一行,m表示排列是第幾位;
|
樣例輸出
1 302715242 260726926 |
public class Main {
public static long postion (int n){
if(n==1){
return 1;
}
return n*postion(n-1);
}
public static long index(char[] p,char c,int inde){
int size=1;
for (int i = 0; i < inde; i++) {
if(c-p[i]>0){
size++;
}
}
return (c-96)-size;
}
public static long getSize(char[] p,int index,long size){
char c=p[index];
if(index==p.length-1){
return 1;
}
long num=index(p, c, index)*postion(12-index-1);
return num+getSize(p, ++index, num);
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
sc.nextLine();
for (int i = 0; i < n; i++) {
String str=sc.nextLine();
char [] cc=str.toCharArray();
System.out.println(getSize(cc, 0, 0));
}
}
}